moleculekit.viewer.backends module#
Registry of viewer backends.
A backend is anything that can show a Molecule: an application embedding
moleculekit, a notebook widget, a browser viewer driven from Pyodide.
Registering one makes its name usable as mol.view(viewer=...), and lets a
viewer that is already on screen follow the changes made to mol.reps
afterwards, rather than having to rebuild its scene or replace moleculekit’s
own methods to notice them.
A backend is duck-typed. Only view is required:
view(mol, name=None): show the molecule. Called bymoleculekit.molecule.Molecule.view().representation_added(mol, index, params): a representation was appended.representation_updated(mol, index, params): one was changed in place.representation_removed(mol, index): one was removed, or all of them whenindexis None.
params is the same translated description the Mol* scene is built from
(type, color, opacity, size_factor, label_fields, sel
and the rest), or None when the selection matched no atoms. A backend that
leaves a method out simply does not hear about that kind of change.
Examples
>>> from moleculekit.viewer.backends import register_viewer
>>> class Printer:
... def view(self, mol, name=None):
... print(f"showing {mol.numAtoms} atoms")
... def representation_added(self, mol, index, params):
... print(f"rep {index} is a {params['type']}")
>>> register_viewer("printer", Printer())
>>> mol.reps.add("protein", "NewCartoon")
rep 0 is a cartoon
>>> mol.view(viewer="printer")
showing 1701 atoms
- moleculekit.viewer.backends.default_viewer()#
The registered backend to use when nothing else chose one.
A live viewer being registered is a strong signal that it is where a molecule should go, so a single registered backend becomes the default. Several are ambiguous, and then the usual resolution order applies.
- moleculekit.viewer.backends.get_viewer(name)#
Look a backend up by name.
- moleculekit.viewer.backends.notify(event, mol, index, params)#
Tell every backend that cares about a change to
mol.reps.- Parameters:
event (
str) –added,updatedorremoved.mol (
moleculekit.molecule.Molecule) – The molecule whose representations changed. A backend showing several molecules uses this to tell which one it was, and ignores molecules it is not showing.index (
intorNone) – Which representation, or None when all of them were removed.params (
callable) – Returns the translated representation, called only if a backend is listening, since translating resolves the selection against the molecule and a scene being built has no need of it.
- moleculekit.viewer.backends.register_viewer(name, backend)#
Register a viewer backend under a name.
- Parameters:
- Raises:
ValueError – If the backend has no
viewmethod, or the name is one of the built-in viewers, which are not replaceable this way.