VPython?

102 views
Skip to first unread message

Bruce Sherwood

unread,
May 21, 2020, 4:03:45 PM5/21/20
to iodide-dev
I'm the lead developer of VPython, a Python module that makes it easy rather than impossibly difficult to write Python programs that generate navigable real-time 3D animations. For an example, click tinyurl.com/vporbit to see a binary-star animation. After playing with the camera controls, click "View this program" to see the amazingly short Python program. This is all at glowscript.org, where Python code is transpiled in the browser by RapydScript-NG, which produces efficient JavaScript and, except for pretty esoteric cases, does a good job of obeying Python syntax. Most Python-to-JavaScript transpilers produce very slow JavaScript, but these "GlowScript VPython" programs run several times faster than with installed Python.

This pure-browser version of VPython is an excellent environment for beginning programmers, and there are 90,000 user accounts at glowscript.org. The majority of these accounts are students in intro physics courses where computational modeling is taught. The big advantage is that these students don't need to know how to wrestle with installers, nor do they need to know about folders and files and file extensions, all of which are surprisingly serious barriers to these students. As the user types, the program is autosaved to the cloud. The limitation is that, GlowScript VPython being essentially a thin wrapper around JavaScript, one cannot import Python modules, though this is not a serious issue in an intro physics course.

There is also a vpython module for use with installed Python (vpython.org). The syntax is the same as with GlowScript VPython, but of course it permits access to all Python modules and so is the right choice for more advanced courses. It runs in Jupyter notebook and Jupyter lab as well as with other launchers such as spyder or IDLE.

In all of these cases the 3D graphics are produced with the GPU-based WebGL 3D graphics library that is built into browsers. In the case of the vpython module used with installed Python, the Python API is written in Python, which contains local server capabilities to communicate with a browser that accesses the Glowscript library. Here are a short and a long presentation on the architecture of the several flavors of VPython:



Obviously it would be much better if glowscript.org used real Python, not Python transpiled to JavaScript. I totally agree with the notion that Python really MUST run in browsers. My question is this: What would I have to learn and do to get VPython running in Iodide? Or to put it another way, is there someone knowledgeable with Iodide who would be interested in getting VPython to run there?

You can see lots of examples of VPython by going to glowscript.org and clicking "Example programs". Click "Help" to see the documentation. Be aware that VPython's support for vector operations includes operator overloading even in the browser so that vectors can be added and subtracted, etc. Also, in the transpiling to JavaScript I insert async and await as needed, which makes possible infinite loops and pauses in a browser environment.

Bruce

William Lachance

unread,
May 23, 2020, 7:23:26 PM5/23/20
to Bruce Sherwood, iodide-dev
Hi Bruce, this looks interesting. Note that development on iodide has slowed down quite a bit lately (maybe temporarily, maybe for the long term), but people are still working on it a bit and experimenting with things. Also, development on Pyodide (python on webassembly) is continuing to progress, mostly thanks to the efforts of Roman Yurchak.

I don't think I have time to work on this myself, but if you wanted to do the legwork I could see two ways forward for you:

1. Experiment with running vpython inside the iodide environment itself, probably as a language plugin (https://iodide-project.github.io/docs/language_plugins/), maybe using pyodide to run the code itself.
2. Experiment with running vpython just using pyodide (https://pyodide.readthedocs.io/en/latest/)-- you'd lose the interactive iodide environment but this might be an easier way to prototype things.

Hope that helps,

Will

--
You received this message because you are subscribed to the Google Groups "iodide-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iodide-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/iodide-dev/192b7775-e87c-4f54-af7c-529207cf0f95%40googlegroups.com.

Bruce Sherwood

unread,
May 23, 2020, 9:16:36 PM5/23/20
to iodide-dev
Thanks much for the response. The second link (pyodide) is broken. I agree with your estimate that pyodide seems the place to experiment.

Can you give me a brief overview of the pyodide architecture? In particular, do you see any problems in the fact that our Python code implements a local server that talks to JavaScript code that talks to WebGL and also sends update information back to the Python code, such as the current location of the mouse? Our Python code sets up an html server in order to start up the JavaScript component, and it also sets up a websocket to carry out fast data transfers between the Python and JavaScript components.

Bruce

Louis Abraham

unread,
May 24, 2020, 3:15:56 AM5/24/20
to 'Louis Abraham' via iodide-dev
I don't think it's possible to open a websocket connection. Actually, IIRC pyodide doesn't possess any networking capabilities.

The good news is that you can just replace the websocket with callbacks.
For instance, instead of sending packets, Python can directly trigger the onmessage function of JS. The other direction might need some refactoring as your Python code is probably not event based, but it shouldn't be difficult.
--
You received this message because you are subscribed to the Google Groups "iodide-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iodide-dev+...@googlegroups.com.

Bruce Sherwood

unread,
May 24, 2020, 10:27:39 AM5/24/20
to iodide-dev
Thanks! Having thought about this a bit, I was going to ask whether it would in fact be unnecessary to do server-client communications. You've answered the question I was going to ask.

Bruce

Bruce Sherwood

unread,
May 24, 2020, 1:24:30 PM5/24/20
to iodide-dev
For concreteness, can one of you state explicitly 1) the syntax for calling a JavaScript function from Python and 2) the syntax for sending event data such as a mouse position from JavaScript to Python?

Also, can you say briefly how one sets up a JavaScript library from within Python?

Bruce

William Lachance

unread,
May 24, 2020, 3:01:02 PM5/24/20
to iodide-dev
Hi Bruce, the pyodide example notebook for iodide has some examples of interacting between js-space and python-space:


The same principles apply even if you're using pyodide outside of iodide itself.

Will

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

Bruce Sherwood

unread,
May 24, 2020, 3:06:42 PM5/24/20
to iodide-dev
Thanks much for the detailed help, guys! 

I see that https://pyodide.readthedocs.io/en/latest/ now works for me, which looks useful. Dunno why I couldn't reach that site a couple of days ago.

Bruce

Bruce Sherwood

unread,
May 24, 2020, 4:11:58 PM5/24/20
to iodide-dev
I see what happened. I had accidentally picked up some extra characters in the url  https://pyodide.readthedocs.io/en/latest/. All fine now.

Bruce
Reply all
Reply to author
Forward
0 new messages