Visualize in jupyter notebook

Dear @stukowski,

I want to inform some problems when using Ovito in jupyter notebook (I remember that I reported this problem before in your own website’s forum, but now it is for Pro users only, so I cannot find that thread)

  1. After version 3.7.9, when I render animation with Tachyon engine, the process runs very slow and tends to run endlessly (I try with Conda python 3.9, 3.10, 3.11). When I try ver 3.7.9 with Python 3.9, the render engine works well.
  2. When I try with this code: (ver 3.9.1)
widget = vp.create_jupyter_widget()
display(widget)

Then it comes with the error:

Failed to load model class 'OvitoViewportModel' from module 'jupyter-ovito'
Error: No version of module jupyter-ovito is registered
    at ph.loadClass (https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/thang/.vscode/extensions/ms-toolsai.jupyter-renderers-1.0.17/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:4099813)
    at ph.loadClass (https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/thang/.vscode/extensions/ms-toolsai.jupyter-renderers-1.0.17/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:4403287)
    at ph.loadModelClass (https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/thang/.vscode/extensions/ms-toolsai.jupyter-renderers-1.0.17/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:4097773)
    at ph._make_model (https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/thang/.vscode/extensions/ms-toolsai.jupyter-renderers-1.0.17/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:4094616)
    at ph.new_model (https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/thang/.vscode/extensions/ms-toolsai.jupyter-renderers-1.0.17/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:4092246)
    at ph.handle_comm_open (https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/thang/.vscode/extensions/ms-toolsai.jupyter-renderers-1.0.17/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:4091039)
    at https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/thang/.vscode/extensions/ms-toolsai.jupyter-renderers-1.0.17/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:4402511
    at g._handleCommOpen (https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/thang/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-win32-x64/out/webviews/webview-side/ipywidgetsKernel/ipywidgetsKernel.js:2:36099)
    at async g._handleMessage (https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/thang/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-win32-x64/out/webviews/webview-side/ipywidgetsKernel/ipywidgetsKernel.js:2:37993)

Can you help with a little guide?
I use jupyter notebook in VScode.
Thank you.

The two problems you describe are unrelated and we need to discuss them separately. Let’s refer to them as problem I and II.

Problem I:

This is unexpected. Could you please describe in more detail which operation takes longer than usual? Is it the call to the Viewport.render_image() function? If you have been doing this in a Jupyter notebook, it could be useful to test this also outside Jupyter from a standalone Python program to see if it makes any difference. Note that you can include a call to the ovito.enable_logging() function in your program to see some more information on what OVITO is currently doing.

Problem II:

So far I could not reproduce the issue in my local environment (macOS). Perhaps because I used different installation steps. Here is what I did: First, I’ve created a new Conda environment named “jupyter”, installing the ovito package and ipywidgets. The latter is needed by the create_jupyter_widget() function:

conda create -n jupyter --strict-channel-priority -c https://conda.ovito.org -c conda-forge ovito==3.9.1 ipywidgets

Next, I created a Jupyter notebook in VS Code and selected the “jupyter” conda environment as active interpreter for the notebook. The following cell code produces a black viewport widget (as it should be):

from ovito.vis import Viewport
vp = Viewport()
vp.create_jupyter_widget()

In VS Code’s “Output” terminal (after selecting “Jupyter”), I see the following log output:

10:24:49.004 [info] User belongs to experiment group 'FastKernelPicker'
10:24:49.004 [info] User belongs to experiment group 'NewRemoteUriStorage'
10:24:49.004 [info] User belongs to experiment group 'PasswordManager'
10:24:49.004 [info] User belongs to experiment group 'NewJupyterSession'
10:24:49.207 [info] Start refreshing Kernel Picker (1692001489207)
10:24:49.208 [info] Using Pylance
10:24:49.475 [info] Starting Kernel startUsingPythonInterpreter, .jvsc74a57bd04ca29c47d6b62bf13677655d657ac3421b0c2ea5d37db50160fad649d39e7893.~/miniconda3/envs/jupyter/python.~/miniconda3/envs/jupyter/python.-m#ipykernel_launcher  (Python Path: ~/miniconda3/envs/jupyter/bin/python, Conda, jupyter, 3.10.6) for '~/prj/ovito-pro/tests/scripts/Untitled.ipynb' (disableUI=true)
10:24:50.017 [info] Process Execution: ~/miniconda3/envs/jupyter/bin/python -m pip list
10:24:50.212 [info] Process Execution: ~/miniconda3/envs/jupyter/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
10:24:50.215 [info] Process Execution: ~/miniconda3/envs/jupyter/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9003 --control=9001 --hb=9000 --Session.signature_scheme="hmac-sha256" --Session.key=b"047a77c5-41a9-4d06-bcde-609043cf2c0f" --shell=9002 --transport="tcp" --iopub=9004 --f=~/Library/Jupyter/runtime/kernel-v2-39151f0gm5nF3qgx7.json
    > cwd: ~/prj/ovito-pro/tests/scripts
10:24:50.258 [info] End refreshing Kernel Picker (1692001489207)
10:24:50.459 [info] ipykernel version & path 6.25.1, ~/miniconda3/envs/jupyter/lib/python3.11/site-packages/ipykernel/__init__.py for ~/miniconda3/envs/jupyter/bin/python
10:24:50.751 [warn] StdErr from Kernel Process 0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
10:24:50.751 [warn] StdErr from Kernel Process 0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
10:24:51.076 [info] Started Kernel jupyter (Python 3.10.6) (pid: 39607)
10:24:51.076 [info] Started new session 1bf59fb7-fd2f-4e3f-81cf-aae25846253d
10:24:51.146 [info] Process Execution: ~/miniconda3/envs/jupyter/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2023.7.1002162226-darwin-arm64/pythonFiles/printJupyterDataDir.py
10:25:18.143 [info] Handle Execution of Cells 0 for ~/prj/ovito-pro/tests/scripts/Untitled.ipynb
10:25:18.149 [info] Kernel acknowledged execution of cell 0 @ 1692001518146
10:25:19.220 [info] End cell 0 execution @ 1692001519218, started @ 1692001518146, elapsed time = 1.072s
10:27:51.594 [info] Handle Execution of Cells 0 for ~/prj/ovito-pro/tests/scripts/Untitled.ipynb
10:27:51.612 [info] Kernel acknowledged execution of cell 0 @ 1692001671607
10:27:51.614 [info] End cell 0 execution @ 1692001671612, started @ 1692001671607, elapsed time = 0.005s
10:27:54.861 [info] Dispose Kernel '~/prj/ovito-pro/tests/scripts/Untitled.ipynb' associated with '~/prj/ovito-pro/tests/scripts/Untitled.ipynb'
10:27:54.861 [info] Dispose Kernel process 39607.
10:28:00.101 [info] Start refreshing Interpreter Kernel Picker
10:28:00.102 [info] Start refreshing Kernel Picker (1692001680102)
10:28:00.116 [info] Starting Kernel startUsingPythonInterpreter, .jvsc74a57bd04ca29c47d6b62bf13677655d657ac3421b0c2ea5d37db50160fad649d39e7893.~/miniconda3/envs/jupyter/python.~/miniconda3/envs/jupyter/python.-m#ipykernel_launcher  (Python Path: ~/miniconda3/envs/jupyter/bin/python, Conda, jupyter, 3.10.6) for '~/prj/ovito-pro/tests/scripts/Untitled.ipynb' (disableUI=true)
10:28:00.123 [info] Process Execution: ~/miniconda3/envs/jupyter/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
10:28:00.141 [info] Process Execution: ~/miniconda3/envs/jupyter/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9003 --control=9001 --hb=9000 --Session.signature_scheme="hmac-sha256" --Session.key=b"4242ce19-63a3-4f21-b03d-a18253985f34" --shell=9002 --transport="tcp" --iopub=9004 --f=~/Library/Jupyter/runtime/kernel-v2-39151QkBNnXn71tW9.json
    > cwd: ~/prj/ovito-pro/tests/scripts
10:28:00.446 [info] ipykernel version & path 6.25.1, ~/miniconda3/envs/jupyter/lib/python3.11/site-packages/ipykernel/__init__.py for ~/miniconda3/envs/jupyter/bin/python
10:28:00.785 [warn] StdErr from Kernel Process 0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
10:28:00.870 [warn] StdErr from Kernel Process ~/.local/lib/python3.11/site-packages/traitlets/traitlets.py:2548: FutureWarning: Supporting extra quotes around strings is deprecated in traitlets 5.0. You can use 'hmac-sha256' instead of '"hmac-sha256"' if you require traitlets >=5.
  warn(
10:28:00.870 [warn] StdErr from Kernel Process ~/.local/lib/python3.11/site-packages/traitlets/traitlets.py:2499: FutureWarning: Supporting extra quotes around Bytes is deprecated in traitlets 5.0. Use '4242ce19-63a3-4f21-b03d-a18253985f34' instead of 'b"4242ce19-63a3-4f21-b03d-a18253985f34"'.
  warn(
10:28:00.997 [info] Started Kernel jupyter (Python 3.10.6) (pid: 40407)
10:28:00.997 [info] Started new session 5e91cf1b-3873-4fd6-875d-ce2ccff607b2
10:28:02.245 [info] Handle Execution of Cells 0 for ~/prj/ovito-pro/tests/scripts/Untitled.ipynb
10:28:02.249 [info] Kernel acknowledged execution of cell 0 @ 1692001682247
10:28:02.516 [info] End refreshing Kernel Picker (1692001680102)
10:28:02.884 [info] End cell 0 execution @ 1692001682883, started @ 1692001682247, elapsed time = 0.636s

You may want to compare this to the output you see on your system.

Another thing I did in the meantime is pushing the latest version 0.1.5 of the JavaScript jupyter-ovito package module to the npm repo (replacing the outdated version 0.1.4). It may be under certain circumenstances that VS Code’s Jupyter implementation tries to pull this package from the repo, which is needed for the frontend implementation of the 3d viewport widget. The error message suggest that this module wasn’t available for some reason, maybe because VS code could access the module’s latest version. So this update could make a difference for you if you try again. Under normal circumstance though, the module gets automatically installed as part of the ovito conda package and no download from the npm server is necessary.

Dear @stukowski

Problem I: I encounter the problem when using the vp.render_anim function with Tachyon engine. I have not tested outside jupyter yet.

Problem II: I use the same installation steps as you did, but on Windows 11. As my error message, there maybe problem with jupyter-ovito module.

Please test the rendering with Tachyon outside a Jupyter notebook to see if the behavior is different.

To learn more about Problem II, It would be useful if you could post the VS Code Jupyter log after the error has occurred. See my screenshot for how to access the log in VS Code. Thanks.

Dear @stukowski

Here is the screenshot of my VScode Jupyter after problem II occurs

It would be helpful, if you could post the entire text from the log window, not just a screenshot showing the last few lines only. I would like to understand why VS Code decides to download the jupyter-ovito package from the CDN server in the first place, which provides the notebook widget. This download should not be necessary under normal circumstances, because a copy of this package is already shipped as part of the ovito conda package.

Please also pay attention to the version of Jupyter extension you are using. Is it the most recent one? The information is displayed at the top of the log window:

Visual Studio Code (1.81.1, undefined, desktop)
Jupyter Extension Version: 2023.7.1002162226.
Python Extension Version: 2023.14.0.

Note also that VS Code requires the ipykernel package to run Jupyter notebooks. VS Code may try to install that package automatically in the conda environment for you, but it can make sense to install by hand during setup of the conda environment:

conda create -n jupyter --strict-channel-priority -c https://conda.ovito.org -c conda-forge ovito==3.9.1 ipywidgets ipykernel

Getting custom Jupyter widgets to work in VS Code is a very very complicated matter. It requires a lot of machinery behind the scene (see here) and many things can break. My own understanding of the system is very limited. A google search for the error message you see in the log window brings up a few hits, for example this issue. Apparently, version conflicts between the various involved components may be responsible.

Dear @stukowski

I have just tried with Ovito 3.9.2. And this problem is gone. Thank you for the update.
It is simply that Ovito works with python < 3.11?

For future updates, can you add some control features on the right side of the widget? Something like slider, or play function,… to work around frames in the traj?

Thank you so much for your contribution.

So far we did not have a conda package for Python 3.11 on Windows due to build issues. But such a package is going to be provided with the next OVITO release.

I’ve set up a demo notebook, which shows how to create a time slider widget controlling the displayed trajectory frame. See the “viewport trajectory demo” here: Alexander Stukowski / ovito-binder · GitLab

However, automatic playback of the trajectory, like in the desktop app, is not possible yet. The OVITO Jupyter widget definitely needs some more love, because its capabilities are still very rudimentary.

As OVITO developers, we are faced with a dilemma: We would actually like to develop and offer a better Jupyter integration, but we rely on funding from our customers. In fact, we need to convince more people to license the paid software to keep the project alive. We don’t currently earn anything from the OVITO Python package and the Jupyter widget is a part of it. We put a lot of our time into these by-products, which we make freely available to the community. At the same time, improved GUI capabilities of the Jupyter widget may actually hurt sales of the OVITO Pro desktop app by making advanced features easy to use outside the application. Such an improvement would be both a curse and a blessing. Therefore, we are currently hesitant to invest more effort into this part of the software.

I am happy to discuss this. Let me know what you think about having Jupyter integration as part of the OVITO Pro offering. Is this something you would be interested in if it offered more capabilities than it does right now?

1 Like