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 an interactive viewer in a native window or notebook canvas.
  • scene.save_image(path, ...) / scene.to_png(...) / scene.display(...): render the scene directly to a static PNG at any requested resolution. This is independent of any viewer and does not use notebook JavaScript or browser canvas readback.
  • scene.set_camera_view(...) / scene.rotate_camera(...): set the reproducible camera used by both static exports and newly created viewers.
  • 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)

scene.set_camera_view(azimuth=35, elevation=20, distance=32, fov=18)
scene.save_image("rendered_scene.png", width=1600, height=1000)
viewer = Viewer.render(scene, width=800, height=500)

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

In a notebook, use a static PNG display when you do not need interaction:

scene.display(width=1200, height=800)
scene.display(width=1200, height=800, background="transparent")

For static exports, omit background to use the scene background, pass a color such as "#ffffff" or [255, 255, 255], or use "transparent" for a PNG with a transparent background.

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.19.tar.gz (122.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.2.19-cp39-abi3-win_amd64.whl (7.0 MB view details)

Uploaded CPython 3.9+Windows x86-64

cosmol_viewer-0.2.19-cp39-abi3-win32.whl (6.9 MB view details)

Uploaded CPython 3.9+Windows x86

cosmol_viewer-0.2.19-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.2 MB view details)

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

cosmol_viewer-0.2.19-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (8.6 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ i686

cosmol_viewer-0.2.19-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.8 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

cosmol_viewer-0.2.19-cp39-abi3-macosx_11_0_arm64.whl (6.5 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

cosmol_viewer-0.2.19-cp39-abi3-macosx_10_12_x86_64.whl (6.9 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for cosmol_viewer-0.2.19.tar.gz
Algorithm Hash digest
SHA256 79411624ef82e78a0fa99be2ba8e5eec9180a5d300de3ec680a8579b8aa72ac7
MD5 9cd53f8e3fef8678917c83eb812a7f01
BLAKE2b-256 d6e76b5e74235e5218d39990fba3e55507e3e2552912940b15b9ea3504b7eee8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.19-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 5bdc315feb64f3cf5995eaf3be6ca7000d8c7284abe7f83b896774b572ec8e2f
MD5 20182ac7f14e715f9e17d8a863f31061
BLAKE2b-256 b038573818f61ff8f4d38891d136d642dbc72cb0549965a4d8025b28de3f4dd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.19-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 6b87b1563256b069e9e27aa0c9b4bb449fc48e727d8124c2837a0f7e4e6dbcd3
MD5 a1d40207ca62eda91d63977f883c1b64
BLAKE2b-256 04214da6bc91dd63827d1fa5bdac9dd7665a75c1ce28130c92df2d46e789ccf3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.19-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f2075b6406b6048c891287dd173b6051236685151210df0e8f4cf48c38cdd1c6
MD5 b321b0f7bebdcf445139188439420020
BLAKE2b-256 d4b56987f58a1e8b3782b78d64875a9a5e803b76b31546fce8bfb19653c81274

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.19-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 890b0792b4c06e7c74f6f3cc8e99ab4071cd17d545d4d38d92fb73426366594c
MD5 591b04c460f531cf345705ff0fb26d71
BLAKE2b-256 f3c3af8d589f2cbfd2116d89bdb9e62083ede4356a9b3682a6bff4478de69131

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.19-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2478e1cee76569333989542b773aa45104f1798268c20dcf996d9c24c9fa34fb
MD5 79033d2c2cb66683a81f3b33ec362d0c
BLAKE2b-256 2e605e4809112afe3fa486f34a2d15d7f1291971ce0f7d3464b81edca2633809

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.19-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ed8f90e64e1219512c4fbb62272effdec143f6846e1a63795bffecea247c9794
MD5 34dbcb635113a652ed94c6659b1907d2
BLAKE2b-256 9ad75cd4c5a201b3b08601ec7bf2466f63d854f77bf511607d7513b5623b1131

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.19-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5b2cce416e6f8313ed613763010b1fe2c49406ca00e00f2586584b9a7439b2d6
MD5 61ac6db2002fa84aabc05c7b97e97abb
BLAKE2b-256 c74291e7b3be94a185c3c23df7f5613b660223f71be22d06222766dd164e580d

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