A simple IPython/Jupyter widget to embed an interactive 3Dmol.js viewer in a notebook.
The widget is completely static, which means the viewer doesn't need a running IPython kernel to be useful and web pages and presentations generated from the notebook will work as expected. However, this also means there is only one-way communication between the notebook and the viewer.
From PyPI:
pip install py3Dmol
The returned view object has the exact same API as 3Dmol.GLViewer with the exception that functions return None.
import py3Dmol
p = py3Dmol.view(query='mmtf:1ycr')
p.setStyle({'cartoon': {'color':'spectrum'}})
p
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
<py3Dmol.view at 0x7f53b44fd3d0>
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(width=400,height=400)
xyzview.addModel(xyz,'xyz',{'vibrate': {'frames':10,'amplitude':1}})
xyzview.setStyle({'stick':{}})
xyzview.setBackgroundColor('0xeeeeee')
xyzview.animate({'loop': 'backAndForth'})
xyzview.zoomTo()
xyzview.show()
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
Display local file.
benz='''
RDKit 3D
6 6 0 0 0 0 0 0 0 0999 V2000
-0.9517 0.7811 -0.6622 C 0 0 0 0 0 0 0 0 0 0 0 0
0.2847 1.3329 -0.3121 C 0 0 0 0 0 0 0 0 0 0 0 0
1.2365 0.5518 0.3512 C 0 0 0 0 0 0 0 0 0 0 0 0
0.9517 -0.7811 0.6644 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.2847 -1.3329 0.3144 C 0 0 0 0 0 0 0 0 0 0 0 0
-1.2365 -0.5518 -0.3489 C 0 0 0 0 0 0 0 0 0 0 0 0
1 2 2 0
2 3 1 0
3 4 2 0
4 5 1 0
5 6 2 0
6 1 1 0
M END
$$$$'''
view = py3Dmol.view(data=benz,style={'stick':{'colorscheme':'cyanCarbon'}})
view.show()
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
You can create a single canvas object with multiple viewers arrayed in a grid (3Dmol.createViewerGrid).
view = py3Dmol.view(query='pdb:1dc9',linked=False,viewergrid=(2,2))
view.setViewStyle({'style':'outline','color':'black','width':0.1})
view.setStyle({'cartoon':{'arrows':True, 'tubes':True, 'style':'oval', 'color':'white'}},viewer=(0,1))
view.setStyle({'stick':{'colorscheme':'greenCarbon'}},viewer=(1,0))
view.setStyle({'cartoon':{'color':'spectrum'}},viewer=(1,1))
view.removeAllModels(viewer=(0,0))
view.addModel(benz,'sdf',viewer=(0,0))
view.setStyle({'stick':{}},viewer=(0,0))
view.zoomTo(viewer=(0,0))
view.render()
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
<py3Dmol.view at 0x7f53b4515580>
view = py3Dmol.view(query='pdb:1ycr')
chA = {'chain':'A'}
chB = {'chain':'B'}
view.setStyle(chA,{'cartoon': {'color':'spectrum'}})
view.addSurface(py3Dmol.VDW,{'opacity':0.7,'color':'white'}, chA)
view.setStyle(chB,{'stick':{}})
view.show()
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
view = py3Dmol.view(query='pdb:5ire',options={'doAssembly':True})
view.setStyle({'cartoon':{'color':'spectrum'}})
view.show()
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
Color by temperature factors
view = py3Dmol.view(query='pdb:1ycr')
view.setStyle({'cartoon': {'color':'white'}})
view.addSurface(py3Dmol.VDW,{'opacity':0.7,'colorscheme':{'prop':'b','gradient':'sinebow','min':0,'max':70}})
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
<py3Dmol.view at 0x7f53b4515f70>
Generate an inline image of what is currently in the viewer (all white if the structure hasn't loaded yet).
png = view.png()
png
import requests, base64
r = requests.get('https://mmtf.rcsb.org/v1.0/full/5lgo')
view = py3Dmol.view()
view.addModel(base64.b64encode(r.content).decode(),'mmtf')
view.addUnitCell()
view.zoomTo()
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
<py3Dmol.view at 0x7f53b44fdf70>
Specifying individual styles for a viewer grid in the constructor
view = 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'}}]])
view.show()
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
v = py3Dmol.view(query='pdb:3erk',style={'cartoon':{'colorscheme':'ssPyMol'},'stick':{'radius':0.05}})
v.setStyle({'resn':'SB4'},{'stick':{}})
v.zoomTo({'resn':'SB4'})
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
<py3Dmol.view at 0x7f53b44fda90>
v = py3Dmol.view(query="pdb:1ubq",style={'cartoon':{},'stick':{}})
v.setHoverable({},True,'''function(atom,viewer,event,container) {
if(!atom.label) {
atom.label = viewer.addLabel(atom.resn+":"+atom.atom,{position: atom, backgroundColor: 'mintcream', fontColor:'black'});
}}''',
'''function(atom,viewer) {
if(atom.label) {
viewer.removeLabel(atom.label);
delete atom.label;
}
}''')
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
<py3Dmol.view at 0x7f53a5f57190>