Skip to content

Commit

Permalink
Issue #678
Browse files Browse the repository at this point in the history
write_html in py3Dmol for turning viewer into webpage
  • Loading branch information
dkoes committed May 13, 2023
1 parent fa5e8cb commit 0153c32
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion py3Dmol/py3Dmol/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time
import json
import sys
import re

try:
import IPython.display
Expand Down Expand Up @@ -71,7 +72,7 @@ def __init__(self,query='',width=640,height=480,viewergrid=None,data=None,style=
width = '%dpx'%width
if type(height) == int:
height = '%dpx'%height
self.startjs = '''<div id="%s" style="position: relative; width: %s; height: %s">
self.startjs = '''<div id="%s" style="position: relative; width: %s; height: %s;">
<p id="%s" style="background-color:#ffcccc;color:black">You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension: <br>
<tt>jupyter labextension install jupyterlab_3dmol</tt></p>
</div>\n''' % (divid,width,height,warnid)
Expand Down Expand Up @@ -208,6 +209,31 @@ def update(self):
self.updatejs = ''
return IPython.display.publish_display_data({'application/3dmoljs_load.v0':script, 'text/html': script},metadata={})


def write_html(self, f=None, fullpage=False):
'''Write html to reproduce viewer in a web page to a file.
f -- file name (str) or writeable file object; if unspecified html string is returned
fullpage -- instead of specified width/height make viewer fill the web page
'''

if f == None:
return self._make_html()

if type(f) == str:
f = open(f,'wt')
html = self._make_html()
html = f'''<html>
<body style="margin: 0; padding: 0; display: block;">
{html}
</body>
</html>'''
if fullpage:
html = re.sub(r'width: (\S+);', 'width: 100%;', html)
html = re.sub(r'height: (\S+);', 'height: 100vh;', html)

f.write(html)


@using_ipython
def png(self):
'''output png image of viewer, which must already be instantiated'''
Expand Down

0 comments on commit 0153c32

Please sign in to comment.