From 4e10aca789eb892d1670d939c406546e7baa9e8f Mon Sep 17 00:00:00 2001 From: David Koes Date: Thu, 23 Jan 2025 21:49:46 -0500 Subject: [PATCH] support options in py3dmol for view with data argument --- py3Dmol/examples.ipynb | 451 ++++++++++++++---------------------- py3Dmol/py3Dmol/__init__.py | 6 +- py3Dmol/setup.py | 2 +- 3 files changed, 183 insertions(+), 276 deletions(-) diff --git a/py3Dmol/examples.ipynb b/py3Dmol/examples.ipynb index 1e7ee4da..f63daeec 100644 --- a/py3Dmol/examples.ipynb +++ b/py3Dmol/examples.ipynb @@ -47,7 +47,7 @@ { "data": { "text/plain": [ - "'2.0.3'" + "'2.4.2'" ] }, "execution_count": 2, @@ -66,11 +66,10 @@ "outputs": [ { "data": { - "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", + "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", + "
\n", + "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" @@ -123,7 +122,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 3, @@ -154,16 +153,15 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 11, "metadata": {}, "outputs": [ { "data": { - "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", + "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", + "
\n", + "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" ] @@ -217,9 +215,7 @@ } ], "source": [ - "xyzview = py3Dmol.view(width=400,height=400)\n", - "xyzview.addModel(xyz,'xyz',{'vibrate': {'frames':10,'amplitude':1}})\n", - "xyzview.setStyle({'stick':{}})\n", + "xyzview = py3Dmol.view(width=400,height=400,data=xyz,options={'vibrate': {'frames':10,'amplitude':1}},style='stick')\n", "xyzview.setBackgroundColor('0xeeeeee')\n", "xyzview.animate({'loop': 'backAndForth'})\n", "xyzview.zoomTo()\n", @@ -235,16 +231,15 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { - "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", + "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", + "
\n", + "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" ] @@ -326,16 +321,15 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 13, "metadata": {}, "outputs": [ { "data": { - "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", + "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", + "
\n", + "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "", + "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", + "
\n", + "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" @@ -509,16 +502,15 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 15, "metadata": {}, "outputs": [ { "data": { - "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", + "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", + "
\n", + "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" @@ -584,16 +576,15 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 16, "metadata": {}, "outputs": [ { "data": { - "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", + "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", + "
\n", + "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" @@ -647,10 +638,10 @@ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 10, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -675,12 +666,12 @@ "outputs": [ { "data": { - "application/3dmoljs_load.v0": "\n ", + "application/3dmoljs_load.v0": "\n ", "text/html": [ - "\n", + "\n", " " ] }, @@ -693,87 +684,6 @@ "png" ] }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", - "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", - "
\n", - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import requests, base64\n", - "r = requests.get('https://mmtf.rcsb.org/v1.0/full/5lgo')\n", - "view = py3Dmol.view()\n", - "view.addModel(base64.b64encode(r.content).decode(),'mmtf')\n", - "view.addUnitCell()\n", - "view.zoomTo()" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -783,16 +693,15 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 19, "metadata": {}, "outputs": [ { "data": { - "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", + "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", + "
\n", + "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "", + "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", + "
\n", + "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" @@ -928,10 +836,10 @@ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 14, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -944,16 +852,15 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 21, "metadata": {}, "outputs": [ { "data": { - "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", + "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ - "
\n", - "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", - " jupyter labextension install jupyterlab_3dmol

\n", + "
\n", + "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" @@ -1007,10 +914,10 @@ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 15, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -1038,18 +945,18 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 22, "metadata": {}, "outputs": [ { "data": { - "application/3dmoljs_load.v0": "", + "application/3dmoljs_load.v0": "", "text/html": [ "" ] @@ -1063,7 +970,7 @@ "'Inspect the JavaScript console for your requested result.'" ] }, - "execution_count": 18, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } diff --git a/py3Dmol/py3Dmol/__init__.py b/py3Dmol/py3Dmol/__init__.py index 75be5d78..3960dbb6 100644 --- a/py3Dmol/py3Dmol/__init__.py +++ b/py3Dmol/py3Dmol/__init__.py @@ -63,7 +63,7 @@ def __init__(self,query='',width=640,height=480,viewergrid=None,data=None,style= data -- molecular data to provide to addModel, with viewer grid can be indexed (r,c) format -- format of provided data style -- style to apply, with viewer grid can be indexed (r,c) - options -- optional options to provide to $3Dmol.download + options -- optional options to provide to $3Dmol.download or addModel js -- url for 3Dmol.js''' divid = "3dmolviewer_UNIQUEID" warnid = "3dmolwarning_UNIQUEID" @@ -159,7 +159,7 @@ def __init__(self,query='',width=640,height=480,viewergrid=None,data=None,style= f = format[r][c] except: f = format - self.startjs += f"viewergrid_UNIQUEID[{r}][{c}].addModel({json.dumps(d)}{','+json.dumps(f) if f else ''});\n" + self.startjs += f"viewergrid_UNIQUEID[{r}][{c}].addModel({json.dumps(d)},{json.dumps(f) if f else 'undefined'},{json.dumps(options) if options else 'undefined'});\n" if style: try: s = style[r][c] @@ -171,7 +171,7 @@ def __init__(self,query='',width=640,height=480,viewergrid=None,data=None,style= else: cmds = '' if data: - cmds = f"viewer_UNIQUEID.addModel({json.dumps(data)}{','+json.dumps(format) if format else ''});\n" + cmds = f"viewer_UNIQUEID.addModel({json.dumps(data)},{json.dumps(format) if format else 'undefined'},{json.dumps(options) if options else 'undefined'});\n" if style: cmds += "viewer_UNIQUEID.setStyle(%s);\n"%json.dumps(style) self.startjs += cmds + "viewer_UNIQUEID.zoomTo();\n" diff --git a/py3Dmol/setup.py b/py3Dmol/setup.py index d0a917b4..e13a81ff 100644 --- a/py3Dmol/setup.py +++ b/py3Dmol/setup.py @@ -24,7 +24,7 @@ # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/single_source_version.html # Keep version in synce with 3Dmol.js version. Use "postX" suffix if needed - version='2.4.2', + version='2.4.2post1', description='An IPython interface for embedding 3Dmol.js views in Jupyter notebooks', long_description=long_description,