Molecular visualization tools
Project description
COSMol-viewer
COSMol-viewer is a high-performance molecular visualization library, written in Rust and powered by WebGPU, designed for seamless integration into Python workflows.
- ⚡ High-speed rendering — GPU-accelerated performance at native speed
- 🧬 Flexible input — Load structures from
.sdf,.pdb, or dynamically generated coordinates - 📓 Notebook-ready — Fully compatible with Jupyter and Google Colab, ideal for teaching, research, and interactive demos
- 🔁 Dynamic visualization — Update molecular structures on-the-fly or play smooth preloaded animations
- 🎨 Customizable — Fine-grained control of rendering styles, camera, and scene parameters
Installation
pip install cosmol-viewer
Quick Start
from cosmol_viewer import Scene, Viewer, parse_sdf, Molecules
# === Step 1: Load and render a molecule ===
with open("molecule.sdf", "r") as f:
sdf = f.read()
mol = Molecules(parse_sdf(sdf)).centered()
scene = Scene()
scene.scale(0.1)
scene.add_shape(mol, "mol")
viewer = Viewer.render(scene, width=600, height=400) # Launch viewer
print("Press Any Key to exit...", end='', flush=True)
_ = input() # Keep the viewer open until you decide to close
Animation Modes
COSMol-viewer supports two complementary animation workflows, depending on whether you prefer real-time updates or preloaded playback.
1. Real-Time Updates (Frame-by-Frame Streaming)
Update the molecule directly inside an existing scene:
import time
from cosmol_viewer import Scene, Viewer, parse_sdf, Molecules
scene = Scene()
scene.scale(0.1)
# Initial load
with open("frames/frame_1.sdf", "r") as f:
sdf = f.read()
mol = Molecules(parse_sdf(sdf)).centered()
scene.add_shape(mol, "mol")
viewer = Viewer.render(scene, width=600, height=400)
# Update in real time
for i in range(2, 10):
with open(f"frames/frame_{i}.sdf", "r") as f:
sdf = f.read()
updated_mol = Molecules(parse_sdf(sdf)).centered()
scene.update_shape("mol", updated_mol)
viewer.update(scene)
time.sleep(0.033) # ~30 FPS
print("Press Any Key to exit...", end='', flush=True)
_ = input()
Use cases:
- Visualizing the progress of a simulation step-by-step
- Interactive experiments or streaming scenarios where frames are not known in advance
Trade-offs:
- ✅ Low memory usage — no need to preload frames
- ⚠️ Playback smoothness depends on computation / I/O speed → may stutter if frame generation is slow
2. Preloaded Playback (One-Shot Animation) (Start from 0.1.3)
Load all frames into memory first, then play them back smoothly:
from cosmol_viewer import Scene, Viewer, parse_sdf, Molecules
frames = []
interval = 0.033 # ~30 FPS
# Preload all frames
for i in range(1, 10):
with open(f"frames/frame_{i}.sdf", "r") as f:
sdf = f.read()
mol = Molecules(parse_sdf(sdf)).centered()
scene = Scene()
scene.scale(0.1)
scene.add_shape(mol, "mol")
frames.append(scene)
# Playback once
Viewer.play(frames, interval=interval, loops=1, width=600, height=400)
print("Press Any Key to exit...", end='', flush=True)
_ = input()
Use cases:
- Smooth, stable playback for presentations or teaching
- Demonstrating precomputed trajectories (e.g., molecular dynamics snapshots)
Trade-offs:
- ✅ Very smooth playback, independent of computation speed
- ⚠️ Requires preloading all frames → higher memory usage
- ⚠️ Longer initial load time for large trajectories
Choosing the Right Mode
- ✅ Use real-time updates if your frames are generated on-the-fly or memory is limited
- ✅ Use preloaded playback if you want guaranteed smooth animations and can preload your trajectory
Exiting the Viewer
Important: The viewer is bound to the Python process.
When your script finishes, the rendering window will close automatically.
To keep the visualization alive until you are ready to exit, always add:
print("Press Any Key to exit...", end='', flush=True)
_ = input()
This ensures:
- The window stays open for inspection
- The user decides when to end visualization
- Prevents premature termination at the end of the script
Documentation
For API reference and advanced usage, please see the latest documentation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
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 cosmol_viewer-0.1.3.dev3.tar.gz.
File metadata
- Download URL: cosmol_viewer-0.1.3.dev3.tar.gz
- Upload date:
- Size: 73.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f31c1cc3621d572294b9b3fa48daf80ac7598155599e56e20f41abec663a756b
|
|
| MD5 |
a9cc3bd94b90b1364b616ca6d68fe084
|
|
| BLAKE2b-256 |
b1efd101c5b6acb5733acfda04f8c91858c71a7e767f705549f545e58c360515
|
File details
Details for the file cosmol_viewer-0.1.3.dev3-cp37-abi3-win_amd64.whl.
File metadata
- Download URL: cosmol_viewer-0.1.3.dev3-cp37-abi3-win_amd64.whl
- Upload date:
- Size: 5.9 MB
- Tags: CPython 3.7+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64e7c413a98652194e089db2f32c32ac6e21986dd395192cad99338fe790ac84
|
|
| MD5 |
5b652fe34ef64cfaec1245e7d091d6b9
|
|
| BLAKE2b-256 |
49994b840f11a50ac4cc5f26e019413a0d45a562b08e258dca0e43cfd43ad60e
|
File details
Details for the file cosmol_viewer-0.1.3.dev3-cp37-abi3-win32.whl.
File metadata
- Download URL: cosmol_viewer-0.1.3.dev3-cp37-abi3-win32.whl
- Upload date:
- Size: 5.8 MB
- Tags: CPython 3.7+, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d4c78ca8bb2bdf6ab25d7b994d94769150c0f79626cf7eaa4db1a2f6ea44412
|
|
| MD5 |
62fd97c49bc557230b222f1c3ba3ae75
|
|
| BLAKE2b-256 |
8e414f81fe492db1ed3149e564d5cb2cba544c8b33ab6f07a22fb5a120f612af
|
File details
Details for the file cosmol_viewer-0.1.3.dev3-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: cosmol_viewer-0.1.3.dev3-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 9.1 MB
- Tags: CPython 3.7+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d6ae691a22067314b4fff65d6f64954de6eb9122b7103972ad32582c67e5901
|
|
| MD5 |
ed4c1ed1390fd87ac4aa237f290ec12f
|
|
| BLAKE2b-256 |
0cd1e41fb2892099036f07a164ea22d014575c31c42109165a54361e8abe2f3d
|
File details
Details for the file cosmol_viewer-0.1.3.dev3-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: cosmol_viewer-0.1.3.dev3-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 9.0 MB
- Tags: CPython 3.7+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59a0e4edd3576cd82d9d93efe2a61ad4871deeb586207660ee53b303a1f29dca
|
|
| MD5 |
b1dcd0aa4b1395edc73f7e1282d41582
|
|
| BLAKE2b-256 |
b74396def3859cfebea760b5671ee6ebbf134602b42ea96a5b8aa579bbfcf378
|
File details
Details for the file cosmol_viewer-0.1.3.dev3-cp37-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: cosmol_viewer-0.1.3.dev3-cp37-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 5.8 MB
- Tags: CPython 3.7+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62118c724c859996bcd4d0898ec621548ff73b6329226025b600faa1a0f5044d
|
|
| MD5 |
4502521d99d32d95e47026ab02f6c66a
|
|
| BLAKE2b-256 |
a034b977c34efa00b16c7356d1a8e10aef97d6a869089fc5af9de36816abc19e
|
File details
Details for the file cosmol_viewer-0.1.3.dev3-cp37-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: cosmol_viewer-0.1.3.dev3-cp37-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 6.0 MB
- Tags: CPython 3.7+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4701ee937982c5f1b2bc8cce04da4436b7def55bd3591aac875c453c7f45fbd
|
|
| MD5 |
d94d6f5581c246886177e2d32b025eec
|
|
| BLAKE2b-256 |
f5443e26e4b382c7018e54d8dc92aef8d4423eb37450a42dc34eb31bc8d52b02
|