Skip to main content

MolGFX

Documentation

MolGFX is a semantic molecular-rendering library for Python, Rust, and WebGPU. It renders structures supplied by MolFrame as interactive browser scenes or deterministic native images. MolGFX does not parse, fetch, dock, simulate, or open an application window.

MolGFX haemoglobin cartoon, heme, and local pocket

Install

macOS builds include native Metal and Vulkan portability. For repeatable headless images on virtual machines, install a software Vulkan adapter and select it with WGPU_BACKEND=vulkan and VK_DRIVER_FILES. Byte-exact rendering is checked on an explicit reference adapter; Apple's hosted paravirtual Metal device produces different pixels for repeated static exposures.

python -m pip install --upgrade molgfx

Render a structure

import molframe
import molgfx

# Infer bonds so atom-and-bond representations have sticks to draw.
structure = molframe.read("structure.cif").infer_bonds()
scene = molgfx.Scene(structure)
scene.add(molgfx.rep.cartoon(target=molgfx.sel.protein(), color=molgfx.color.chain()))
scene.add(molgfx.rep.ball_and_stick(target=molgfx.sel.ligands()))

molgfx.Renderer().render_image(scene, size=(1920, 1080)).save("structure.png")

Cartoon representations accept aspect_ratio (width divided by depth, default 5.0) and arrow_factor (terminal beta-arrow shoulder scale, default 1.5). Set arrow_factor=0.0 to disable arrows. The Rust builder and browser scene contract expose the same controls. helix_profile and nucleic_profile select "elliptical", "rounded" (planar faces with semicircular ends), or "square" cross-sections (helix default: elliptical; nucleic default: square). Rust uses molgfx::rep::CartoonProfile. Profiles change the cross-section while preserving source guide coordinates and picking anchors.

Set direction_wedges=True on molgfx.rep.cartoon(...) to draw a flat N-to-C direction triangle at each selected polymer guide (default: False). The triangle's width and length are half the cartoon width. It follows live source coordinates and picks the guide atom. A selection containing one guide uses the direction of its source polymer; an isolated residue requires N/CA/C (or C5'/C4'/C3' for nucleic acids). Missing direction atoms produce a render error. Glycan cartoon mode rejects this control. Rust uses .direction_wedges(true); commands use direction_wedges=true.

Use gaps="dashed" on cartoon, backbone, trace, tube or putty to connect missing polymer intervals with closed tubes of radius 0.15 Å and 0.5 Å dashes separated by 0.5 Å. The default is "hidden". Chain boundaries and excluded residues remain disconnected. Connectors follow live coordinates and retain source atom identity for picking. Rust uses .gaps(molgfx::rep::GapStyle::Dashed); commands use gaps=dashed.

The browser viewer highlights picked atoms and bond endpoints on hover without changing the authored scene or selection. Rust hosts can use Scene::set_pointer_hover; leaving the molecule restores the authored hover channel. Repeated picks of the same entity preserve the settled exposure.

For stereo, Rust uses molgfx::camera::stereo_eyes and Renderer::render_stereo_images. StereoImages::compose supports parallel and crossed side-by-side views, HD compression, checkerboard, rows, columns and red/cyan, red/blue or red/green anaglyphs. Eye cameras remain parallel and converge through an off-axis projection. Python uses Camera.stereo_eyes, and WASM uses Camera.stereoEyes. The stereo_image Rust example saves eye images and display layouts; the embedding host owns window-specific stereo presentation.

Presentation effects are explicit values on a render profile:

import molgfx

profile = molgfx.profile.converged().with_effect(molgfx.effect.bloom(intensity=0.8))
renderer = molgfx.Renderer(profile=profile)

molgfx.effect also provides depth cue, depth of field, motion blur, backdrop, lighting, shape cues, display and anti-aliasing constructors. Invalid settings raise an error before rendering. without_effect(kind) restores the quality recipe's default for that kind. Rust exposes the same values under molgfx::profile; WASM Renderer.create accepts an optional effects JSON array of { "kind": ..., "settings": ... } records validated by the shared core.

For native HDR export, retain scene-linear highlights in half-float OpenEXR:

hdr = molgfx.Renderer(profile=molgfx.profile.converged()).render_hdr_image(scene, size=(1920, 1080))
hdr.save("structure.exr")

HdrImage exposes width, height, exr_bytes(), rgba16f() and quality_json(). HDR capture completes the profile's exposure without baking in bloom, exposure, tone mapping, display conversion or screen overlays. Python .save() accepts only .exr filenames and raises ValueError before writing any other format. Rust uses HdrImage::save_exr and also supports an explicit camera through Renderer::render_hdr_image_with_camera.

A structure is optional for a scalar-volume scene. Bind voxel values separately from the portable scene specification; the same grid can drive several presentations without another upload:

import hashlib
import struct
import molgfx

values = [
    max(0.0, 1.0 - ((x - 3.5) ** 2 + (y - 3.5) ** 2 + (z - 3.5) ** 2) ** 0.5 / 4.0)
    for z in range(8)
    for y in range(8)
    for x in range(8)
]
source = molgfx.data.source(hashlib.sha256(struct.pack("<512f", *values)).hexdigest())
scene = molgfx.Scene()
volume = molgfx.density.volume(source=source, dimensions=(8, 8, 8))
volume = volume.isosurface(0.5, color=(220, 40, 70)).direct(
    [
        (0.0, (50, 150, 255), 0.0),
        (1.0, (50, 150, 255), 0.4),
    ]
)
identity = scene.add(volume)
scene.bind_volume(identity, values)
molgfx.Renderer().render_image(scene, size=(640, 480)).save("density.png")

values are indexed with x fastest, then y, then z. For a map read by MolFrame, supply its column-major voxel_to_world affine instead of assuming unit voxels at the origin. Region bounds use half-open voxel indices.

Targets are MolFrame queries, and a small command language works around them through molgfx.Session:

show cartoon, protein
select pocket, byres (within 5 of resname HEM) and protein
show licorice, $pocket
color orange, $pocket

Named snapshots capture the complete authored scene (camera, styles, interaction state, assemblies and overlays), not coordinates, pixels or renderer buffers:

import molframe
import molgfx

session = molgfx.Session(molframe.read("structure.cif").infer_bonds())
session.snapshot_save("overview")
session.execute("color orange, protein")
session.snapshot_restore("overview")
session.undo()  # Return to the state immediately before restoration.
session.snapshot_remove("overview")

The same operations are commands: snapshot save|restore|remove NAME. Named captures round-trip in Session.to_json() and Session.from_json(scene, json). Restoration requires local bindings with matching content identities; missing or mismatched assets leave the live scene unchanged. Revisions advance on restore and undo rather than returning to the captured revision. Rust uses the canonical snapshot::SceneSnapshot, Scene::snapshot, Scene::restore_snapshot and PatchOperation::RestoreSnapshot. Frame export remains render_sequence or render_camera_path; no retained movie request is an export implementation.

The Rust engine opens no window and owns no application panels. This repository also supplies the official browser host: a canvas, navigation and semantic picks, not a Workbench, console, toolbar or file picker. Session remains the sole command and undo/redo authority. Python notebooks use this same frontend through AnyWidget, a normal package dependency.

import molframe
import molgfx

session = molgfx.Session(molframe.read("structure.cif").infer_bonds())
session.execute("show cartoon, protein")
viewer = molgfx.Viewer(session)
viewer  # Display in Jupyter.

Embedding in a host application

An application that already owns a wgpu device (a desktop shell, a compositor) can render without any CPU copy. Enable the host feature of molgfx, create the device from molgfx::host::host_device_descriptor(&adapter) (or merge host_requirements() into your own), then:

  • Renderer::from_host(profile, HostContext { adapter, device, queue }, HostOutputFormat::Rgba8) refuses a device that lacks a feature or limit and lists each one.
  • renderer.host_target(&texture) checks a texture you own (2-D, one layer and mip, not multisampled, RENDER_ATTACHMENT; an sRGB texture must list its linear sibling in view_formats). renderer.create_host_target(w, h) makes one the renderer owns (render attachment, texture binding, copy source).
  • render_to_host (realtime) and render_to_host_converged record one submission on your queue and return a HostFrame { report, fence }. Work you submit later on the same queue is ordered after it; wait on the fence only to read results on the CPU. Resize by making a new target. Several targets may share one renderer, but they share its temporal history, so alternating sizes restarts accumulation.

See crates/molgfx/examples/host_embed.rs. The renderer and host must use the same wgpu version; molgfx::host::wgpu re-exports the one in use.

Browser SDK

npm install molgfx
import { Viewer } from "molgfx";
import "molgfx/viewer.css";

const viewer = await Viewer.create(document.getElementById("scene")!);
await viewer.load(await fetch("structure.cif").then(r => r.arrayBuffer()), { name: "structure.cif" });
viewer.execute("show cartoon, protein");
// On host teardown:
await viewer.dispose();

The one npm package exports molgfx, molgfx/viewer, molgfx/anywidget and molgfx/react. The React adapter owns viewer mount/dispose, not molecular state. Public TypeScript declarations never expose the raw wasm ABI.

"use client";
import { MolGFXViewer } from "molgfx/react";
import "molgfx/viewer.css";

export function Protein({ bytes }: { bytes: Uint8Array }) {
  return <MolGFXViewer structure={bytes} name="protein.cif"
    style={{ width: "100%", height: 480 }}
    onError={error => console.error(error.message)} />;
}

Keep the input byte object stable between unchanged React renders. The adapter reuses its Viewer for structure updates and disposes it on unmount; it does not duplicate Scene, Session or rendering semantics.

Shared frontend build

nix develop -c npm ci --prefix web
nix develop -c npm run build --prefix web
nix develop -c npm test --prefix web
nix develop -c npm pack ./web --pack-destination web

web/scripts/build.mjs runs wasm-pack into ignored web/generated, then emits the official package into ignored web/dist and copies identical artifacts to Python static assets. The documentation site imports the public npm package; Next.js emits its WASM asset rather than loading a private runtime directory. A content-hash manifest and archive-level checks prove that npm tarballs and Python wheels carry identical runtime bytes. Build before running maturin from a source checkout; PyPI source distributions include the prebuilt runtime and need Python >=3.12, Rust and dependency fetching, but no Node, wasm-pack or adjacent MolFrame checkout. Cargo, Python and npm versions must match. Native wheel builds repair external libraries; release gates rebuild the source archive and exercise fresh installed wheels before publication.

Python and npm have separate release workflows. Published versions are immutable; release 0.4.1 supersedes the existing PyPI 0.4.0 package. npm trusted publishing requires the package owner to authorize this repository and release workflow. Until the first registry publication, the docs use the public package through a local npm dependency, exercising the same exports and bundler asset resolution.

The optional Mol* adapter runs the requested local Mol* source checkout in real Chromium/WebGL2. Install its locked Node transport dependencies, then provide a real Chromium executable and PyMOL binary:

nix develop -c npm ci --prefix crates/molgfx-bench/parity
MOLSTAR_CHROMIUM=/path/to/chrome nix develop -c cargo run -p molgfx-bench --bin parity -- \
  --manifest crates/molgfx-bench/parity/gallery.json --cache ~/.cache/molgfx-corpus \
  --output target/gallery --case P2-iso-solid-skewMRC \
  --recipe molgfx-converged --recipe pymol-ray --recipe molstar-imagepass \
  --molstar-root ../molstar --pymol /path/to/pymol

The manifest records explicit omissions for recipes that cannot faithfully represent a case; failures are never counted as omissions. Review visual semantics before blessing golden images. Motion-blur orbit cases also require --ffmpeg /path/to/ffmpeg.

Writing queries and commands walks through both, and the MolFrame query reference lists every keyword.

Status

Beta. Public APIs are available through the molgfx facade; MolFrame provides structure input and molecular selection semantics.

License

MIT

Metadata

Release files for molgfx 0.5.8

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

Source distribution (sdist)

Source distribution for molgfx 0.5.8
File Size Uploaded Provenance
molgfx-0.5.8.tar.gz 4.9 MB With provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for molgfx 0.5.8
File
molgfx-0.5.8-cp312-abi3-win_amd64.whl CPython 3.12 abi3 Windows x86-64 Details
molgfx-0.5.8-cp312-abi3-manylinux_2_28_x86_64.whl CPython 3.12 abi3 Linux glibc 2.28+ x86-64 Details
molgfx-0.5.8-cp312-abi3-manylinux_2_28_aarch64.whl CPython 3.12 abi3 Linux glibc 2.28+ ARM64 Details
molgfx-0.5.8-cp312-abi3-macosx_11_0_arm64.whl CPython 3.12 abi3 macOS 11.0+ ARM64 Details
molgfx-0.5.8-cp312-abi3-macosx_10_12_x86_64.whl CPython 3.12 abi3 macOS 10.12+ x86-64 Details

Total release size: 94.5 MB

Release files / molgfx-0.5.8.tar.gz

Download URL molgfx-0.5.8.tar.gz
Size 4.9 MB
Tags Source
SHA-256 checksum
How to use checksums
add42c1f8194baca7bba516db7871d1e058040c473a880f492829528dca44eca
BLAKE2b-256 checksum
How to use checksums
d7ee5ab8571ec155dd095a5519d3dfd519dfdb596de1c9d7c666a879f2200588
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Release files / molgfx-0.5.8-cp312-abi3-win_amd64.whl

Download URL molgfx-0.5.8-cp312-abi3-win_amd64.whl
Size 25.7 MB
Tags CPython 3.12 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
e9eafce193dce14ee265b79df153a3b4f2c08fe368e4ed4766fbbd7684bc544e
BLAKE2b-256 checksum
How to use checksums
0312ccae0b2deaa9ab444be28f1721696eb91d006e680b608d3a8527292a9a90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Release files / molgfx-0.5.8-cp312-abi3-manylinux_2_28_x86_64.whl

Download URL molgfx-0.5.8-cp312-abi3-manylinux_2_28_x86_64.whl
Size 16.5 MB
Tags CPython 3.12 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
62a5fa50afd1ea28be5ac156001c654a6ae23042feea96d7dfc56b715b29c8fa
BLAKE2b-256 checksum
How to use checksums
78b31bfe377cfe7fc52a7f8120939614d3894b5f2fbb4142206ed0ecfc28cb53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Release files / molgfx-0.5.8-cp312-abi3-manylinux_2_28_aarch64.whl

Download URL molgfx-0.5.8-cp312-abi3-manylinux_2_28_aarch64.whl
Size 15.8 MB
Tags CPython 3.12 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
2992b65178d27bc04119a560693f0f4fbc6230d75ac0b785a59da15e31b7464b
BLAKE2b-256 checksum
How to use checksums
96f9ec54e91fa51fe12954e2705f4c2e81b5f9cb5ecceb3781945b7f8815f994
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Release files / molgfx-0.5.8-cp312-abi3-macosx_11_0_arm64.whl

Download URL molgfx-0.5.8-cp312-abi3-macosx_11_0_arm64.whl
Size 15.4 MB
Tags CPython 3.12 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
208c7d76b93621030b0713a032b073551c1f700a6a82f6d36edf1a7a2793e55e
BLAKE2b-256 checksum
How to use checksums
93f43e1a17cc6cc5dbcf5e4218b6169dbf573063c0b25a5d06a7c7415dedf4c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Release files / molgfx-0.5.8-cp312-abi3-macosx_10_12_x86_64.whl

Download URL molgfx-0.5.8-cp312-abi3-macosx_10_12_x86_64.whl
Size 16.3 MB
Tags CPython 3.12 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
7557549e264e0e1ed7822759a8150ba8aca24fdf257963ad5c56acc2eabf5d73
BLAKE2b-256 checksum
How to use checksums
606ed8bb6dcc86ef7313cf31006b7b7f3e942656573a3480818c6074039ccb20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Security for molgfx 0.5.8

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.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for molgfx-0.5.8.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

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.

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