Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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.

Full installation, workflow, and Python API documentation is available at https://cosmol-studio.github.io/COSMol-viewer/.

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. Google Colab is detected automatically and uses an isolated software-rendering subprocess, so notebook code does not need to set either this variable or LIBGL_ALWAYS_SOFTWARE.

For an interactive native window:

from cosmol_viewer import Viewer

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

viewer.keep_alive()

keep_alive() waits for Enter, stdin EOF, or the native viewer process to exit, then closes the viewer. It is a finalization step: the viewer cannot be reused afterwards. Calling it in Jupyter or Colab raises RuntimeError.

Native viewers run in a dedicated executable bundled with the Python wheel. No register_render() call is needed in Python. For a source development build, see DEV.md.

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.

Metadata

Release files for cosmol-viewer 0.5.0rc2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cosmol-viewer 0.5.0rc2
File Size Uploaded Provenance Actions
cosmol_viewer-0.5.0rc2.tar.gz 2.3 MB Without provenance Details for cosmol_viewer-0.5.0rc2.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for cosmol-viewer 0.5.0rc2
File Actions
cosmol_viewer-0.5.0rc2-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for cosmol_viewer-0.5.0rc2-cp39-abi3-win_amd64.whl
cosmol_viewer-0.5.0rc2-cp39-abi3-win32.whl CPython 3.9 abi3 Windows x86-32 Details for cosmol_viewer-0.5.0rc2-cp39-abi3-win32.whl
cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details for cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-32 Details for cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
cosmol_viewer-0.5.0rc2-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for cosmol_viewer-0.5.0rc2-cp39-abi3-macosx_11_0_arm64.whl
cosmol_viewer-0.5.0rc2-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for cosmol_viewer-0.5.0rc2-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 79.9 MB

Release files / cosmol_viewer-0.5.0rc2.tar.gz

Download URL cosmol_viewer-0.5.0rc2.tar.gz
Size 2.3 MB
Tags Source
SHA-256 checksum
How to use checksums
3413298726206f2e56c814b582304f74575455db0a24d3ccc782da04166a6533
BLAKE2b-256 checksum
How to use checksums
6b6dadc5b7f2e32c3140c07e3c40c5ddf0cebe8653ef16e6f5803db318ad73ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / cosmol_viewer-0.5.0rc2-cp39-abi3-win_amd64.whl

Download URL cosmol_viewer-0.5.0rc2-cp39-abi3-win_amd64.whl
Size 11.4 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
4af8db1f0150e57d76c475ae942e82f92086f69a35f408eb727920b1bbd944b0
BLAKE2b-256 checksum
How to use checksums
5991b124d75e627281e975d3217f403c4093ef83f3f635bdd3e2af67f6507455
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / cosmol_viewer-0.5.0rc2-cp39-abi3-win32.whl

Download URL cosmol_viewer-0.5.0rc2-cp39-abi3-win32.whl
Size 11.1 MB
Tags CPython 3.9 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
0a2d813707392904d0b1f2c1e15ce81dce883b7a4c933b0906d4a6be3005b381
BLAKE2b-256 checksum
How to use checksums
decb854c4f5afa6b8e22459f3c38d0a75d84a8ec69e77dc114bf54c18a48a868
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 11.9 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
1efb2cbdce25ddc9fc474ac88412e9d255adb6f9b94a7853e10d67b1001df16b
BLAKE2b-256 checksum
How to use checksums
7c4cf0d8d55c024469e769a683e2aa9fb7d88d40188033505518c59081397f65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Size 12.5 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-32 abi3
SHA-256 checksum
How to use checksums
a1e4871b7b54c1b18940a41a61c1bae6ef4fc47ea54f5ac66bccbcde3a9caee4
BLAKE2b-256 checksum
How to use checksums
e2c919b2b0e05053b4cfd0647414d3b25741bb86f9877f631ba8e9dd5d4c894b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL cosmol_viewer-0.5.0rc2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 11.4 MB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
2740ff04c41db8ad2cd04b50280c2008ce6cfb5ebf415d5ec34a0757a8261170
BLAKE2b-256 checksum
How to use checksums
64659f739af9bbe7df24dcd0afffd643e7c3f41a75d6a64685f3816755bed0dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / cosmol_viewer-0.5.0rc2-cp39-abi3-macosx_11_0_arm64.whl

Download URL cosmol_viewer-0.5.0rc2-cp39-abi3-macosx_11_0_arm64.whl
Size 9.3 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
85312ea552fa7cfa438ab0cbdbbc7e781d4b2e835cad40e467629dfdc024eb81
BLAKE2b-256 checksum
How to use checksums
79961aa2bce26c604249faac49e6f54a23dc911497068add520a4079e3887332
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / cosmol_viewer-0.5.0rc2-cp39-abi3-macosx_10_12_x86_64.whl

Download URL cosmol_viewer-0.5.0rc2-cp39-abi3-macosx_10_12_x86_64.whl
Size 10.0 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
b801ec4abcc105d00e7deb5563fb71bc54115107ef9d2a3c336d78ff990d0703
BLAKE2b-256 checksum
How to use checksums
1a4678ab57026822153d043d28dcf2d8a4a426b2c59f4da6fd895f33292da3fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Security for cosmol-viewer 0.5.0rc2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page