Molecular visualization driven by a local WebSocket-hosted page
Project description
molvis
Python package for MolVis molecular visualization. A single
mv.Molvis() class works from both plain Python scripts (opens a
browser tab) and Jupyter notebooks (mounts the page bundle inline in
the cell, isolated by Shadow DOM — no iframe). Both hosts drive the
same page bundle over a local WebSocket.
Installation
pip install molvis
Quick start
Plain Python script
import molvis as mv
import molpy as mp
viewer = mv.Molvis() # starts a local WebSocket + opens a browser tab
viewer.draw_frame(frame)
viewer.snapshot() # PNG screenshot
Jupyter notebook
import molvis as mv
scene = mv.Molvis(name="protein")
scene.draw_frame(frame)
scene # mounts the viewer inline in the cell
Both modes use the same command API (draw_frame, set_style,
snapshot, selection, palettes, …) and the same event channel.
Bidirectional events
Frontend interactions (selection, mode, frame navigation, …) flow back into Python. Selection is a shared state — canvas clicks are observable from Python without polling.
viewer = mv.Molvis()
# Subscribe to a frontend event (callback runs on the WS thread)
handle = viewer.on("selection_changed",
lambda ev: print("sel:", ev["atom_ids"]))
handle.remove() # unsubscribe
# Block until a specific event fires
ev = viewer.wait_for("selection_changed", timeout=30)
# Cached state, updated live by incoming events — no RPC roundtrip
print(viewer.selection) # Selection(atom_ids=(...), bond_ids=(...))
print(viewer.current_mode) # "view" | "select" | "edit" | ...
print(viewer.current_frame)
# Force a fresh snapshot from the canvas
viewer.refresh_state()
Transport
mv.Molvis() auto-creates a WebSocketTransport. Outside Jupyter it
opens the page in the default browser; inside Jupyter the cell mounts
the bundle inline. For advanced setups (CDN-hosted page, explicit port,
CORS …) pass your own transport:
viewer = mv.Molvis(transport=mv.WebSocketTransport(
page_base_url="https://molvis.dev/app", # host page on a CDN
port=8765, # fixed port instead of OS-assigned
open_browser=False,
))
Token authentication is automatic: the page URL carries a one-time token that the frontend must echo back in its hello handshake.
Scene registry
a = mv.Molvis() # default scene
b = mv.Molvis()
assert a is b # same instance
protein = mv.Molvis(name="protein")
ligand = mv.Molvis(name="ligand")
mv.Molvis.list_scenes()
mv.Molvis.get_scene("protein")
protein.close() # stop transport + drop from registry
Drawing API
scene = mv.Molvis()
scene.draw_frame(frame, style="ball_and_stick")
scene.draw_box(box)
scene.draw_atoms(atoms, style="spacefill")
scene.new_frame()
scene.set_style(style="spacefill", atom_radius=0.5)
scene.set_theme("modern") # "classic" | "modern"
scene.clear()
Query commands (block until the frontend responds)
png_bytes = scene.snapshot()
frame = scene.export_frame()
selected = scene.get_selected() # mp.Frame with just the selection
scene.select_atom_by_id([0, 2])
Palette utilities
import molvis as mv
from IPython.display import Image
scene = mv.Molvis()
scene # render the cell mount first
scene.list_palettes()
scene.palette_entries("cpk")[:5]
scene.palette_colors("glasbey-vivid")[:8]
png = scene.palette_preview("glasbey-vivid")
Image(data=png)
Error handling
Fire-and-forget commands (e.g. draw_frame) log errors asynchronously.
Query commands raise MolvisRPCError:
try:
scene.export_frame()
except mv.MolvisRPCError as exc:
print(exc.code, exc)
Development
npm run build:page # build page bundle + copy to dist/
cd python && python -m pytest tests/ -v
Packaging
npm run build:page
cd python && python -m build --wheel
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file molcrafts_molvis-0.0.6.tar.gz.
File metadata
- Download URL: molcrafts_molvis-0.0.6.tar.gz
- Upload date:
- Size: 6.4 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5bff6cb1d3894067967a870b981845156beafd9676c9a3a7abe719e5abb76951
|
|
| MD5 |
01b4a68e7b6c4ff99fb37557d0c37b57
|
|
| BLAKE2b-256 |
3bde1d2dc9acdc670d8df4b13458c38742a5eaa98d4d15f06077794eddf07ab8
|
Provenance
The following attestation bundles were made for molcrafts_molvis-0.0.6.tar.gz:
Publisher:
release-python.yml on MolCrafts/molvis
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
molcrafts_molvis-0.0.6.tar.gz -
Subject digest:
5bff6cb1d3894067967a870b981845156beafd9676c9a3a7abe719e5abb76951 - Sigstore transparency entry: 1405708658
- Sigstore integration time:
-
Permalink:
MolCrafts/molvis@a458a1bc1a4268694247f709ea5cb9fa753674c9 -
Branch / Tag:
refs/tags/v0.0.6 - Owner: https://github.com/MolCrafts
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-python.yml@a458a1bc1a4268694247f709ea5cb9fa753674c9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file molcrafts_molvis-0.0.6-py3-none-any.whl.
File metadata
- Download URL: molcrafts_molvis-0.0.6-py3-none-any.whl
- Upload date:
- Size: 6.4 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14e22bdc6ca3eb00ef87f3ca4485375f3f6bd144d224366840ebdb9cc4b05df3
|
|
| MD5 |
798da1279cb64442ce8cbd4bb258408b
|
|
| BLAKE2b-256 |
1ba907f44f18fcfdb43db76617f0a266b170cbe26a1952052a31fc2131a11efe
|
Provenance
The following attestation bundles were made for molcrafts_molvis-0.0.6-py3-none-any.whl:
Publisher:
release-python.yml on MolCrafts/molvis
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
molcrafts_molvis-0.0.6-py3-none-any.whl -
Subject digest:
14e22bdc6ca3eb00ef87f3ca4485375f3f6bd144d224366840ebdb9cc4b05df3 - Sigstore transparency entry: 1405708671
- Sigstore integration time:
-
Permalink:
MolCrafts/molvis@a458a1bc1a4268694247f709ea5cb9fa753674c9 -
Branch / Tag:
refs/tags/v0.0.6 - Owner: https://github.com/MolCrafts
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-python.yml@a458a1bc1a4268694247f709ea5cb9fa753674c9 -
Trigger Event:
push
-
Statement type: