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. Until the offscreen backend is fully winit-free, use save_image / to_png / display as a static workflow, or use Viewer.render as an interactive workflow; do not call static export immediately before Viewer.render in the same native process.

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.21.tar.gz (117.8 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.21-cp39-abi3-win_amd64.whl (6.7 MB view details)

Uploaded CPython 3.9+Windows x86-64

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

Uploaded CPython 3.9+Windows x86

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

Uploaded CPython 3.9+macOS 11.0+ ARM64

cosmol_viewer-0.2.21-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.21.tar.gz.

File metadata

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

File hashes

Hashes for cosmol_viewer-0.2.21.tar.gz
Algorithm Hash digest
SHA256 cfa466f46983b31a88efaebf774d3d95e5da75b784308a5c5c6e24484d4d0c1d
MD5 9e8afac470a5a6a16262c3c492596e1c
BLAKE2b-256 78906ad3b1bf97c30cc0752d1b0b0ff1348881d22d6456f90e1c8e34ac287c96

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.21-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 dcea9919969b203325a7ad54d2dbe3b19ad1cb7b1212962ce0df594e7b403c22
MD5 46b0d9865b2a2d8d2e5426bd64e180d2
BLAKE2b-256 bb58120ec8d7a18eab07082124d869ba1aab361fcc3b75b758b82ea272aae890

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.21-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 782d8f2cfcc73da779cdda94b3580905288a83129e771d4a5effa034bd6c312e
MD5 d9e83d777bb1c98a3ca64b62bee61d3d
BLAKE2b-256 cef191ff1ad598ad1862c317b04bfa0668fe86a9e1537d8c410d7e26cb471008

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.21-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 609f733fe589210fd0827a31caa4d14ec779e476cb63f1d9b15e5811fb247511
MD5 2605626640993f3fbe6ab85fb889ec6e
BLAKE2b-256 0bab80efbac4d2027904b6d1dc192920e8c9629003463336f671f95a7519418f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.21-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 b52ec672ebc92934765832d44476d0d6d221ed799139cff4ce7358de559e016d
MD5 5a29fad020569f4400a2451deb24b7e1
BLAKE2b-256 bda30f2a491e6c8a8b68c8a1516a8b51a5a8501d6bf3ca7be35283ea8aacf042

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.21-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4dddec818be9890b9c55854a32748d4653dc073ac7e26f91645ed3a97777bde9
MD5 a885462971d78624689cc2a84dfdb8cd
BLAKE2b-256 201ac8bd7400c6d5cee1e262fcbeed48fdcae4dbaa0f42ef9f65b370ca47ce3e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.21-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6473e0c29c554b6917f5212d23202ab14789d3e06a6e5a3f943d39f28ee72f5e
MD5 5d1e0bb4d2eaa2b0b700329ee9bc3a58
BLAKE2b-256 42b65b422e2716b5feec018543f8583ce02e6b49458276433c3065565625319e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.21-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 bbd7f71fd31e9fe6b7d054bc177c0085f8d55cc8943fb66f70b60c42dca8f9c3
MD5 43f5e31c54d51037ed3d89afcb2ac64f
BLAKE2b-256 671bf801e17fcc2b3b15b27772aa33cb0fe64b97ce7253415eb41b1f48d8f222

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