The Panel Chemistry NGLViewer
allows you to use the powerful JS NGL Viewer using Python 🐍 and HoloViz Panel ❤️.
When using NGL please cite
object
(str): The object to display. For example
extension
parameter to be set to the relevant extension.extension
(str): One of '', 'pdb', 'cif', 'csv', 'ent', 'gro', 'json', 'mcif', 'mmcif', 'mmtf', 'mol2', 'msgpack', 'netcdf', 'parm7', 'pqr', 'prmtop', 'psf', 'sd', 'sdf', 'top', 'txt', 'xml'. Default is '', i.e. the blob object is loaded as a url or non-url.
representation
(str): A display representation. One of 'axes', 'backbone', 'ball+stick', 'cartoon', 'helixorient', 'hyperball', 'label', 'licorice', 'line', 'point', 'ribbon', 'rocket', 'rope', 'spacefill', 'surface', 'trace', 'unitcell'. Default is 'ball+stick'.
color_scheme
(str): A predefined or 'custom' color scheme. One of 'atomindex', 'bfactor', 'chainid', 'chainindex', 'chainname', 'custom', 'densityfit', 'electrostatic', 'element', 'entityindex', 'entitytype', 'geoquality', 'hydrophobicity', 'modelindex', 'moleculetype', 'occupancy', 'random', 'residueindex', 'resname', 'sstruc', 'uniform', 'value', 'volume'. If 'custom' is specified you need to set the 'custom_color_scheme' parameter. Default color_scheme is 'element'.
custom_color_scheme
A custom color scheme. See http://nglviewer.org/ngl/api/manual/coloring.html#custom-coloring.
effect
(str): One of '', 'spin' or 'rock'. Default is '', i.e. no effect
For layout and styling related parameters see the Panel Customization Guide.
import panel as pn
from panel_chemistry.pane import NGLViewer # panel_chemistry needs to be imported before you run pn.extension()
from panel_chemistry.pane.ngl_viewer import EXTENSIONS
pn.extension("ngl_viewer", sizing_mode="stretch_width")
viewer = NGLViewer(object="1CRN", background="#F7F7F7", min_height=700, sizing_mode="stretch_both")
settings = pn.Param(
viewer,
parameters=["object","extension","representation","color_scheme","custom_color_scheme","effect",],
name="⚙️ Settings"
)
file_input = pn.widgets.FileInput(accept=','.join('.' + s for s in EXTENSIONS[1:]))
def filename_callback(target, event):
target.extension = event.new.split('.')[1]
def value_callback(target, event):
target.object = event.new.decode('utf-8')
file_input.link(viewer, callbacks={'value': value_callback, 'filename': filename_callback})
header = pn.widgets.StaticText(value='<b>{0}</b>'.format("💾 File Input"))
file_column = pn.layout.Column(header, file_input)
layout = pn.Param(
viewer,
parameters=["sizing_mode", "width", "height", "background"],
name="📐 Layout"
)
pn.Row(
viewer,
pn.WidgetBox(settings, layout, width=300, sizing_mode="fixed",),
)
accent="#D2386C"
pn.template.FastListTemplate(
site="Panel Chemistry", site_url="./",
title="NGLViewer",
sidebar=[file_column, settings, layout],
main=[viewer],
accent_base_color=accent, header_background=accent,
).servable();
Serve the app via panel serve NGLViewer.ipynb
and check it out at http://localhost:5006/NGLViewer.
If you add the flag --autoreload
you will get automatic reloading when ever you save the file.
You can also use the Panel Jupyter Preview to serve the app in a seperate window in Jupyter Lab.