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 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

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)

Static exports and native interactive viewers both bootstrap native GL on desktop. save_image / to_png first try the fast in-process offscreen path; on platforms with a headless GL path, this avoids creating a GUI event loop. If the in-process path cannot be created after a native viewer has already run, they automatically retry in an isolated Python subprocess. Set COSMOL_VIEWER_RENDER_ISOLATED=1 to force that isolated path.

For an interactive native window:

from cosmol_viewer import Viewer

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

print("Press Any Key to exit...", end='', flush=True)
_ = input()

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 an interactive notebook canvas, enable a transparent scene background before rendering:

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

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() use COSMolKit's protein reader, then the viewer core assigns 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.22.tar.gz (148.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.22-cp39-abi3-win_amd64.whl (7.5 MB view details)

Uploaded CPython 3.9+Windows x86-64

cosmol_viewer-0.2.22-cp39-abi3-win32.whl (7.4 MB view details)

Uploaded CPython 3.9+Windows x86

cosmol_viewer-0.2.22-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.8 MB view details)

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

cosmol_viewer-0.2.22-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (9.2 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ i686

cosmol_viewer-0.2.22-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

cosmol_viewer-0.2.22-cp39-abi3-macosx_11_0_arm64.whl (6.9 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

cosmol_viewer-0.2.22-cp39-abi3-macosx_10_12_x86_64.whl (7.4 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for cosmol_viewer-0.2.22.tar.gz
Algorithm Hash digest
SHA256 40bb8f5487e5a0c302135c820ceb1e14b597e6b9c3344ba13e95f6f1834ebd6c
MD5 f3e717e3ac19541b1030aae66429859d
BLAKE2b-256 7beaa6191dc2eee79cf454303902c729a6a5aa9d707d27d362ed98b0d3d9beeb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.22-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 41dfba7abc74b7c621314984430664a5e500aca0969a4f5b07ed0da363187b6d
MD5 51a4791746e0562cc45e4ee236253924
BLAKE2b-256 4cc9c5ee7ea9afd5d963bedc1d4b3c89c6f7c69d2564fd949957136dfa45b95d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.22-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 ba5273881af903e1f16a43243c7002f66c23074c2e815b62b1e77b392da14b91
MD5 5382f64192d2e136596c6514fdfddaaa
BLAKE2b-256 10e6dd05763f729fc7791bdef7bea12ffabf68d8f28804252c3e9a3224a1ddce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.22-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b59c14bb1b39e65c0c02459ac8bf8514fd66e247c3b363b2122e5761ec5e0b94
MD5 91f17ce9d1576b9755f4b98ba33d96b4
BLAKE2b-256 89c036131413f1f23dca3cf63e14031b1a6bc649801401690194a2a96adfd204

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.22-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 1205996171553c9787022ab618eeb1a7d80346d3d8cca19326798a72ac16d7ef
MD5 59811425a44f3fb26fba494aa8c41221
BLAKE2b-256 2ba94c58fe03bbce56fc759ffa9d122d8adf99fea745d0e14e410da51d2ca796

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.22-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 da8bc66628d2a0afa46356e6e28eae06d866eaa08a895c6e57e6c5907bd59a8e
MD5 2d1bce11950c3122bdef900f13926b37
BLAKE2b-256 baccc950c7feaead96f383124c76bc90982f47a3a8bbe353c11c735a615d1883

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.22-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e12bb56f864d1eb484d0c7f30ee075ee6c9764b2c20d69bf165be2646c573f1d
MD5 21ddf9eb34991e88e37f02482fa60bf0
BLAKE2b-256 ff599d1aa81ab1d9bc94d16093f458cf217e9b8d3941664ee55241f230fcc2df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.22-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 29fd834af2cd4837b9f499a36760249adcfbb6c03da0c0c0470190e80255a9aa
MD5 3de1e3c62cf84ee5ae40963341382c54
BLAKE2b-256 21cc6fe72ef14e46dc0fd6f749a69a0b93c0dba6b9bf8e14ec0cac4c0399180e

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