Skip to main content

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)

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.

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.26.tar.gz (162.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.26-cp39-abi3-win_amd64.whl (7.6 MB view details)

Uploaded CPython 3.9+Windows x86-64

cosmol_viewer-0.2.26-cp39-abi3-win32.whl (7.5 MB view details)

Uploaded CPython 3.9+Windows x86

cosmol_viewer-0.2.26-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.9 MB view details)

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

cosmol_viewer-0.2.26-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (9.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ i686

cosmol_viewer-0.2.26-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.4 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

cosmol_viewer-0.2.26-cp39-abi3-macosx_11_0_arm64.whl (7.0 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

cosmol_viewer-0.2.26-cp39-abi3-macosx_10_12_x86_64.whl (7.5 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for cosmol_viewer-0.2.26.tar.gz
Algorithm Hash digest
SHA256 6378a99f58c589c7fa7cff8ac10e6e40c490b541184829ea00deecca3fa0b77c
MD5 673cafc6f5b738b364233a01afadb7c7
BLAKE2b-256 67583cef5d43905c793d977815ae3b86aba8ddda799a5d9af1a1ec248ae88ac4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.26-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c51ded49d1f14b7533c61595bccb2304a11ed56dd5ab5b1ea24ff61a65795b87
MD5 172c1825b8a8e3b00bd7a1fb7ec5a6bf
BLAKE2b-256 5b3ac851cca91912edc48faf9ecc8593a4686c93d71615b8077a602ba9373d35

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.26-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 fb85784bd5982dc4c6f5fde4ec25f4c17eaf895c25ecc645268b205a19c962c8
MD5 fd83767a885aeb5fd6ae2a8199832274
BLAKE2b-256 7fa466a407d670a8c54cc124ba94186ef0a41ef0cc5b93694e176cd5882a8f48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.26-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7d694cf35aac928d03f8dd2a944d0aea6d56b50cffa2e2ea65a74a3f58bd3c56
MD5 45b4b107ae6ce5b8cc2d3b9b047b0ee3
BLAKE2b-256 9ce34a78fb38a683886a9b6fdb1c3c8c18da96dfbf22ea174c860909801273ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.26-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 bbd145b2e8f907c7bb7be78b6e585a957f9a6ecf30eace4dd123129554c8a1a8
MD5 5901bfa8c4fb7299906866e6ffc6c4e5
BLAKE2b-256 34f00449a54404839d3c42fddf957c6bdc9a4d65f41a8742e624787e6c5664e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.26-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 87d649b24728723dcdcc697ec3b9fc15df9f55ba2a21d661939f859227aece43
MD5 5ec9d125d7233e02c6adabc96f60d0c2
BLAKE2b-256 3ae84a402f6f26189acc9b1e6aa4f5e0359dc6f0f9f86943587eecfa3d67eb6d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.26-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 47001c0036e747537b31d660a686687ec8e329e033063045b993a71d656153b1
MD5 8818f3c2951f5d076bfcb6835b8a28b8
BLAKE2b-256 3139ba7b8aef37bff6236518c9ac947c2650aa58145b2fdad69b9caf21f3a666

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.26-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9db4e8b4e2de4735b53c0652ca98fbf3fb6b6bc2337206ddcda6c65387b7d9ee
MD5 b41d1f057d521dcaf617d3ff5620baf6
BLAKE2b-256 4598fb7bfa463365b3a7ac6e40f9b5762393f5fd8173a00b71a642a0361518f7

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.0

8 files

This release

0.2.26 This release

8 files

0.2.25

8 files

0.2.24

8 files

0.2.23

8 files

0.2.22

8 files

0.2.21

8 files

0.2.20

8 files

0.2.19

8 files

0.2.18

8 files

0.2.17

8 files

0.2.16

8 files

0.2.14

8 files

0.2.13

8 files

0.2.12

10 files

0.2.11

10 files

0.2.10

10 files

0.2.9

10 files

0.2.8

10 files

0.2.7

10 files

0.2.6

10 files

0.2.5

10 files

0.2.4

10 files

0.2.3

10 files

0.2.2

10 files

0.2.1

10 files

0.2.0

10 files

0.1.7

10 files

0.1.6

10 files

0.1.5

10 files

0.1.4

9 files

0.1.3

6 files

0.1.2

7 files

0.1.1

7 files

0.1.0

107 files

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