|
| 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 |
0 commit comments