Jupyter VPython

896 views
Skip to first unread message

Bruce Sherwood

unread,
Apr 8, 2016, 6:37:28 PM4/8/16
to Project Jupyter
"pip install vpython" installs an unusual module that makes it quite easy instead of extremely difficult to write Python programs that display real-time navigable 3D animations in a Jupyter notebook. Details are provided on the first page of vpython.org.

Consider for example the following remarkably short program which displays a 3D cube, positions the camera so that the scene fills the canvas, sets up appropriate lighting, and activates mouse or touch controls to rotate and zoom the camera:

from vpython import *
box()

Most 3D libraries require a great deal of code to accomplish this.

You can run vpython demo programs at Binder, even on mobile devices:


Classic VPython was created in 2000 by David Scherer, a student at Carnegie Mellon University. Its core is written in C++. In 2014 John Coady created a pure Python implementation of the Classic VPython API which sends data to a Jupyter notebook that invokes the GlowScript 3D libraries, which are based on WebGL. Ruth Chabay and I have been working with Coady to implement the GlowScript VPython API, which is somewhat different from the classic API but better adapted to the browser/GPU world. Because the API is the same, identical code can run in a Jupyter notebook or in the pure browser GlowScript environment (glowscript.org), where the RapydScript Python-to-JavaScript transpiler runs in the browser. (Currently widget invocation is different in the two versions; it is planned to provide wrappers around the Jupyter and jquery widgets so that widget code will also be transportable between the two environments.)

The GlowScript environment is particularly appropriate for novice programmers, whereas Jupyter VPython is better suited for professionals, because it provides access to all standard Python modules, which is not possible in the JavaScript-based GlowScript environment.

Although Jupyter VPython is already quite fast, as can be seen in some of the demo programs, John, Ruth, and I intend to use Cython to make it significantly faster.

Bruce Sherwood

MinRK

unread,
Apr 8, 2016, 6:59:27 PM4/8/16
to Project Jupyter
This is very cool! Thanks for sharing.

-MinRK

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/9cca2b21-b472-4492-8194-696db8dbb2b0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hai Nguyen

unread,
Apr 8, 2016, 10:10:24 PM4/8/16
to jup...@googlegroups.com
cool. thanks for sharing. 

PS: there are some non-working notebooks: For example Gyroscope.ipynb gives NameError.

Hai

Bruce Sherwood

unread,
Apr 8, 2016, 11:19:49 PM4/8/16
to jup...@googlegroups.com
Sometimes one can get the message, as is the case with Gyroscope, "I couldn't find a kernel matching VPython. Please select a kernel." You are then offered a list of notebooks that includes VPython, which one needs to choose. I don't get these messages when running the programs on my own computer.

Is this a bug in Binder, or is the notebook type not part of the notebook?

Hai Nguyen

unread,
Apr 8, 2016, 11:38:32 PM4/8/16
to jup...@googlegroups.com
I see. I chose Python 2 kernel since it was 1st option (there are 3 options: py2, py3, and VPython). But then following your suggestion to choose VPython kernel, the notebook works smoothly.
thanks

Hai 

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.

Bruce Sherwood

unread,
Apr 9, 2016, 11:34:51 PM4/9/16
to Project Jupyter
0.3.6: Fixes some bugs; RotatingBoxes runs MUCH faster, Stonehenge fly-through now works better.

Thomas Haslwanter

unread,
Apr 13, 2016, 3:12:17 AM4/13/16
to Project Jupyter
Bruce, I got things to work in the notebook.
But in a normal Python module, the command
from vpython import *

produces the error message

Traceback (most recent call last):
  Python Shell, prompt 3, line 1
  File "C:\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\Lib\site-packages\vpython\__init__.py", line 1, in <module>
    from .vpython import *
  File "C:\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\Lib\site-packages\vpython\vpython.py", line 363, in <module>
    get_ipython().kernel.comm_manager.register_target('glow', GlowWidget)
builtins.AttributeError: 'NoneType' object has no attribute 'kernel'

Is there any way to use vpython in normal python scripts?

Bruce Sherwood

unread,
Apr 13, 2016, 10:59:08 AM4/13/16
to Project Jupyter
Do you mean that the demo programs work at Binder but not on your own machine? If this is the case, what is the environment on your own machine? 

Note that the function get_ipython() that is None in the error report should have been imported by a statement early in vpython.py, "from IPython.core.getipython import get_ipython", which evidently failed.

Review the instructions at vpython.org, which may shed light on the problem.

Bruce Sherwood

unread,
Apr 15, 2016, 11:29:41 PM4/15/16
to Project Jupyter
This problem turned out to be a mistake on my part. Some of the notebooks contained the wrong kernel reference. This has been fixed.

Thomas Haslwanter

unread,
May 6, 2016, 5:32:05 AM5/6/16
to Project Jupyter
VPython in Jupyter is really great, thanks a lot!
I have just only 2 problem2 with using it:
  1. when I save and close the notebook, the visualization vanishes. And after re-opening the notebook, can only get the visualization to re-appear if I manually restart the kernel.
  2. Once a visualization is stopped, I can not continue it (e.g. in the next cell of the notebook).

My questions:

  • is there a way to avoid that problem?
  • is there a way to continue visualizations?

Bruce Sherwood

unread,
May 6, 2016, 10:30:11 AM5/6/16
to Project Jupyter
The basic reason why the visualization is not saved is because it is produced and maintained dynamically by GPU WebGL mechanisms. It is an interesting question whether there might be a way to add something to the VPython environment that would (perhaps optionally) store and display a saved image that was created dynamically.

Please give an example of problem #2, as I'm unfamiliar with this. If cell 1 contains "b = box()" and I execute the cell, I see a white box. If in cell 2 I execute "b.color = color.cyan" or "sphere(pos=vector(2,1,0)", this changes the 3D display.

Perhaps I don't know what you mean by the visualization being "stopped".

Thomas Haslwanter

unread,
May 6, 2016, 2:22:22 PM5/6/16
to Project Jupyter
Thanks for the feedback!
To give you an example, have a look at
http://nbviewer.jupyter.org/github/thomas-haslwanter/CSS_ipynb/blob/master/Vestibular_3D_Animation.ipynb

The first time you run the command, the visualization appears. However, if you re-run the last command-box, there is no continuation of the arrow movement; instead, one has to re-run the last-but-one box (i.e. re-create an arrow), and only then the animation works again.

Bruce Sherwood

unread,
May 6, 2016, 7:20:10 PM5/6/16
to jup...@googlegroups.com
It would be very helpful if you could provide a notebook example that doesn't do I/O with files that I don't have. Thanks.

martin....@cfa.harvard.edu

unread,
Dec 6, 2016, 11:37:29 PM12/6/16
to Project Jupyter
I am using the Python3 version of Jupyter. Having some trouble with get_ipython() returning None it turned out that the reason was that Jupyter called the default Python which was still 2.7. update-alternatives fixed that and I got the first animations going.

At the end of a cell, even if it is just a static scene,
from vpython import *
box()
either the VPython notebook recalls the IPython kernel continuously or the IPython kernel goes crazy. The little light right of the VPython label is continuously flickering on and off. Is there a special exit or end_animation call?

Second question: Is there a way to use VPython outside of Jupyter or to embed the animations in to a pyqt program?

Remark: VPython is very nice, kind of povray light with much better animation and 3D support

Bruce Sherwood

unread,
Dec 7, 2016, 8:09:40 PM12/7/16
to Project Jupyter
At the end of the Python program the JavaScript machinery is still active, which is important because it makes it possible to rotate and zoom the camera to examine the 3D scene created by the Python program. Also, there are still messages being sent between the server and the browser. You can kill the flicker by clicking the black box at the top of the notebook page, and you can kill everything by reloading the browser page or restarting the kernel.

I know nothing about pyqt. 

It is possible to use VPython outside of Jupyter. One way is to run the program at glowscript.org, and another is to use Classic VPython, which is no longer being developed (see vpython.org for more info). Do read this:

Reply all
Reply to author
Forward
0 new messages