The Panel Chemistry py3DMol
allows you to use the powerful py3Dmol viewer using Python 🐍 and HoloViz Panel ❤️.
When using py3DMol please cite
object
(py3Dmol.view): A py3Dmol.view
object to displaylayout
(HTML): A Panel HTML pane containing the html
and javascript
needed to render the object
.After instantiation of the object you can provide layout options (height, width, sizing_mode etc) to the layout
. See the Panel Customization Guide.
import py3Dmol
import panel as pn
from panel_chemistry.pane import Py3DMol
pn.extension()
p = py3Dmol.view(query="mmtf:1ycr")
p.setStyle({"cartoon": {"color": "spectrum"}})
pviewer=Py3DMol(p, height=400, sizing_mode="stretch_width", name="Basic")
pviewer
gridviewer = py3Dmol.view(query='pdb:1dc9',viewergrid=(2,2),style=[[{'stick':{}},{'cartoon':{'arrows':True, 'tubes':True, 'style':'oval', 'color':'white'}}],
[{'stick':{'colorscheme':'greenCarbon'}},{'cartoon':{'color':'spectrum'}}]])
gridviewer = Py3DMol(gridviewer, height=400, sizing_mode="stretch_width", name="Grid")
gridviewer
xyz = '''4
* (null), Energy -1000.0000000
N 0.000005 0.019779 -0.000003 -0.157114 0.000052 -0.012746
H 0.931955 -0.364989 0.000003 1.507100 -0.601158 -0.004108
H -0.465975 -0.364992 0.807088 0.283368 0.257996 -0.583024
H -0.465979 -0.364991 -0.807088 0.392764 0.342436 0.764260
'''
xyzview = py3Dmol.view()
xyzview.addModel(xyz,'xyz',{'vibrate': {'frames':10,'amplitude':1}})
xyzview.setStyle({'stick':{}})
xyzview.setBackgroundColor('0xeeeeee')
xyzview.animate({'loop': 'backAndForth'})
xyzview.zoomTo()
xyzviewer = Py3DMol(xyzview, height=400, sizing_mode="stretch_width", name="Interactive")
xyzviewer
You can change the object
of Py3DMol
xyzviewer.object = p
... and change it back again
xyzviewer.object = xyzview
we can change the background color
def set_background(color='0xeeeeee'):
xyzview.setBackgroundColor(color)
xyzviewer.param.trigger("object")
set_background("#e6f6ff")
... and use a ColorPicker
widget to speed up your workflow
background = pn.widgets.ColorPicker(value="#e6f6ff", name="Background")
pn.bind(set_background, color=background, watch=True)
background
We can also change the style
def set_style(style="stick"):
xyzview.setStyle({style: {}})
xyzview.zoomTo()
xyzviewer.param.trigger("object")
set_style("sphere")
... and use a RadioButtonGroup
to speed up your workflow
style=pn.widgets.RadioButtonGroup(value="sphere", options=["line", "cross", "stick", "sphere"], name="Style", button_type="success")
set_style=pn.bind(set_style, style=style, watch=True)
style
accent = "#0072B5"
pn.template.FastListTemplate(
site="Panel Chemistry", site_url="./",
title="Py3DMol Pane",
sidebar=[background, style],
main=[pn.Tabs(xyzviewer, gridviewer, pviewer)],
header_background=accent, accent_base_color=accent
).servable();
Serve the app via panel serve Py3DMol.ipynb
and check it out at http://localhost:5006/Py3DMol.
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.