Skip to content

Commit c781a72

Browse files
add architecture docs
credits to @christian-oreilly
1 parent c5a4628 commit c781a72

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

docs/source/architecture.rst

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
Architecture
2+
============
3+
4+
IPyNiiVue is a widget that bridges WebGL-powered JavaScript visualization (Niivue) with Python in notebooks. This document contains an overview of how JavaScript interacts with Python in this library.
5+
6+
System Overview
7+
---------------
8+
9+
- **Python Side**: Users interact with the ``NiiVue`` class to load and manipulate neuroimaging data
10+
- **JavaScript Side**: Handles WebGL rendering and user interactions in the browser
11+
- **Communication Layer**: Traitlets synchronize state between Python and JavaScript via WebSocket (JupyterLab) or HTTP (Marimo)
12+
13+
Architecture Layers
14+
-------------------
15+
16+
1. Python Backend (Kernel)
17+
~~~~~~~~~~~~~~~~~~~~~~~~~~
18+
19+
Runs in the notebook kernel process and contains:
20+
21+
- ``NiiVue``, ``Volume``, ``Mesh``, and ``MeshLayer`` classes
22+
- State management through traitlets
23+
- Data processing and computational logic
24+
- Chunked data handling for large data from the frontend (to overcome Tornado's 10MB limit)
25+
26+
2. Widget Bridge Layer
27+
~~~~~~~~~~~~~~~~~~~~~~
28+
29+
Provided by anywidget framework:
30+
31+
- Maintains synchronized widget models between kernel and browser
32+
- Handles state synchronization via traitlets
33+
- Manages WebSocket/HTTP communication
34+
- Serializes/deserializes data between Python and JavaScript
35+
36+
3. JavaScript Frontend (Browser)
37+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38+
39+
Runs in the browser and includes:
40+
41+
- WebGL rendering via Niivue.js library
42+
- UI event handling (mouse, keyboard interactions)
43+
- Visual output in notebook cells
44+
45+
Data Flow
46+
---------
47+
48+
.. mermaid::
49+
50+
flowchart LR
51+
subgraph "Kernel"
52+
A[Python Backend<br/>NiiVue class]
53+
end
54+
55+
subgraph "Communication Layer"
56+
B[Widget Bridge<br/>Traitlets sync<br/>WebSocket/HTTP]
57+
end
58+
59+
subgraph "Browser"
60+
C[JavaScript Frontend<br/>WebGL rendering<br/>User interactions]
61+
D[Notebook Output Cell<br/>Visual display]
62+
end
63+
64+
A <--> B
65+
B <--> C
66+
C <--> D
67+
68+
Communication Patterns
69+
~~~~~~~~~~~~~~~~~~~~~~
70+
71+
1. **State Updates**: Property changes (opacity, colormap, etc.) sync automatically via traitlets
72+
2. **Large Data Transfer**: Volume/mesh data transmitted in chunks to handle size limitations
73+
3. **Efficient Updates**: Only array differences (indices + values) sent for existing data
74+
4. **Event Handling**: User interactions in JS trigger Python callbacks via custom messages
75+
76+
Some Implementation Details
77+
---------------------------
78+
79+
**JavaScript Bundle**:
80+
81+
- Source: ``js/`` directory
82+
- Build: esbuild bundles to ``src/ipyniivue/static/widget.js``
83+
- Main components: ``widget.ts``, ``volume.ts``, ``mesh.ts``, ``lib.ts``
84+
85+
**Python Components**:
86+
87+
- ``widget.py``: Core NiiVue widget and data models
88+
- ``serializers.py``: Data conversion between Python and JavaScript
89+
- ``config_options.py``: Configuration management
90+
- ``traits.py``: Custom trait types for specialized data
91+
92+
**Build System**:
93+
94+
- ``build.js``: Generates colormaps and shader names during build
95+
- ``pyproject.toml``: Python packaging configuration

docs/source/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
# -- General configuration ---------------------------------------------------
1818
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
1919

20-
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"]
20+
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"
21+
"sphinxcontrib.mermaid", "myst_parser"]
2122

2223
templates_path = ["_templates"]
2324
exclude_patterns = []

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ A Jupyter Widget for Niivue based on anywidget.
1212
options
1313
traits
1414
enums
15+
architecture
1516
contributing

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ classifiers = [
2525
homepage = "https://github.com/niivue/ipyniivue"
2626

2727
[project.optional-dependencies]
28-
dev = ["watchfiles", "jupyterlab", "ruff", "pytest", "sphinx", "furo"]
28+
dev = ["watchfiles", "jupyterlab", "ruff", "pytest", "sphinx", "furo", "sphinxcontrib-mermaid", "myst-parser"]
2929

3030
[pytest]
3131
nb_test_files = true

0 commit comments

Comments
 (0)