How louisnw01/lightweight-charts-python works
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