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().rainbow_residues()

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. Use .rainbow_residues() for ChimeraX-style residue rainbow coloring, or .color("#10ACBF") for a uniform cartoon color.

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.18.tar.gz (115.5 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.18-cp39-abi3-win_amd64.whl (6.9 MB view details)

Uploaded CPython 3.9+Windows x86-64

cosmol_viewer-0.2.18-cp39-abi3-win32.whl (6.8 MB view details)

Uploaded CPython 3.9+Windows x86

cosmol_viewer-0.2.18-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.1 MB view details)

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

cosmol_viewer-0.2.18-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (8.4 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ i686

cosmol_viewer-0.2.18-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.7 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

cosmol_viewer-0.2.18-cp39-abi3-macosx_11_0_arm64.whl (6.4 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

cosmol_viewer-0.2.18-cp39-abi3-macosx_10_12_x86_64.whl (6.8 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for cosmol_viewer-0.2.18.tar.gz
Algorithm Hash digest
SHA256 e5bd33a1a6f73c0867373b22d7660ac96c424429494b76703f5d84e1eeace83b
MD5 aefa8c26977867c69721e584ba55898f
BLAKE2b-256 214727a3d05bea43290fe6d336600b6b76a55f1e85d83b7e0eb62100141ad3e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.18-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b50057c5fa3f29ee64e4cf2a6265409b88600de423df47bdadaf99f6d9b5fbe7
MD5 13a3395095cb25657b94541466973e38
BLAKE2b-256 eaf61f6094798071780ca6fec7f0e98ac0eee31040499184fa3da2b16a3d7888

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.18-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 b3cac6bdc5b6d142e6e299281dda22fafacfbc331175a00f7cd54d5a35feaf86
MD5 c7754149570b48e46fa3b586a1efffec
BLAKE2b-256 39dae8c5683427dcbdac236eff40aef9fd0ffff27c4313ae9a2b3de426a021e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.18-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f20657e64a363c6ffc735e45d8898890201b21fef7ea254c3dad22da894363da
MD5 e4b3f86d8da6a2bdb7b82b49a0008cd0
BLAKE2b-256 651899ead30cb33a7569085e5f2456a2c19a3849e5354dba272dab64af0363c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.18-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 86d6259c3109146772d9922ca2587e9015eac1178f24363f31cd47e564353704
MD5 ed51301157e32a971ddf9c6eb68d9b20
BLAKE2b-256 ea5464b8f8e6f0aa7093aa2e6bc7ea5222479ad18bac05ed091ef45da9566781

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.18-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 624f07379e1354cde2a9f45ca5ee89b2456c84a589eaad84a0aad79c816e8cdb
MD5 f753d037167925ddd3624191147c2875
BLAKE2b-256 9fad8325aef4e69bed3e659691ff38dba72b0b55c70a26ef9d23be24d6d9e4d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.18-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 686d02b8a7345e2033226fae7183b590cab8e918ae65bd3c6d6935cb1ae58bcb
MD5 b29df63403201f4e68da86c57eb4f463
BLAKE2b-256 bffdfd050f5ec84586ff78494f3829b86b2f8a46296c79a1a6a49b754dc6a39f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.18-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8a69e91d2bf8c2af0bb62e633378eabcf8c6b1e66bb2170872c6a6f8a81b5d83
MD5 cf4d1b045880247b088193fa6713b13b
BLAKE2b-256 be9037d5f684fc0cb4d6affef3dbd9436e78ce9b4da480ab124f74a067892fe9

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