# louisnw01/lightweight-charts-python architecture

> Architecture diagram of the GitHub repository louisnw01/lightweight-charts-python, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/louisnw01/lightweight-charts-python
- Repository: https://github.com/louisnw01/lightweight-charts-python

## Overview

##### Project’s type and purpose
- It is a polyglot library—a Pythonic wrapper around TradingView’s Lightweight Charts JavaScript library.
- Provides a simple Python API to construct, style, and update interactive financial charts in Jupyter, GUI frameworks (PyQt5/6, PySide6, wxPython), Streamlit, or asyncio apps.
- Bundles a TypeScript codebase (under src/) into a JavaScript library (bundle.js) with Rollup.

##### Top-level components
- Python wrapper (lightweight_charts/ folder & setup.py)
- TS/JS chart engine (src/ folder + rollup.config.js → dist bundle)
- Static assets (lightweight_charts/js/*.js, styles.css, index.html)
- Build & packaging (build.sh, setup.py, package.json, rollup.config.js, tsconfig.json)
- Documentation (docs/ with Sphinx conf.py & Makefile)
- Examples (examples/ Python scripts + CSV data)
- Tests (test/ Python pytest harness + JSON snapshots)
- External service: Polygon.io market data API

##### Major architectural patterns
- Facade: Chart class exposes high-level methods (set, update, styling, callbacks) that abstract away JS interactions.
- Bundler pipeline: TypeScript modules → Rollup → bundle.js injected into Python package.
- Plugin/extension: toolbox, drawing tools, subcharts implemented as pluggable TS modules and Python wrapper objects.
- Multi-environment rendering: Chart.show() chooses environment (browser file://, Jupyter HTML, PyQt WebView, Streamlit iframe).
- Event/callback cross-language bridge: Python callbacks wired to JS events via a lightweight RPC or message channel.

##### How components interact
1. Python user code (e.g., examples/ scripts or end-user app) imports lightweight_charts.Chart.
2. Chart constructs an internal representation of panes, series, toolbox, topbar, etc., and serializes initial data to JSON.
3. Chart.show()
– Launches a local HTML page (index.html) that references bundle.js and communicates via websocket or inline script.
– Spins up a minimal HTTP server (or file://) to serve static assets and listens for update messages.
4. JS chart engine (bundle.js) bootstraps TradingView’s Lightweight Chart in the browser/WebView:
– Receives JSON payloads (initial data, updates, styling commands) over the message channel.
– Renders charts, panes, lines, tables, toolbox, and topbar UI.
– Emits events (search, timeframe change, drawing moves) back to Python.
5. Python wrapper dispatches JS-origin events to user-registered callback functions.
6. For live data, Python may poll or subscribe to Polygon.io (via polygon.py) and push updates to the chart.

##### Building and packaging
- rollup.config.js & tsconfig.json define how src/*.ts → dist/bundle.js
- build.sh builds TS bundle and then python package (setup.py)
- package.json holds Node/TS dev-dependencies
- setup.py / package-lock.json / pip install generate the PyPI package

##### Documentation and testing subsystems
- docs/: Sphinx (conf.py) reads source/ and reference MD files, outputs HTML to ReadTheDocs
- examples/: live code used both in docs and as standalone demos
- test/: pytest suite loads chart code, mocks rendering calls (drawings.json), asserts expected JSON commands

##### External dependencies
- polygon.io REST/WebSocket API for market data
- TradingView Lightweight Charts JS library (vendored in src/js)

–––

Diagramming guidelines

– Python Application / User Code (examples, notebooks)
– Python Wrapper Module
– Local HTTP/WebSocket Server (embedded in Chart.show)
– JS Chart Engine (bundle.js)
– Rendering Environment (Browser / WebView / Streamlit/IFrame)
– External Data API (Polygon.io)
– Build System (Rollup + setup.py)
– Documentation Generator (Sphinx)
– Test Harness (pytest)

1. User code → Python Wrapper (import Chart, call methods)
2. Wrapper → Local server (serialize commands)
3. Local server → JS Engine (deliver JSON messages)
4. JS Engine → Rendering Environment (DOM updates)
5. JS Engine → Wrapper (events over websocket)
6. Wrapper → External API (Polygon requests)
7. Build System → Produces bundle.js & PyPI package
8. Docs Generator → consumes docs/source → HTML
9. Test Harness → exercises wrapper + mocks JS commands → asserts JSON

D. Labels & details
– Annotate key patterns: “Facade”, “Bundle pipeline”, “Event bridge”
– Show optional runtime targets: Jupyter, PyQt, Streamlit

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Python Runtime
    subgraph "Python Runtime"
        direction TB
        UserCode["Python User Code<br/>(examples/, notebooks)"]:::python
        subgraph "Python Wrapper Module"
            direction TB
            ChartPy["chart.py"]:::python
            Abstract["abstract.py"]:::python
            Drawings["drawings.py"]:::python
            Toolbox["toolbox.py"]:::python
            Topbar["topbar.py"]:::python
            Table["table.py"]:::python
            Util["util.py"]:::python
            Widgets["widgets.py"]:::python
        end
        PolygonPy["polygon.py"]:::python
        LocalServer["Local HTTP/WebSocket Server"]:::python
    end

    %% JS Chart Engine
    subgraph "JS Chart Engine"
        direction TB
        IndexTS["index.ts"]:::js
        PluginBase["plugin-base.ts"]:::js
        General["general/*.ts"]:::js
        JSEngine["bundle.js (Engine)"]:::js
        Rendering["Rendering Env<br/>(Browser/WebView/Streamlit)"]:::js
    end

    %% Static Assets
    subgraph "Static Assets"
        direction TB
        JSBundle["bundle.js"]:::js
        VendoredTS["lightweight-charts.js"]:::js
        ShellHTML["index.html"]:::js
        Styles["styles.css"]:::js
    end

    %% Build & Packaging
    subgraph "Build & Packaging"
        direction TB
        RollupConfig["rollup.config.js"]:::build
        TSConfig["tsconfig.json"]:::build
        PackageJSON["package.json"]:::build
        PackageLock["package-lock.json"]:::build
        BuildSh["build.sh"]:::build
        Bundle["bundle.js & PyPI pkg"]:::build
    end

    %% Documentation
    subgraph "Documentation"
        direction TB
        DocsMake["Makefile"]:::build
        ConfPy["conf.py"]:::build
        DocsOutput["HTML Docs"]:::build
    end

    %% Tests
    subgraph "Test Harness"
        direction TB
        RunTests["run_tests.py"]:::build
        TestUtil["util.py"]:::build
        DrawMock["drawings.json"]:::build
    end

    %% Examples
    subgraph "Examples"
        direction TB
        Example1["setting_data.py"]:::python
    end

    %% External Services
    PolygonIO["Polygon.io API"]:::external

    %% Runtime Flows
    UserCode -->|import Chart| ChartPy
    Example1 -->|scripts| UserCode
    ChartPy -->|Facade RPC| LocalServer
    LocalServer -->|"HTTP/WebSocket JSON"| JSEngine
    JSEngine -->|DOM updates| Rendering
    JSEngine -->|events JSON| ChartPy
    ChartPy -->|"REST API JSON"| PolygonIO

    %% Bundle pipeline
    IndexTS -->|"TS modules"| RollupConfig
    PluginBase --> RollupConfig
    General --> RollupConfig
    RollupConfig -->|bundle| JSBundle
    JSBundle -->|packaged| Bundle
    Bundle -.->|included in| JSBundle

    %% Build & Packaging deps
    TSConfig -.-> RollupConfig
    PackageJSON -.-> RollupConfig
    PackageLock -.-> PackageJSON
    BuildSh -.-> RollupConfig
    BuildSh -.-> TSConfig
    BuildSh -->|runs| Bundle
    Bundle -->|deployed via| SetupPy(["setup.py"]):::build

    %% Docs Flow
    DocsMake --> ConfPy
    ConfPy -->|build| DocsOutput

    %% Test Flow
    RunTests --> ChartPy
    RunTests --> DrawMock
    RunTests --> TestUtil

    %% Styling
    classDef python fill:#D0E6F8,stroke:#0366D6,color:#0366D6
    classDef js fill:#FEEBC8,stroke:#DD6B20,color:#DD6B20
    classDef build fill:#E2E8F0,stroke:#718096,color:#2D3748
    classDef external fill:#C6F6D5,stroke:#38A169,color:#2F855A

    %% Click Events
    click ChartPy "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/chart.py"
    click Abstract "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/abstract.py"
    click Drawings "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/drawings.py"
    click Toolbox "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/toolbox.py"
    click Topbar "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/topbar.py"
    click Table "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/table.py"
    click Util "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/util.py"
    click Widgets "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/widgets.py"
    click PolygonPy "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/polygon.py"
    click IndexTS "https://github.com/louisnw01/lightweight-charts-python/blob/main/src/index.ts"
    click PluginBase "https://github.com/louisnw01/lightweight-charts-python/blob/main/src/plugin-base.ts"
    click RollupConfig "https://github.com/louisnw01/lightweight-charts-python/blob/main/rollup.config.js"
    click TSConfig "https://github.com/louisnw01/lightweight-charts-python/blob/main/tsconfig.json"
    click PackageJSON "https://github.com/louisnw01/lightweight-charts-python/blob/main/package.json"
    click PackageLock "https://github.com/louisnw01/lightweight-charts-python/blob/main/package-lock.json"
    click BuildSh "https://github.com/louisnw01/lightweight-charts-python/blob/main/build.sh"
    click JSBundle "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/js/bundle.js"
    click VendoredTS "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/js/lightweight-charts.js"
    click ShellHTML "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/js/index.html"
    click Styles "https://github.com/louisnw01/lightweight-charts-python/blob/main/lightweight_charts/js/styles.css"
    click DocsMake "https://github.com/louisnw01/lightweight-charts-python/tree/main/docs/Makefile"
    click ConfPy "https://github.com/louisnw01/lightweight-charts-python/blob/main/docs/source/conf.py"
    click Example1 "https://github.com/louisnw01/lightweight-charts-python/blob/main/examples/1_setting_data/setting_data.py"
    click RunTests "https://github.com/louisnw01/lightweight-charts-python/blob/main/test/run_tests.py"
```
