Skip to content

Commit a3cab5e

Browse files
authored
Add generic plot widgets and demos (#341)
* Add generic plot widgets and demos - add ScatterPlotWidget, BarPlotWidget, and HistogramWidget with shared series normalization - wire new widgets into exports and anywidget frontend mapping, plus notebook/marimo/vscode demos - harden trajectory dict validation for empty species lists and extend widget tests * Fix marimo widget rendering and improve demo notebooks Marimo serializes cell outputs independently with a ~10 MB limit, causing widgets embedding ~10 MB of inline JS to fail. anywidget also only treats _esm values starting with http(s):// as URLs. - Serve ESM via marimo's virtual-file system with absolute URL resolution so anywidget imports it as a URL (not inline source) - Keep CSS inline (~166 KB) to avoid stylesheet loading edge cases - Add _in_marimo_runtime, _marimo_esm_url, _init_marimo_assets with 27 tests including mutation-verified coverage - Fix marimo demo cell dependency wiring (_struct, _phonon_doc, _band_data, _dos_data), broken markdown cells, and ruff noqa - Add marimo check --strict pre-commit hook - Fix RUF005 in bar_plot.py, TRY300 in matterviz.py * Update widget demos with realistic fixture data and clearer narratives Replace synthetic band/DOS demo payloads with phonon test fixtures and add concise explanatory headings across notebook, marimo, and VSCode interactive demos. * Guard against marimo VS Code extension kernel crash - Skip js() virtual file call when virtual_files_supported=False to prevent ~14 MB base64 data URL allocations that crash the kernel - Fall back to relative ./@file/ URL when request context is unavailable - Set resolved ESM URL on class (not per-instance) to avoid N copies - Use display() from IPython for plot widgets that shadow the method - Document VS Code extension limitation in MatterVizWidget docstring * Simplify marimo ESM caching in MatterVizWidget Use dedicated class-level marimo URL cache fields instead of overloading the generic asset tuple cache, and keep test cache cleanup aligned with the new cache state. * Auto-discover to_dict fields from synced traitlets and reorganize tests Replace manual state_fields tuples with automatic discovery via self.traits(sync=True) in MatterVizWidget.to_dict(). This fixes serialization for ScatterPlotWidget and HistogramWidget which were missing their state_fields definitions, and eliminates the need for every subclass to maintain a redundant field list. Also simplify production code (DRY numpy conversion in normalize_plot_json, loop-ify lattice params, remove narrating comments in trajectory.py) and split test_new_widgets.py into test_normalize.py, test_widget_construction.py, and test_mime.py organized by domain rather than chronology. * Rename display() to show(), add configure_assets(), fix marimo cell deps - Rename MatterVizWidget.display() to show() to avoid shadowing the 'display' traitlet on ScatterPlot/BarPlot/HistogramWidget which made widget.display() raise TypeError: 'dict' object is not callable - Add configure_assets(version=, esm_src=, css_src=) for global asset override via version tags, HTTP URLs, or local file paths - Fix marimo demo: cells now properly return and declare dependencies for _struct, _band_data, _phonon_doc, _dos_data - Simplify matterviz.py: flatten _marimo_esm_url resolution, trim docstrings, merge isinstance checks in normalize_plot_json * Add CI job to execute widget demos Runs all three widget demos (vscode, marimo, jupyter) in a single consolidated step to catch runtime errors in demo scripts.
1 parent 485ed72 commit a3cab5e

20 files changed

+2866
-879
lines changed

.github/workflows/test.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ jobs:
4242
test-cmd: pytest --durations 20 --cov-branch --cov-report=xml --cov pymatviz --splits 4 --group ${{ matrix.split }} --splitting-algorithm least_duration
4343
upload-coverage: strict
4444

45+
run-widget-demos:
46+
runs-on: ubuntu-latest
47+
steps:
48+
- name: Check out repository
49+
uses: actions/checkout@v4
50+
51+
- name: Setup Python
52+
uses: actions/setup-python@v5
53+
with:
54+
python-version: '3.11'
55+
56+
- name: Setup uv
57+
uses: astral-sh/setup-uv@v6
58+
59+
- name: Build widget assets, install deps, run all demos
60+
run: |
61+
curl -fsSL https://deno.land/install.sh | sh
62+
export PATH="$HOME/.deno/bin:$PATH"
63+
cd pymatviz/widgets/web && deno task build && cd ../../..
64+
uv pip install -e '.[test]' marimo nbconvert --system
65+
python examples/widgets/vscode_interactive_demo.py
66+
marimo export script examples/widgets/marimo_demo.py -o /tmp/marimo_flat.py && python /tmp/marimo_flat.py
67+
jupyter nbconvert --to notebook --execute --ExecutePreprocessor.timeout=120 examples/widgets/jupyter_demo.ipynb --output /tmp/jupyter_out.ipynb
68+
4569
find-example-scripts:
4670
runs-on: ubuntu-latest
4771
outputs:

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ repos:
2626
files: ^(readme\.md|pymatviz/.*\.py)$
2727
pass_filenames: false
2828

29+
- repo: local
30+
hooks:
31+
- id: marimo-check
32+
name: marimo check
33+
entry: marimo check --strict
34+
language: python
35+
files: examples/widgets/marimo_demo\.py$
36+
additional_dependencies: [marimo]
37+
2938
- repo: https://github.com/janosh/format-ipy-cells
3039
rev: v0.1.11
3140
hooks:

0 commit comments

Comments
 (0)