Skip to main content

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

cosmol_viewer-0.1.3.dev2.tar.gz (58.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cosmol_viewer-0.1.3.dev2-cp37-abi3-win_amd64.whl (5.9 MB view details)

Uploaded CPython 3.7+Windows x86-64

cosmol_viewer-0.1.3.dev2-cp37-abi3-win32.whl (5.8 MB view details)

Uploaded CPython 3.7+Windows x86

cosmol_viewer-0.1.3.dev2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.1 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ x86-64

cosmol_viewer-0.1.3.dev2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (9.0 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

cosmol_viewer-0.1.3.dev2-cp37-abi3-macosx_11_0_arm64.whl (5.8 MB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

cosmol_viewer-0.1.3.dev2-cp37-abi3-macosx_10_12_x86_64.whl (6.0 MB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

Details for the file cosmol_viewer-0.1.3.dev2.tar.gz.

File metadata

  • Download URL: cosmol_viewer-0.1.3.dev2.tar.gz
  • Upload date:
  • Size: 58.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for cosmol_viewer-0.1.3.dev2.tar.gz
Algorithm Hash digest
SHA256 e45f4a36798109ebbbc46bf99b2c949e562943e45786da9a97c01d3bb33383d2
MD5 8ae49bc082ff9af97a81ff591c4e826c
BLAKE2b-256 6bde7c65a3e55f0088fa6d0534bb9ce4ba646f50193ada7f8b53fe59908db0a5

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.1.3.dev2-cp37-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.1.3.dev2-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e2450f2313d022f7067a97c681ff5fd24901370f303056ddb0193811466aa5c4
MD5 0582e3a4aef56c5303cf75a321561d87
BLAKE2b-256 a8eaff7dfb0c32ff6419919c26434577a72bc845dbf6d281a3ed2986e60e5460

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.1.3.dev2-cp37-abi3-win32.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.1.3.dev2-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 d6e255b603807a57152d40620114635d715a992b6e899805fc46f7eb3a9c0d4f
MD5 af0bb72d0bbe6227ba3fc9b47b7e6711
BLAKE2b-256 7f861510ad003aa742a608bd428fec668e3c51ab0a7b776747ad2f4754b93630

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.1.3.dev2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.1.3.dev2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ca1b0723878c0774d61a56fcdabee43b89c0047bf71118c8c6c90d1aa12f0604
MD5 c04537c0871495f4fc62f5eb2a1134eb
BLAKE2b-256 f2d9df404baadb188d0457210b065a12278ffee8b4061397f1552b590d8dc5f7

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.1.3.dev2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.1.3.dev2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b27b91946da0770260b0d3bdf57f93086ac1b34bf9501ce821c9b1ab9e14256c
MD5 5131e84a1d56919021aa1d2ddad6c87d
BLAKE2b-256 3b2c3b644420b9db1fa6c1d590bd1602a623bc59694b29834c0f44de9d529936

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.1.3.dev2-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.1.3.dev2-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d444a36180f26773be43a47aadc26975e6f5c5d75854efd5966e6af7f812ddc1
MD5 f4af27611ad1913cd13be67e637b7bef
BLAKE2b-256 5157f3c64dd4cf979558b519c7bb0fb8d617f5b6d287e25fb11610ce2a5841c2

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.1.3.dev2-cp37-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.1.3.dev2-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 97d81ce522b3e5dd28b18c9a199c6c4bd9e8a6ab5f2a391d809e783f8a68663b
MD5 88fe086f0045e3a046b8c3d188ca280f
BLAKE2b-256 7be99fc8f1f91d87cfaf276ddcc85bd191d9c3b52858a132750426696e63abf4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page