simple workaround to display 3d structures in a notebook

3dmol

Easily displaying pdb structures using 3dmol without py3dmol

3dmol.js is a lightweight javascript library for displaying 3d molecules. py3Dmol is a great tool for embedding an interactive 3Dmol.js viewer in a Jupyter notebook.

If for some reason one cannot install py3dmol, it is also possible to display 3d structures by instantiating a viewer for 3dmol.js using IPython's display module, or by using jupyter's HTML cell magic.

In [11]:
from IPython.display import display, HTML
In [19]:
def disp3d(pdb):
    js="<script src='https://3Dmol.org/build/3Dmol-min.js' async></script> <div style='height: 400px; width: 400px; position: relative;' class='viewer_3Dmoljs' data-pdb='{}' data-backgroundcolor='0xffffff' data-style='stick'></div>".format(pdb)
    display(HTML(js))
In [22]:
disp3d('7AML')
In [21]:
disp3d('7BH1')

Jupyter's HTML cell magic does exactly the same thing.

In [23]:
%%HTML
<script src="https://3Dmol.org/build/3Dmol-min.js" async></script>     
         <div style="height: 400px; width: 400px; position: relative;" class='viewer_3Dmoljs' data-pdb='2POR' data-backgroundcolor='0xffffff' data-style='stick'></div>       

Leave a comment

Your email address will not be published. Required fields are marked *