Interactive Three.js scenes in Jupyter and as standalone HTML: framework, pockets, windows, percolation, pore graph
Mofography has a Three.js backend that mirrors the Plotly plotting API. The scenes are fully interactive (rotate, zoom, layer toggles, lighting, view buttons, measure tool, PNG export) and show up directly inside a notebook, like plotly’s fig.show().
This page uses eval: false blocks, because the documentation site cannot run Python. The screenshots are real renders of the code shown, with the control panel visible. Each scene links to a standalone HTML file you can open and rotate.
Use in a notebook
Build a Figure3js (alias Figure3JS), chain plot_* calls, and either call .show() or leave the figure as the last expression of a cell:
Show code
import mofography as mgrfrom mofography import Figure3jsfrom ase.io import readatoms = mgr.update_with_covalent_bonds(read("examples/TestCIFs/P1-IRMOF-1.cif"))fig = Figure3js(layout="white").plot_framework_as_mesh_3js(atoms).plot_cell_3js(atoms, color="orange")fig.show(height=600) # or just: figfig.save_html("irmof1.html") # standalone file, same controls
How it works:
The scene is embedded as an <iframe srcdoc="..."> with the HTML escaped, so it needs no server and works in Jupyter classic, JupyterLab, VS Code, and static nbconvert or Quarto output. Each figure gets a unique iframe id.
show(height=600, width="100%") sets the size. Outside a Jupyter kernel show() serves the scene on 127.0.0.1 and opens your browser. Force either with show(renderer="notebook") or show(renderer="browser").
show_percolation_threejs(...) returns an HTML string that also renders inline and has .show() and .save_html(path).
Limits:
The scene HTML loads three.js from the jsdelivr CDN, so the viewer needs internet access. Offline or JupyterLite pages without outbound access will show an empty frame.
The whole scene is inside the page. Isosurface scenes are several MB, and a notebook with many figures grows quickly. Use save_html and link to the file for large scenes.
Remote kernels work, because rendering happens in your browser from the notebook output.
The plot_*_3js methods on Figure3js mirror the Plotly functions, and each returns the figure, so they chain. plot_framework_as_mesh_3js and plot_cell_3js correspond to plot_framework_as_mesh and plot_cell.
The Cu-BTC all-in-one scene puts every layer above into one file, each toggleable from the side panel: unit cell, framework with aromatic rings, surface, regions (no-PBC and PBC merged), pocket types, maxima, windows, pore graph, and a percolated cluster. It is about 8 MB, so it is served as a separate page rather than embedded.
Build a scene like it by adding one call per layer to a single Figure3js and saving once:
The full recipe (pore graph, windows, percolated cluster, Taubin smoothing, which layers start hidden) is scripts/dev/gen_gallery_assets.py::build_cubtc.
NoteAbout the example scenes
The hosted example HTML files (the gallery scenes and the scenes on this page) are slimmed to keep the docs light, a few MB each:
Meshes use full detail at 0.5 Å grid spacing (marching-cubes step_size=1).
Atoms and bonds are instanced spheres and cylinders drawn by the viewer, not per-atom meshes.
Per-vertex hover text on surfaces is off (hover_label="" in plot_surface).
Binary packing is on: to_html(..., pack_binary=True) stores arrays as base64 float32/uint16 blobs, typically 3 to 4 times smaller than JSON lists.
Framework display styles are included through the Representation dropdown (Ball-and-stick, Stick, Space-filling). Each style is its own instanced trace, added with plot_framework_instanced_3js(..., representation="stick") or representation="space_filling", and the dropdown shows only the selected one. In the Cu-BTC scene the region-labelled surfaces (Regions (no PBC), Regions (PBC-merged)) and the pocket types are off by default but can be toggled in the legend.
To turn omitted features back on, see scripts/dev/gen_gallery_assets.py: call add_framework(..., extra_styles=True) for the extra framework styles, lower step_size or keep it at 1 for finer meshes (raise it to shrink files), drop hover_label="" to get per-vertex hover text, and pass visible=True to add_surface (or remove the id from the hide list) for layers that should start visible. Expect larger files.
Background and legend labels
to_html(path, background=...) sets the initial viewer background (default None keeps the standard look). It accepts a hex colour, ("linear", c1, c2, ...) (left to right), ("radial", centre, edge), or a dict {"type": ..., "colors": [...]}. The Bg picker and gradient buttons in the viewer override it, and Export PNG captures it.
Give a layer a custom legend text with legend_label on its trace; it replaces the generic label:
for t in fig.data:if t.get("id") =="surface": t["legend_label"] ="Pore surface (distance grid, isovalue 1.0 A)"
Controls
Drag to rotate, scroll to zoom, right-drag to pan.
The panel on the right has view buttons (+X to -Z), perspective or orthographic projection, auto-rotate, a measure tool, light and background settings, and PNG export.
Layer checkboxes at the bottom of the panel toggle the framework, cell, surfaces, maxima, connections, and windows.