def test_write_html(mock_unset): from nglview.color import ColormakerRegistry as cm from nglview.theme import ThemeManager import ipywidgets.embed as embed tm = ThemeManager() traj0 = pt.datafiles.load_trpcage() traj1 = pt.datafiles.load_tz2() view = nv.NGLWidget() view.add_trajectory(traj0) view.add_trajectory(traj1) view.gui_style = 'ngl' view._gui_theme = 'dark' display(view) fp = StringIO() with patch.object(embed, 'embed_snippet') as mock_embed: nv.write_html(fp, [view], frame_range=(0, 3)) mock_embed.assert_called_with([tm, cm, view]) mock_unset.assert_called_with() assert len(view._ngl_coordinate_resource[0]) == 3 assert len(view._ngl_coordinate_resource[1]) == 3 # box with patch.object(embed, 'embed_snippet') as mock_embed: nv.write_html(fp, [HBox([view])], frame_range=(0, 3))
def test_write_html(mock_unset): traj0 = pt.datafiles.load_trpcage() traj1 = pt.datafiles.load_tz2() view = nv.NGLWidget() view.add_trajectory(traj0) view.add_trajectory(traj1) view fp = StringIO() nv.write_html(fp, [view], frame_range=(0, 3)) mock_unset.assert_called_with() assert len(view._ngl_coordinate_resource[0]) == 3 assert len(view._ngl_coordinate_resource[1]) == 3
def render_html( view, *, html_file=None, orientation=None, ): """Render widget to HTML display. Parameters ---------- view : ngview.widget.NGLWidget The structure widget to render. html_file : None or str If you want to also save to permanent HTML file, provide name here. orientation : list Set to this camera orientation (list of 16 numbers), fixing this bug: https://github.com/dwhswenson/contact_map/pull/62#issuecomment-583788933 You can get the desired orientation by manually manipulating the widget in a Jupyter notebook and then calling `view._get_orientation`. Returns ------- IPython.display.DisplayHandle A handle that displays the HTML structure in a Jupyter notebook. """ with tempfile.TemporaryFile(mode='w+', suffix='.html') as f: nglview.write_html(f, view) f.flush() f.seek(0) html_text = f.read() if orientation: if len(orientation) != 16: raise ValueError('`orientation` must be list of 16 numbers') html_text = re.sub( r'"_camera_orientation":\s+\[[^\]]*\]', '"_camera_orientation": [' + ', '.join(map(str, orientation)) + ']', html_text) if html_file: if os.path.splitext(html_file)[1] != '.html': raise ValueError(f"`html_file` needs extension .html: {html_file}") with open(html_file, 'w') as f_html: f_html.write(html_text) return IPython.display.display(IPython.display.HTML(data=html_text))
def render_html( view, *, html_file=None, orientation=None, remove_widget_view=False, returnval='display', ): """Render widget to HTML. Parameters ---------- view : ngview.widget.NGLWidget The structure widget to render. html_file : None or str If you want to also save to permanent HTML file, provide name here. orientation : list Set to this camera orientation (list of 16 numbers), fixing this bug: https://github.com/dwhswenson/contact_map/pull/62#issuecomment-583788933 You can get the desired orientation by manually manipulating the widget in a Jupyter notebook and then calling `view._camera_orientation`. remove_widget_view : bool Remove the widget view lines, so the HTML just gives the widget state. Helpful if you want to embed widgets in HTML rendering without showing another time. returnval : {'display', 'HTML', 'none'} Return value (see Returns_). Returns ------- IPython.display.DisplayHandle or IPython.display.HTML or None. A handle for a Jupyter notebook, or `None` depending on value of `returnval`. """ with tempfile.TemporaryFile(mode='w+', suffix='.html') as f: nglview.write_html(f, view) f.flush() f.seek(0) html_text = f.read() if orientation: if len(orientation) != 16: raise ValueError('`orientation` must be list of 16 numbers') html_text = re.sub( r'"_camera_orientation":\s+\[[^\]]*\]', '"_camera_orientation": [' + ', '.join(map(str, orientation)) + ']', html_text) if remove_widget_view: widget_view_regex = ( r'<script type="application/vnd\.jupyter\.widget\-view\+json">' r'\n.*?\n' '</script>') html_text = re.sub(widget_view_regex, '', html_text) if html_file: if os.path.splitext(html_file)[1] != '.html': raise ValueError(f"`html_file` needs extension .html: {html_file}") with open(html_file, 'w') as f_html: f_html.write(html_text) if returnval == 'display': return IPython.display.display(IPython.display.HTML(data=html_text)) elif returnval == 'HTML': return IPython.display.HTML(data=html_text) elif returnval == 'none': return None else: raise ValueError(f"invalid `returnval` {returnval}")