# beancount/fava architecture

> Architecture diagram of the GitHub repository beancount/fava, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/beancount/fava
- Repository: https://github.com/beancount/fava

## Overview

##### Main Components

a. Frontend Layer:
- Single Page Application built with Svelte
- Charts and visualization components
- CodeMirror-based editor integration
- Static assets (CSS, fonts)
- Client-side routing system

b. Backend Layer:
- Python-based web server
- Beancount integration layer
- Core modules for:
- Account management
- Query processing
- Document handling
- Charts generation
- File watching
- Extensions system

c. External Dependencies:
- Beancount (core bookkeeping engine)
- File system (for ledger files)
- Browser (client)

##### Component Relationships

- REST/JSON API between frontend and backend
- File system interactions for ledger files
- Beancount library integration
- Extension system connections
- Document processing flow
- Data flow for queries and reports

##### Architectural Patterns to Highlight

- MVC-like pattern:
- Model: Beancount data structures
- View: Svelte components
- Controller: Python backend handlers

- Plugin architecture for extensions
- Event-driven updates for file changes
- Modular report system

##### Specific Implementation Details

Frontend Structure:
**[Browser] → [Svelte Components] → [API Layer] → [Backend]**
- ↳ [Charts]
- ↳ [Editor]
- ↳ [Reports]

Backend Structure:
**[Web Server] → [Core Modules] → [Beancount Integration]**
- ↳ [Extensions]
- ↳ [File Watcher]
- ↳ [Query Engine]

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef backend fill:#2ecc71,stroke:#27ae60,color:white
    classDef external fill:#f1c40f,stroke:#f39c12,color:black
    classDef api fill:#e67e22,stroke:#d35400,color:white
    classDef data fill:#9b59b6,stroke:#8e44ad,color:white

    %% External Systems
    Browser["Browser"]:::external
    Beancount["Beancount Engine"]:::external
    FileSystem["File System"]:::external

    %% Frontend Layer
    subgraph Frontend
        SPA["Single Page Application"]
        Router["Client Router"]
        Charts["Charts & Visualization"]
        Editor["CodeMirror Editor"]
        StaticAssets["Static Assets"]
        EntryForms["Entry Forms"]
        Reports["Report System"]
        QueryInterface["Query Interface"]
    end

    %% Backend Layer
    subgraph Backend
        WebServer["Python Web Server"]
        APILayer["API Layer"]
        BeancountIntegration["Beancount Integration"]
        
        subgraph CoreModules["Core Modules"]
            AccountMgmt["Account Management"]
            QueryProcessor["Query Processing"]
            DocHandler["Document Handler"]
            ChartGen["Charts Generator"]
            FileWatcher["File Watcher"]
            I18n["Internationalization"]
        end
        
        Extensions["Extensions System"]
    end

    %% Relationships
    Browser --> SPA
    SPA --> Router
    SPA --> Charts
    SPA --> Editor
    SPA --> EntryForms
    SPA --> Reports
    SPA --> QueryInterface
    
    SPA --> APILayer
    APILayer --> WebServer
    WebServer --> CoreModules
    WebServer --> Extensions
    BeancountIntegration --> Beancount
    FileWatcher --> FileSystem
    
    CoreModules --> BeancountIntegration
    Extensions --> BeancountIntegration

    %% Click Events
    click SPA "https://github.com/beancount/fava/blob/main/frontend/src/main.ts"
    click Charts "https://github.com/beancount/fava/tree/main/frontend/src/charts/"
    click Editor "https://github.com/beancount/fava/tree/main/frontend/src/codemirror/"
    click StaticAssets "https://github.com/beancount/fava/tree/main/frontend/css/"
    click Router "https://github.com/beancount/fava/blob/main/frontend/src/router.ts"
    click WebServer "https://github.com/beancount/fava/blob/main/src/fava/application.py"
    click BeancountIntegration "https://github.com/beancount/fava/tree/main/src/fava/beans/"
    click AccountMgmt "https://github.com/beancount/fava/blob/main/src/fava/core/accounts.py"
    click QueryProcessor "https://github.com/beancount/fava/blob/main/src/fava/core/query.py"
    click DocHandler "https://github.com/beancount/fava/blob/main/src/fava/core/documents.py"
    click ChartGen "https://github.com/beancount/fava/blob/main/src/fava/core/charts.py"
    click FileWatcher "https://github.com/beancount/fava/blob/main/src/fava/core/watcher.py"
    click Extensions "https://github.com/beancount/fava/tree/main/src/fava/ext/"
    click I18n "https://github.com/beancount/fava/tree/main/src/fava/translations/"
    click APILayer "https://github.com/beancount/fava/blob/main/src/fava/json_api.py"
    click EntryForms "https://github.com/beancount/fava/tree/main/frontend/src/entry-forms/"
    click Reports "https://github.com/beancount/fava/tree/main/frontend/src/reports/"
    click QueryInterface "https://github.com/beancount/fava/tree/main/frontend/src/reports/query/"

    %% Legend
    subgraph Legend
        Frontend_L["Frontend Component"]:::frontend
        Backend_L["Backend Component"]:::backend
        External_L["External System"]:::external
        API_L["API Component"]:::api
        Data_L["Data Flow"]:::data
    end
```
