From 1496cb75fb8afcf67426f47550ed3e24c319d2a7 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Wed, 19 Jun 2024 22:02:27 -0400 Subject: [PATCH 1/6] try to replace --- nglview/widget.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/nglview/widget.py b/nglview/widget.py index 58a14ece..1b07c4ab 100644 --- a/nglview/widget.py +++ b/nglview/widget.py @@ -1,5 +1,6 @@ import base64 import json +import re import threading import time import uuid @@ -110,6 +111,12 @@ def _unset_serialization(views): snippet = '\n' snippet += '\n' snippet += embed.embed_snippet(views) + # hacky thing for https://github.com/nglviewer/nglview/issues/1107 + # NGL must be fixed before we can remove this hack + _frontend = {'__frontend_version__': __frontend_version__} + pattern = r'("model_name":\s*"NGLModel",\s*"model_module":\s*"nglview-js-widgets",\s*"model_module_version":\s*)"' + re.escape(_frontend['__frontend_version__']) + '",' + replacement = r'\g<1>"3.0.8",' + snippet = re.sub(pattern, replacement, snippet) html_code = embed.html_template.format(title='nglview-demo', snippet=snippet) @@ -332,7 +339,7 @@ def camera(self): @camera.setter def camera(self, value): """ - + Parameters ---------- value : str, {'perspective', 'orthographic'} @@ -694,7 +701,7 @@ def _update_repr_dict(self): def set_representations(self, representations, component=0): """ - + Parameters ---------- representations : list of dict @@ -840,7 +847,7 @@ def _add_shape(self, shapes, name='shape'): Notes ----- Supported shape: 'mesh', 'sphere', 'ellipsoid', 'cylinder', 'cone', 'arrow'. - + See also -------- {ngl_url} @@ -983,7 +990,7 @@ def render_image(self, -------- # tell NGL to render send image data to notebook. view.render_image() - + # make sure to call `get_image` method view.get_image() @@ -1183,7 +1190,7 @@ def add_pdbid(self, pdbid, **kwargs): >>> import nglview >>> view = nglview.NGLWidget() >>> c = view.add_pdbid('1tsu') - >>> # which is equal to + >>> # which is equal to >>> # view.add_component('rcsb://1tsu.pdb') ''' return self.add_component(f'rcsb://{pdbid}.pdb', **kwargs) @@ -1331,7 +1338,7 @@ def _get_remote_call_msg(self, kwargs=None, **other_kwargs): """call NGL's methods from Python. - + Parameters ---------- method_name : str From d8fe3147525a8eb829d07df0afa652274a94163a Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Wed, 19 Jun 2024 22:02:33 -0400 Subject: [PATCH 2/6] Revert "try to replace" This reverts commit 1496cb75fb8afcf67426f47550ed3e24c319d2a7. --- nglview/widget.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/nglview/widget.py b/nglview/widget.py index 1b07c4ab..58a14ece 100644 --- a/nglview/widget.py +++ b/nglview/widget.py @@ -1,6 +1,5 @@ import base64 import json -import re import threading import time import uuid @@ -111,12 +110,6 @@ def _unset_serialization(views): snippet = '\n' snippet += '\n' snippet += embed.embed_snippet(views) - # hacky thing for https://github.com/nglviewer/nglview/issues/1107 - # NGL must be fixed before we can remove this hack - _frontend = {'__frontend_version__': __frontend_version__} - pattern = r'("model_name":\s*"NGLModel",\s*"model_module":\s*"nglview-js-widgets",\s*"model_module_version":\s*)"' + re.escape(_frontend['__frontend_version__']) + '",' - replacement = r'\g<1>"3.0.8",' - snippet = re.sub(pattern, replacement, snippet) html_code = embed.html_template.format(title='nglview-demo', snippet=snippet) @@ -339,7 +332,7 @@ def camera(self): @camera.setter def camera(self, value): """ - + Parameters ---------- value : str, {'perspective', 'orthographic'} @@ -701,7 +694,7 @@ def _update_repr_dict(self): def set_representations(self, representations, component=0): """ - + Parameters ---------- representations : list of dict @@ -847,7 +840,7 @@ def _add_shape(self, shapes, name='shape'): Notes ----- Supported shape: 'mesh', 'sphere', 'ellipsoid', 'cylinder', 'cone', 'arrow'. - + See also -------- {ngl_url} @@ -990,7 +983,7 @@ def render_image(self, -------- # tell NGL to render send image data to notebook. view.render_image() - + # make sure to call `get_image` method view.get_image() @@ -1190,7 +1183,7 @@ def add_pdbid(self, pdbid, **kwargs): >>> import nglview >>> view = nglview.NGLWidget() >>> c = view.add_pdbid('1tsu') - >>> # which is equal to + >>> # which is equal to >>> # view.add_component('rcsb://1tsu.pdb') ''' return self.add_component(f'rcsb://{pdbid}.pdb', **kwargs) @@ -1338,7 +1331,7 @@ def _get_remote_call_msg(self, kwargs=None, **other_kwargs): """call NGL's methods from Python. - + Parameters ---------- method_name : str From 604b27a214a78990066ddd9b3572c5a9fdd7f466 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Wed, 19 Jun 2024 22:08:01 -0400 Subject: [PATCH 3/6] Revert "Revert "try to replace"" This reverts commit d8fe3147525a8eb829d07df0afa652274a94163a. --- nglview/widget.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/nglview/widget.py b/nglview/widget.py index 58a14ece..1b07c4ab 100644 --- a/nglview/widget.py +++ b/nglview/widget.py @@ -1,5 +1,6 @@ import base64 import json +import re import threading import time import uuid @@ -110,6 +111,12 @@ def _unset_serialization(views): snippet = '\n' snippet += '\n' snippet += embed.embed_snippet(views) + # hacky thing for https://github.com/nglviewer/nglview/issues/1107 + # NGL must be fixed before we can remove this hack + _frontend = {'__frontend_version__': __frontend_version__} + pattern = r'("model_name":\s*"NGLModel",\s*"model_module":\s*"nglview-js-widgets",\s*"model_module_version":\s*)"' + re.escape(_frontend['__frontend_version__']) + '",' + replacement = r'\g<1>"3.0.8",' + snippet = re.sub(pattern, replacement, snippet) html_code = embed.html_template.format(title='nglview-demo', snippet=snippet) @@ -332,7 +339,7 @@ def camera(self): @camera.setter def camera(self, value): """ - + Parameters ---------- value : str, {'perspective', 'orthographic'} @@ -694,7 +701,7 @@ def _update_repr_dict(self): def set_representations(self, representations, component=0): """ - + Parameters ---------- representations : list of dict @@ -840,7 +847,7 @@ def _add_shape(self, shapes, name='shape'): Notes ----- Supported shape: 'mesh', 'sphere', 'ellipsoid', 'cylinder', 'cone', 'arrow'. - + See also -------- {ngl_url} @@ -983,7 +990,7 @@ def render_image(self, -------- # tell NGL to render send image data to notebook. view.render_image() - + # make sure to call `get_image` method view.get_image() @@ -1183,7 +1190,7 @@ def add_pdbid(self, pdbid, **kwargs): >>> import nglview >>> view = nglview.NGLWidget() >>> c = view.add_pdbid('1tsu') - >>> # which is equal to + >>> # which is equal to >>> # view.add_component('rcsb://1tsu.pdb') ''' return self.add_component(f'rcsb://{pdbid}.pdb', **kwargs) @@ -1331,7 +1338,7 @@ def _get_remote_call_msg(self, kwargs=None, **other_kwargs): """call NGL's methods from Python. - + Parameters ---------- method_name : str From f1109582593b1da37c04a25feb8f6a7162a68a4f Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Wed, 19 Jun 2024 22:12:42 -0400 Subject: [PATCH 4/6] hack --- nglview/widget.py | 2 +- notebooks/nglview-js-widgets.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 120000 notebooks/nglview-js-widgets.js diff --git a/nglview/widget.py b/nglview/widget.py index 1b07c4ab..4e3273a7 100644 --- a/nglview/widget.py +++ b/nglview/widget.py @@ -114,7 +114,7 @@ def _unset_serialization(views): # hacky thing for https://github.com/nglviewer/nglview/issues/1107 # NGL must be fixed before we can remove this hack _frontend = {'__frontend_version__': __frontend_version__} - pattern = r'("model_name":\s*"NGLModel",\s*"model_module":\s*"nglview-js-widgets",\s*"model_module_version":\s*)"' + re.escape(_frontend['__frontend_version__']) + '",' + pattern = r'("model_module":\s*"nglview-js-widgets",\s*"model_module_version":\s*)"' + re.escape(_frontend['__frontend_version__']) + '",' replacement = r'\g<1>"3.0.8",' snippet = re.sub(pattern, replacement, snippet) html_code = embed.html_template.format(title='nglview-demo', diff --git a/notebooks/nglview-js-widgets.js b/notebooks/nglview-js-widgets.js deleted file mode 120000 index f78500ca..00000000 --- a/notebooks/nglview-js-widgets.js +++ /dev/null @@ -1 +0,0 @@ -../nglview/static/index.js \ No newline at end of file From ef58852d9a9db7eae818b832c87d41f78997d4ff Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Wed, 19 Jun 2024 22:23:41 -0400 Subject: [PATCH 5/6] more --- nglview/tests/test_widget.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nglview/tests/test_widget.py b/nglview/tests/test_widget.py index a7ab11b5..dba952a1 100644 --- a/nglview/tests/test_widget.py +++ b/nglview/tests/test_widget.py @@ -527,7 +527,7 @@ def test_show_pymatgen(): def test_show_qcelemental(): import qcelemental as qcel - mol = qcel.models.Molecule.from_data("He 0 0 0") + mol = qcel.models.Molecule.from_data("He 0 0 0") view = nv.show_qcelemental(mol) view @@ -1026,6 +1026,7 @@ def test_write_html(mock_unset): fp = StringIO() with patch.object(embed, 'embed_snippet') as mock_embed: + mock_embed.embed_snippet.return_value = 'ok' nv.write_html(fp, [view], frame_range=(0, 3)) mock_embed.assert_called_with([tm, cm, view]) mock_unset.assert_called_with() @@ -1034,6 +1035,7 @@ def test_write_html(mock_unset): # box with patch.object(embed, 'embed_snippet') as mock_embed: + mock_embed.embed_snippet.return_value = 'ok' nv.write_html(fp, [HBox([view])], frame_range=(0, 3)) # FIXME: assertion? From a0cc4e638c37521d7e9c088f1d34dd7d0a13ff7f Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Wed, 19 Jun 2024 22:31:25 -0400 Subject: [PATCH 6/6] fix --- nglview/tests/test_widget.py | 10 +++++----- nglview/widget.py | 5 +---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/nglview/tests/test_widget.py b/nglview/tests/test_widget.py index dba952a1..8cb5b893 100644 --- a/nglview/tests/test_widget.py +++ b/nglview/tests/test_widget.py @@ -1025,17 +1025,17 @@ def test_write_html(mock_unset): display(view) fp = StringIO() - with patch.object(embed, 'embed_snippet') as mock_embed: - mock_embed.embed_snippet.return_value = 'ok' + with patch.object(embed, 'embed_snippet') as mock_embed_snippet: + mock_embed_snippet.return_value = 'ok' nv.write_html(fp, [view], frame_range=(0, 3)) - mock_embed.assert_called_with([tm, cm, view]) + mock_embed_snippet.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: - mock_embed.embed_snippet.return_value = 'ok' + with patch.object(embed, 'embed_snippet') as mock_embed_snippet: + mock_embed_snippet.return_value = 'ok' nv.write_html(fp, [HBox([view])], frame_range=(0, 3)) # FIXME: assertion? diff --git a/nglview/widget.py b/nglview/widget.py index 4e3273a7..b9a3c954 100644 --- a/nglview/widget.py +++ b/nglview/widget.py @@ -7,7 +7,7 @@ from logging import getLogger import ipywidgets as widgets -import ipywidgets.embed +from ipywidgets import embed import numpy as np from IPython.display import display from ipywidgets import (Image, Box, DOMWidget, HBox, VBox, IntSlider, Output, Play, Widget, @@ -85,9 +85,6 @@ def write_html(fp, views, frame_range=None): >>> nglview.write_html('index.html', [view], frame_range=(0, 5)) # doctest: +SKIP """ views = isinstance(views, DOMWidget) and [views] or views - embed = ipywidgets.embed - color = None - theme = None for _, v in _INIT_VIEWS.items(): views.insert(0, v)