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

Uploaded CPython 3.9+Windows x86-64

cosmol_viewer-0.2.20-cp39-abi3-win32.whl (6.5 MB view details)

Uploaded CPython 3.9+Windows x86

cosmol_viewer-0.2.20-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.8 MB view details)

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

cosmol_viewer-0.2.20-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.20-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

cosmol_viewer-0.2.20-cp39-abi3-macosx_11_0_arm64.whl (6.2 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

cosmol_viewer-0.2.20-cp39-abi3-macosx_10_12_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for cosmol_viewer-0.2.20.tar.gz
Algorithm Hash digest
SHA256 f4273aeb34ed28ef3b36369b2a52db2bc52932db0708acded7eb5cbac4e9c198
MD5 aabc800360bd6e1df62e3d8d252e3d54
BLAKE2b-256 7fffc3debae0fa45a5c0eef8c7a98a54ec52ddc0bdd85e9be141b16c6b456d02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.20-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 bed049bbcd65fabd2c2c84e1a50ef2f6cf6486d69a94fecc82b77abecd15d031
MD5 ada3859d382b0bfb23e3324855c1a465
BLAKE2b-256 0f2b18174ed4aabdbfd513c80778b4a6497af3ca2f0e285bf5ecab846e186e61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.20-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 20348cb9ff85224ee54764380e904ca760f85517bba2e65357bc2056912e0541
MD5 b49df58f74d9711a57b63404c01e5291
BLAKE2b-256 a7fc27f4e98c148035660b81f74476bc52649592916f90bd3d748c99c302defc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.20-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fe1f53d2539a3a6c9c10e5420e7fa5c05737307f75a85b040ba2064a2a07e210
MD5 d08c0d7228728653b78814a6eec1a67f
BLAKE2b-256 fc843197c875fa21e99812ff3e37e65c20ba5ad2d1eb505e25d686509ac7a78b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.20-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 53e5414d3267e3b509e82daef829d55d63ff43deeac58459377ce1ae504a1e70
MD5 23034c5dfa90be9665d22fc3e7e260ab
BLAKE2b-256 b4915c3f36c281ab4755ab4c4acb77679f3777e3656b677a8c633f8e51fc004f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.20-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 82cdf65dcc78ed597e509285328fe2e6945d405f23b7a9d79db6f5167e162d31
MD5 ca7135cc28c5dfd2352813d72b061e86
BLAKE2b-256 4bdb042a5c40b371eb714142ebc06237def5dff9eda7b3217ccf03b677c47475

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.20-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 28559698a49e1e8cff1d7ddb713fab2fc422033d123ceac05652ed028bde9591
MD5 30e6ffec294d694fec9eeb73b291073a
BLAKE2b-256 985056d34fd9a70436d41d581d1901686c680b8c8ce3fa94835b2818d385d0a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cosmol_viewer-0.2.20-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 79893113750ce15bae9d1e3b24896b81e3b3527cd477ce118ff77f3ebfd64f7f
MD5 31996173c71dc28395903443971d3285
BLAKE2b-256 4c0f1ab5d1a6fe6dbbeefdfaf8c3840504a68a3dea5528351df37d2e9da84ccf

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