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

Uploaded CPython 3.9+Windows x86-64

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

Uploaded CPython 3.9+Windows x86

cosmol_viewer-0.2.17-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.17-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.17-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.17-cp39-abi3-macosx_11_0_arm64.whl (6.2 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

cosmol_viewer-0.2.17-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.17.tar.gz.

File metadata

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

File hashes

Hashes for cosmol_viewer-0.2.17.tar.gz
Algorithm Hash digest
SHA256 28f5cb83b0b169a3445f01e7f24fb6c880c253f8630d9dc7c944e41f27eeede6
MD5 6cad50be9b332e52a7250af664c9a346
BLAKE2b-256 42fd05c25c9f8bb9a5ef84eeb099272a253c67a1a959158a64d8247b8ed8eec0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.17-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 68d1ebae562222f500dd39cd58d4299bb0ba89d32106aa353338955d9a0bdf5e
MD5 c6179e622de88dc11fb6bd6adf2b6551
BLAKE2b-256 9d23a334488eb12d9e8649e83ed3c33f0cb2c87c04ce65a8b845bffea60b0bfd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.17-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 4812a0f412ff039f7585ee0ab2507914041443e6ce27bce77e62a1686df5ccf3
MD5 d5b206983315fe9185fbb0d51df151e6
BLAKE2b-256 287c5a2ba1c4caf45fecb884a41d69c9eab48105ffda681f5f1c12443348cc75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.17-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9ddab39eac276fb136d5b145a128c7ca4953763e9b38fb04297fcd50a80e86b2
MD5 a3ac65fb98fa3157190e584cd6ad3278
BLAKE2b-256 6e79dfd1f210f9cefb0712726b332d0941013135df4da9990a6036b4ded6c513

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.17-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 9dec5e989db1cd99f9876d614822af623cc3945e4a197cab1ee89d09e167d737
MD5 406ce57222b842e4458fd4a9f226fc4b
BLAKE2b-256 53ed421d95c8e3f829e0d4a1c154b988b928230814327ad4cafd5dd9e5e109f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.17-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4d7bf44372a3758acfc9dca48e11eb0bbac3cb8af6eed1a75d17f3ed0a31ee1f
MD5 0f90d844ccf02ae4f05faa85b6aa3f39
BLAKE2b-256 c352030e2e0ad6a0952fbc12e128d385d8a74a91f6e2983bee5b9b7e6efde091

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.17-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a209cabbebef15ef6a72bf400c581592b29602a8e402106c640b83c022d65a9c
MD5 84ab2f50fe503b3fdbdc6bdcbdf25f02
BLAKE2b-256 68ee8bc013abab95e3c3391f9a21de9116119963f49020b17d89ac5011ca8fe4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.17-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a7b24a98aa41a3516fce1ba9b180c58ee2fa28a074a0ffa997ac062c21d73d7e
MD5 3c2c844447febda01a962c6328090994
BLAKE2b-256 3df737bfa49d09f61557099e7be973920e7236e07099a578580876e49ffebf98

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