Skip to main content

Molecular visualization tools

Project description

COSMol-viewer

A high-performance molecular viewer for Python and Rust, powered by a unified Rust core. It supports both in-notebook visualization and native desktop rendering, with smooth playback for scientific animations.

COSMol-viewer is a compact, cross-platform renderer for molecular and geometric scenes. Unlike purely notebook-bound solutions such as py3Dmol, COSMol-viewer runs everywhere:

  • Native desktop window (Python or Rust) via egui
  • Jupyter / IPython notebook via WASM backend
  • Rust applications

All implementations share the same Rust rendering engine, ensuring consistent performance and visual output.


Quick concepts

  • Scene: container for shapes (molecules, proteins, spheres, etc.).
  • Viewer.render(scene, ...): create a static viewer in a native window or notebook canvas. Good for static visualization.
  • viewer.update(scene): push incremental changes after Viewer.render() (real-time / streaming use-cases).
  • Animation: An Animation object containing frames and settings.
  • Animation(interval, loops, interpolate): stores precomputed frames and playback settings.
  • Viewer.play(animation, width, height): recommended for precomputed animations and demonstrations. The viewer takes care of playback timing and looping.

Why prefer play for demos?

  • Single call API (hand off responsibility to the viewer).
  • Built-in timing & loop control.
  • Optional interpolate mode between frames for visually pleasing playback even when input frame rate is low.

Why keep update?

  • update is ideal for real-time simulations, MD runs, or streaming data where frames are not precomputed. It provides strict fidelity (no interpolation) and minimal latency.

Usage

python

See examples in Google Colab.

Install with pip install cosmol-viewer

1. Static molecular rendering

from cosmol_viewer import Molecule, Scene, Viewer

mol_data = open("molecule.sdf", "r", encoding="utf-8").read()

mol = Molecule.from_sdf(mol_data).centered()

scene = Scene()

scene.set_scale(1.0)

scene.add_shape_with_id("molecule", mol)

viewer = Viewer.render(scene, width=800, height=500)

viewer.save_image("screenshot.png")  # Native desktop backend.

print("Press Any Key to exit...", end='', flush=True)
_ = input()  # Keep the viewer open until you decide to close

2. Animation playback with Viewer.play

from cosmol_viewer import Scene, Viewer, Molecule, Animation

anim = Animation(interval=0.05, loops=-1, interpolate=False)
for i in range(1, 10):
    with open(f"frames/frame_{i}.sdf", "r") as f:
        mol = Molecule.from_sdf(f.read())

    scene = Scene()
    scene.add_shape(mol)
    anim.add_frame(scene)

Viewer.play(anim, width=800, height=500)  # loops=-1 for infinite repeat

3. Protein cartoon rendering

from cosmol_viewer import Protein, Scene, Viewer

mmcif_data = open("protein.cif", "r", encoding="utf-8").read()
protein = Protein.from_mmcif(mmcif_data).centered().color("#10ACBF")

scene = Scene()
scene.add_shape_with_id("protein", protein)

viewer = Viewer.render(scene, width=800, height=500)

Protein.from_mmcif() and Protein.from_pdb() parse backbone atoms and use the Rust core to assign secondary structure before rendering a ChimeraX-style cartoon ribbon mesh.

more examples can be found in the examples folder:

cd cosmol_viewer
python .\examples\render_protein.py

Documentation

Please check out our documentation at here.


Contact

For any questions, issues, or suggestions, please contact wjt@cosmol.org or open an issue in the repository. We will review and address them as promptly as possible.

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.2.16.tar.gz (112.4 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.2.16-cp39-abi3-win_amd64.whl (6.8 MB view details)

Uploaded CPython 3.9+Windows x86-64

cosmol_viewer-0.2.16-cp39-abi3-win32.whl (6.6 MB view details)

Uploaded CPython 3.9+Windows x86

cosmol_viewer-0.2.16-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.9 MB view details)

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

cosmol_viewer-0.2.16-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (8.2 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ i686

cosmol_viewer-0.2.16-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.5 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

cosmol_viewer-0.2.16-cp39-abi3-macosx_11_0_arm64.whl (6.2 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

cosmol_viewer-0.2.16-cp39-abi3-macosx_10_12_x86_64.whl (6.6 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file cosmol_viewer-0.2.16.tar.gz.

File metadata

  • Download URL: cosmol_viewer-0.2.16.tar.gz
  • Upload date:
  • Size: 112.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for cosmol_viewer-0.2.16.tar.gz
Algorithm Hash digest
SHA256 9681af1d83f3447e8951a07dda5fbf846f3d92cffdae2232a478f1f3887bbfd9
MD5 c4efc013b73adcbf5467f47333b319ea
BLAKE2b-256 3819197551049239b2f118cb08ddfd028752b9ac18180dac1866cecdb44eafbc

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.2.16-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.2.16-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 3f8d405b14260a35b1d1910941836eb19f9646ba504101acbbb27254cdbb5f9b
MD5 86931f586e55b8038bf460cf8ba60cc3
BLAKE2b-256 7a5ac0ccacc14a9f20b649c14d04b8ffb8a75e5943e303c51ae912431e34061b

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.2.16-cp39-abi3-win32.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.2.16-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 734096ea0cd9e2c20ed61630e47cf1b5ef25ff6850ceebbc8083984ca936180b
MD5 3f8ae97a892cf9ec0144948aa389beb8
BLAKE2b-256 208f9190f9ba9efad3ee0cf19bc34847468a8dbb4b9cfe8539aedf60f6395b2a

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.2.16-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.2.16-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 60276cb282811f32a33509f090c201b1a665a4ca9046dbfa9ec6bc2e8a324f96
MD5 520f1c6755254416fb0174c0f70fb83d
BLAKE2b-256 dfcbe398b3f07f163e8ea91c88e8120afa77fc904ef639197e7bb62a56234c9b

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.2.16-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.2.16-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 064c76bb67277f4a7c97fab033af1c73c674be927f2db0891551b14da7a48c9c
MD5 663a8d5f687d239ba4afd0a70b1c7fb6
BLAKE2b-256 0ef2b24eef09d7f20da7bb3191d6c276223eb807b2d556662ea91c90ac30cccd

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.2.16-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.2.16-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0873551df1e0da4742a98d09e7f5f8f0dccead4db8b0f54b926b926b0bab52ca
MD5 38d9ed98fa340b5287cb5661a645d7f5
BLAKE2b-256 cd3bc10d02560048e3b4ff82a5d7dad6e2e0637c61bc3768e50d44031cf6a952

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.2.16-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.2.16-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f58630108664b594cb1fcb86078c6892b002da870eb54867b8ed062ff026c5f8
MD5 4416009537e23b61d5b132a07e75e2eb
BLAKE2b-256 f0765058d715d01d74184d3338b51e348af3bd789e981e1505d869c7c36b7e6b

See more details on using hashes here.

File details

Details for the file cosmol_viewer-0.2.16-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for cosmol_viewer-0.2.16-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c361e4f4cf839d970d9463346611fa3334bdc331e44e6feb86138524cf258074
MD5 f2d61c437432279be7db995759a210f8
BLAKE2b-256 68d6fe891a44464cb91e468ca4b1da078e8017dd336d072bf8dcb60e50cb2c74

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