A Quarto filter extension that creates interactive molecular structure editors and viewers using MolViewSpec and MolStar.
- 🧬 Interactive 3D molecular structure visualization
- ✏️ Built-in Monaco code editor with live preview
- 🎨 Customizable layout and dimensions
- 🔧 Optional interactive controls (auto-update toggle, execution log)
- 📦 Works offline - all dependencies bundled
- 🚀 Uses @molstar/molstar-components from JSR
quarto add molstar/molviewspec-quartoAdd the filter to your document's YAML header:
---
title: "My Document"
format: html
filters:
- molviewspec-quarto
---Then create a {.molviewspec} code block with JavaScript builder code:
```{.molviewspec}
const structure = builder
.download({ url: 'https://www.ebi.ac.uk/pdbe/entry-files/1cbs.bcif' })
.parse({ format: 'bcif' })
.modelStructure();
structure
.component({ selector: 'polymer' })
.representation({ type: 'cartoon' })
.color({ color: 'blue' });
```Customize your viewers with these attributes:
title- Optional title for the viewerheight- Container height (default:"400px")width- Container width (default:"100%")
layout- Editor/viewer arrangement:"horizontal"or"vertical"(default:"horizontal")editorHeight- Editor panel height (default:"400px")viewerHeight- Viewer panel height (default:"400px")
autoRun- Auto-execute code on edit:"true"or"false"(default:"true")autoRunDelay- Delay before auto-execution in ms (default:500)controls- Enable interactive controls:"true"or"false"(default:"false")
With title:
```{.molviewspec title="Protein Structure"}
// your code here
```With interactive controls:
```{.molviewspec controls="true" title="Interactive Demo"}
// your code here
```When controls="true", users can:
- Toggle auto-update on/off
- View execution logs for debugging
Custom dimensions:
```{.molviewspec height="800px" width="100%"}
// your code here
```Split your code using --- as a separator:
- Story code (above
---): Runs but hidden from editor - for setup/imports - Scene code (below
---): Visible and editable in the Monaco editor
```{.molviewspec}
// Hidden setup code
const structure = builder
.download({ url: 'https://www.ebi.ac.uk/pdbe/entry-files/1cbs.bcif' })
.parse({ format: 'bcif' })
.modelStructure();
---
// Visible, editable code
structure
.component({ selector: 'polymer' })
.representation({ type: 'cartoon' })
.color({ color: 'purple' });
```The builder variable provides a fluent API for creating molecular visualizations:
download()- Load structure from URLparse()- Parse file (bcif, cif, pdb, etc.)modelStructure()- Create model structure nodecomponent()- Select parts (polymer, ligand, water, etc.)representation()- Add visuals (cartoon, ball_and_stick, surface, spacefill, etc.)color()- Apply colors (named colors or hex codes)focus()- Focus camera on component
Resources:
# Build and preview
deno task build && quarto preview index.qmd
# Build only
deno task build- Quarto >= 1.8.0
- HTML output format (non-HTML formats display original code block)
- Modern browser with ES6+ support
- Lua filter detects
{.molviewspec}code blocks - Extracts JavaScript builder code
- Generates HTML containers with unique IDs
- Loads bundled MolStar library and Monaco editor
- Initializes interactive editor/viewer components
- Executes code and renders molecular structure
Override these CSS classes to customize appearance:
.molviewspec-container- Main container.molviewspec-viewer- Viewer wrapper.molviewspec-header- Title header.molviewspec-title- Title text
Viewers not rendering?
- Check browser console for errors
- Ensure HTML output format
- Verify JavaScript syntax in code blocks
Monaco workers failing?
- Check Network tab for 404s on worker files
- Ensure assets are properly deployed with your site
MIT
Zachary Charlop-Powers