VPython with IPython Notebook Inline

1,309 views
Skip to first unread message

John

unread,
Jun 26, 2014, 2:58:02 PM6/26/14
to vpytho...@googlegroups.com
Hi

   I have been looking at ways of how to run Vpython inline in an IPython Notebook. The solution that I have come up with is to create a new python module that understands the VPython API but instead of displaying 3D content in a separate wx window, it interprets the VPython API commands and displays the result using GlowScript which appears inline in the notebook. If you are an IPython Notebook user, you can download the python module called "ivisual" from PyPI using the command  "pip install ivisual" . Then in an IPython Notebook cell enter the command "from ivisual import * " and in another cell enter  s = sphere().  You should then see a 3D window of a sphere appear inline in the notebook which is rendered using GlowScript.

You can also try the "Ball in Box" VPython demo running in an IPython Notebook if you run this notebook.


Regards

John

Bruce Sherwood

unread,
Jun 26, 2014, 9:36:54 PM6/26/14
to vpytho...@googlegroups.com
Congratulations, John. Sounds like a major development.

John

unread,
Jun 26, 2014, 9:50:04 PM6/26/14
to vpytho...@googlegroups.com
Thanks. The functionality provided in this ivisual package is the intersection of GlowScript and VPython. If there is a corresponding GlowScript command for the VPython command then it gets executed as expected. If the VPython functionality doesn't exist in GlowScript then it will only perform what GlowScript is capable of. Here are some examples of IPython Notebooks using the ivisual module. You will need to run them in the IPython Notebook to see the 3D GlowScript canvas running inline.

and a bouncing ball demo with ipython slider widget can be found here http://nbviewer.ipython.org/url/dl.dropboxusercontent.com/u/5095342/visual/visual_widget.ipynb .

Give it a try and let me know what you think.

Aaron Titus

unread,
Jun 27, 2014, 9:17:54 AM6/27/14
to vpytho...@googlegroups.com
I haven't had a chance to test your programs yet. However, I've lamented for about 2 years the fact that I could not run vpython code in Notebook. In fact, was able to run the vpython code, but closing the vpython window would crash Notebook.

This is a great achievement. Thank you!

Aaron Titus

--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John

unread,
Jun 27, 2014, 3:09:13 PM6/27/14
to vpytho...@googlegroups.com
The current version of IPython Notebook has the infrastructure for setting up a communications channel between the ipython kernel and the javascript frontend. I made use of this communication channel for communicating between my python code in the kernel and the GlowScript javascript running in the browser front end. The performance that can be achieved will depend on the performance of GlowScript as well as the data throughput of the communication channel. The RotatingBoxes demo will give you an idea of the capabilities of this configuration. Setting the value of N in the loadBoxes(N = 8) cell command to increase the number of rotating boxes will demonstrate the performance that can be achieved on your computer. You can compare that with the standalone VPython version of the rotating boxes demo.

John

unread,
Jul 2, 2014, 5:38:42 PM7/2/14
to vpytho...@googlegroups.com
I have uploaded the latest version of the IVisual package to PyPI which allows you to run VPython simulations inline an IPython Notebook. It can be found at this site https://pypi.python.org/pypi/IVisual . This page provides a number links to sample IPython Notebooks that demonstrate usage of the IVisual package for VPython simulations. I have included notebooks that I made from Erik Thompson's Video Introduction to VPython that now run in an IPython Notebook.  http://showmedo.com/videotutorials/series?name=pythonThompsonVPythonSeries

Are there any existing python test files of the VPython API that I can use to test out my implementation of the API for IPython Notebook?

Thanks

John

Bruce Sherwood

unread,
Jul 2, 2014, 7:07:45 PM7/2/14
to vpytho...@googlegroups.com
The set of examples that is installed with VPython is an important source of test cases. Here they are in the GitHub repository:

https://github.com/BruceSherwood/vpython-wx/tree/master/site-packages/visual/examples

Also, in the "Lecture-demo materials" section of matterandinteractions.org are lots of physics demo programs written in VPython.

Peter Farrell

unread,
Oct 19, 2014, 2:51:49 AM10/19/14
to vpytho...@googlegroups.com
I'm a huge fan of VPython, and I'm disappointed that it won't work in IPython yet. I've complained to Anaconda that VPython isn't on their list of packages. Even after finding a VPython package using their "binstar" search and going through the "conda install" process, it still gives me an error message about "not a valid win32 application" when I try to run my VPython programs.

I'm encouraged by the Glowscript semi-solution, but there are so many differences that the majority of VPython programs will have to be rewritten to match the Glowscript API. The "earth" and "diffuse" materials are gone (probably others, too).

Keep up the good work, guys.

Peter Farrell

Bruce Sherwood

unread,
Oct 19, 2014, 5:16:49 PM10/19/14
to vpytho...@googlegroups.com
First, I encourage VPython users interesting in IPython to try John Coady's ivisual, which I think shows much promise, and to give him feedback on what you find. I would guess that having some people beating on ivisual will be very helpful to John in making sure that he's captured the full capabilities of VPython. 

Moreover, VPython, which uses OpenGL, necessarily cannot display in an IPython notebook, whereas ivisual can and does, using the GlowScript libraries to drive WebGL. Moreover, having the WebGL rendering separate from the user's Python code means that different CPU cores can work on these two tasks, whereas VPython has to do rendering in the same core used by the user's program. And it is still the case today that VPython's use of OpenGL is almost entirely in the CPU, whereas the GlowScript library does most of the rendering in the GPU and is able to do pixel-level transparency rather than the error-prone object-level transparency of VPython. I had thought it important to rework VPython to exploit the GPU, but John is already doing that with ivisual.

There are some speed issues because ivisual is written in pure Python, but John has been experimenting with speeding up his library by using Cython (or possibly the newer Numba). 

It would be a big win to have a working VPython that is written in pure or very nearly pure Python, because it would be easier to maintain and to extend, and many more people could contribute to its development, whereas in all the 15 years of VPython development very few people have ever touched the C++ code that is at its heart.

Second, I hope to be able to release in a few days GlowScript 1.1 that lets you write GlowScript programs in Python!! This is based on RapydScript, which is not exactly Python, but for many situations the code is indistinguishable, because it is the more arcane aspects of Python where RapydScript necessarily diverges in its task of compiling to JavaScript. With RapydScript/Python you will have to write vec() instead of vector(), etc., but a program will look very similar to the corresponding VPython program. At a later time I hope to implement a VPython -> GlowScript/RapydScript/Python convertor that will be able to do a MUCH better job than my existing VPython -> JavaScript converter.


Aaron Titus

unread,
Oct 19, 2014, 7:29:43 PM10/19/14
to vpytho...@googlegroups.com
Hi Peter,

I've been using iPython Notebook with ivisual for my Intermediate Mechanics course all semester and it works wonderfully. We haven't needed some of the features you might require, but I'm thrilled with the progress. This is the closest thing to running visual with iPython Notebook that has ever existed. I think it has tremendous promise.

Aaron

Josh Gates

unread,
Oct 20, 2014, 7:53:06 AM10/20/14
to vpytho...@googlegroups.com
Hi Aaron,

Could you post some links to exemplars of your work with that (or students' work)? It seems very exciting.

Hope things are going well!

Thanks,
jg

Aaron Titus

unread,
Oct 20, 2014, 9:45:34 AM10/20/14
to vpytho...@googlegroups.com
My course is at:


Naturally, it's a work in progress. For each of the outline items on the left that says, "Notebook"  there are tutorials in notebook that they are asked to view.

1. Here are some notebooks on the Cartesian Coord System, dot product, and cross product.


2. This tutorial helps them visualize the derivative of unit vectors in polar coordinates. I thought this was a nifty example of something that is more easily visualized dynamically than on paper, perhaps.


3. Here's an example of rocket motion in space (no grav).


4. Here's an example from a quiz question on a particle colliding with and sticking to a barbell. 


5. For many homework problems, I ask them to solve the problem analytically and numerically. Here's an example that shows them how to do that for a problem from Taylor's Classical Mechanics book.


6. Students write notebooks for compuational portions of quiz questions and labs as well.


At the end of the semester, I'll try to organize all of the notebooks in a clearer way.

Aaron

Peter Farrell

unread,
Oct 20, 2014, 11:04:15 AM10/20/14
to vpytho...@googlegroups.com
Hi, Aaron,

I've definitely been spoiled by the VPython I've had running perfectly on my computers so far. Heck, I learned Javascript so I could make use of Glowscript if students only have chromebooks. IPython seems to be the wave of the future (with good reason), so it's a shame VPython doesn't work on it yet. 

I dutifully installed IVisual and it works, but on Glowscript you can go back and forth between the editing and running pages. On IPython when I go back and forth between editing and running the script, some of the objects persist, ruining the model. Do I have to reload the notebook? I will admit if the code is the final version (for the students to view, for example) and you're just running the script once, this probably isn't a problem. I think this is what they call "first world problems." 

I'm glad IVisual is available, and I'll probably adapt to it fine. The examples from your notebooks are great! 

BTW, I installed WinPython (because I learned it comes pre-loaded with VPython) and it's working like a charm so far.

Peter Farrell



--
You received this message because you are subscribed to a topic in the Google Groups "VPython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vpython-users/G4cdbaDO_lg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vpython-user...@googlegroups.com.

John

unread,
Oct 20, 2014, 12:39:53 PM10/20/14
to vpytho...@googlegroups.com
The ivisual module website on pypi also has a number of sample notebooks that you can try.


Also here is the ivisual test cases notebook that I use for testing ivisual before putting out a release. You can download this notebook and click shift-enter on each of the cells to run each individual test.


You can have a maximum of about 11 webgl windows in a webpage so by running these tests in a notebook some of the older test windows will disappear.

With ivisual in an ipython notebook, you create a new 3D canvas using the command

scene = canvas()

and all subsequent vpython commands will act on this scene. You don't have to put a all the vpython commands in a single notebook cell. You can can add objects to the scene using different notebook cells. To create a new scene to display content just type something like

scene2 = canvas()

and then all subsequent vpython commands will then act on this new scene.

To reset everything use the "restart kernel" button in the IPython notebook rather than closing and reopening the notebook.

John

Bruce Sherwood

unread,
Oct 20, 2014, 12:50:01 PM10/20/14
to vpytho...@googlegroups.com
Peter, I feel quite strongly that for novice IPython use it is important to set up a simple option to start from scratch, and I made an attempt in that direction but didn't quite succeed. I don't consider the existing option (restart kernel) to be acceptable except for testing purposes, because it throws up an intrusive and scary dialog box instead of simply rerunning the current cell.

I know that expert programmers love the Python Shell and related aspects of the IPython notebook. On the basis of a great deal of experience with novices, I consider both environments highly inappropriate for novices, because it's hard to know what is the current import status. For their short programs, they need to be able to click or press F5 and run the whole program, clean, with no history, thereby having the immediacy and interactivity of a shell, but in the context of full programs.

John

unread,
Oct 20, 2014, 1:49:14 PM10/20/14
to vpytho...@googlegroups.com
In this ipython meeting entitled "Ipython Notebook in Education" around the 44 minute mark of the video they start talking about Integrated Development Environment's versus the Ipython Notebook.


They point out that the IPython Notebook is definitely not an IDE .

Aaron Titus

unread,
Oct 21, 2014, 9:05:56 AM10/21/14
to vpytho...@googlegroups.com
Thanks, John, for the link to this video. During the discussion about the difference between an IDE and IPython notebook, at approximately the 50:45 mark, Fernando Perez says that in a notebook, the focus is on the "narrative" with text, code, graphs, etc. integrated into a coherent story whereas in an IDE the focus is on the code such as functions, classes, clean API, reliability, etc. He says that they are not polar opposites but how to combine the two is an open question.

Since many vpython users are also teachers, I think we might take Perez' description and think about how and when to use VPython or IPython notebook in teaching. 

When the focus is on the code, especially when students learn numerical integration and the Momentum Principle for example, then use VPython. When the focus is on "telling a story" that requires explanation, code, graphs, etc. use IPython Notebook. For example, IPython Notebook might be the tool of choice for students who have already learned VPython and are now working on a class project or who are using compuational modeling as part of a lab report and need the mathematical markup, graphs with matplotlib, etc. Notebook can be an excellent tool for submitting lab reports.

For teachers, I think that IPython Notebook is the tool of choice for an online tutorial where an author wishes to combine narrative with a simulation.  Steve Spicklemire's notebooks at:


are a good example of this.

However, for a class demonstration of a simulation, VPython would probably be the choice.

AT

Steve Spicklemire

unread,
Oct 21, 2014, 10:32:33 AM10/21/14
to vpytho...@googlegroups.com, Steve Spicklemire
Thanks Aaron! Nice of you to mention these.

I should point out that these guys are definitely a work in progress. I’ll be updating them this spring with newer content.

It’s also useful to use the ‘nbviewer’ site to render them in HTML so you can actually see how they appear to students in the iPython notebook itself.

<http://nbviewer.ipython.org/github/sspickle/sci-comp-notebooks/tree/master/>

thanks!
-steve

Aaron Titus

unread,
Oct 21, 2014, 1:09:52 PM10/21/14
to vpytho...@googlegroups.com
You raise an excellent point, Peter. The replies by Bruce and John give good insight, but I can perhaps add one more tidbit to the discussion.

When one of my students (a junior who already has VPython experience) was writing a notebook, she was getting unexpected results. It turned out that it was because in one cell she initialized some of her variables and in another cell she had a few more variables and her loop. As she changed variables in the later cell and re-ran that cell, she was got unexpected results. We finally found that it was because some of her variables were not being re-initialized.

This is a classic example of what you are talking about.  So there's definitely a "best practice" when writing code in a notebook. For example, importing libraries should be accomplished in their own cell because you don't have to import them again. But initializing variables should be in the same cell as other code that you expect to modify and run again.

Thank you for prompting an interesting and helpful discussion.

Aaron

Bruce Sherwood

unread,
Oct 21, 2014, 1:29:54 PM10/21/14
to vpytho...@googlegroups.com
I would hope that it doesn't have to be either/or. If IPython could be ever so slightly modified so that clicking a button or pressing F5 would restart the kernel and run the program in the cell where the cursor is (without advancing the cursor), then it would be possible for intro-level students (or me, for that matter) to use the notebook the way one uses VPython.

As you know, I made a brief stab at modifying IPython in this way. I succeeded in repurposing a button to do a restart but failed in trying to have this action followed by executing the current cell. I'm sure that someone knowledgeable in IPython could achieve the effect easily.

Incidentally, Ruth Chabay makes a telling point about the importance of being able to run complete programs from a restart base, in the educational setting. In a small upper-level class, such as the one Aaron is teaching, lots of personal attention can deal with the kinds of problems that occur (and which he has just documented). But in intro courses with hundreds of students (for example, courses based on Matter & Interactions at places like Purdue or Georgia Tech) or even many thousands of students (the MOOC run by Georgia Tech) it would be death to throw students into IPython without a mechanism that permits running whole programs with full initialization.

John

unread,
Oct 21, 2014, 7:25:28 PM10/21/14
to vpytho...@googlegroups.com
Hi Bruce

    The javascript command to execute the current cell is  IPython.notebook.execute_cell(), I think this is the command you were looking for to use in your custom button. Other javascript commands that you might find useful are :

    IPython.notebook.get_selected_index()        for currently selected cell index
    IPython.notebook.execute_cells_above()       to execute all the cells above currently selected cell, excluding current cell
    IPython.notebook.execute_cells_below()
    IPython.notebook.execute_all_cells()

All of these commands and more can be found in the notebook.js file that can be found in the ipython notebook distribution in the directory.

   site-packages/IPython/html/static/notebook/js

I think most of the commands in the IPython Notebook menu's and toolbar can be found in the notebook.js file.

I just created a custom button that can run in a notebook using the following code that goes in a file called custom.js

   $([IPython.events]).on('app_initialized.NotebookApp', function(){

      IPython.toolbar.add_buttons_group([

      {

         'label' : 'restart',

         'icon' : 'icon-rocket', // select your icon from http://fortawesome.github.io/Font-Awesome/icons

         'callback': function () {

             IPython.notebook.kernel.execute('%reset -f')

             IPython.notebook.execute_cells_above()

         }

      }

      // add more button here if needed.

      ]);

   });


The callback function for this button will clear all the variables in the IPython notebook and will then run all of the cells above the currently selected cell.


One way to add a custom button to a notebook is to create a custom ipython profile and add the custom button to the profile. Then you launch the IPython Notebook using this profile. For instance, I can create a custom profile called "vpython", add a custom button to the profile, then launch the IPython Notebook using this profile. Using the Windows Command prompt I can type


ipython profile create vpython


this creates a directory with a bunch of files on my machine at  C:\Users\John\.ipython\profile_vpython . Edit the custom.js file adding the above code for the custom button. The custom.js file is located in the directory  \.ipython\profile_vpython\static\custom . Then launch the IPython Notebook using the custom profile.


ipython notebook --profile=vpython


Now the IPython Notebook will contain a custom button that will clear all the variables and run all the cells above the currently selected cell.


Also there is a nice list of custom button icons that you can choose from for your custom buttons.


http://fortawesome.github.io/Font-Awesome/icons/  


I chose the rocket icon from this list of icons.


John


John

unread,
Oct 23, 2014, 12:58:05 PM10/23/14
to vpytho...@googlegroups.com
Bruce wrote

   "I would hope that it doesn't have to be either/or. If IPython could be ever so slightly modified so that clicking a button or pressing F5 would restart the kernel and run the program in the cell where the cursor is (without advancing the cursor), then it would be possible for intro-level students (or me, for that matter) to use the notebook the way one uses VPython."

Here is an image of an ipython notebook that I customized using an ipython profile.


I added 4 buttons to the toolbar. The first is a refresh button that will remove all user defined variables and then run all of the cells from the beginning of the notebook to the currently selected cell. The second button with the rocket icon will do the same thing but run all of the cells in the notebook. The third button with the magic wand icon will just run all of the cells in the notebook. The fourth button with the lightning bolt icon will reset the kernel without prompting the user to confirm.

The method I used to customize IPython using a custom profile is documented here.


The custom.js code for these four buttons can be found here.


You can create your own version of a custom profile on your machine using the above documentation and just use the code for the buttons in custom.js . Then launch the IPython Notebook with your custom profile using the command line

ipython notebook --profile=my_custom_profile_name

This is one way to customize the IPython Notebook to make the slight modifications that Bruce was looking for. There may be other ways of accomplishing the upcoming version 3 of the IPython Notebook.

John



On Tuesday, October 21, 2014 10:29:54 AM UTC-7, Bruce Sherwood wrote:

Bruce Sherwood

unread,
Oct 23, 2014, 2:46:07 PM10/23/14
to vpytho...@googlegroups.com
Excellent, John. It did seem likely that someone who actually knew something about IPython could achieve these effects where I failed.

I could be wrong on this, but I believe that an important modality (maybe a 5th button), and one I would want for intro physics students, is to restart and then run only the current cell. That way the student could have more than one complete program in the notebook and choose which one to run from scratch.

John

unread,
Oct 24, 2014, 1:29:28 PM10/24/14
to vpytho...@googlegroups.com
I added a fifth button the my custom vpython profile to restart and then run only the current cell as shown in this screen capture.


The profile mechanism that IPython provides for customizing the notebook is meant to be something that the user performs on his own machine. The idea is that a user can create custom profiles for different projects to customize the notebook to suite their preferences. The profiles that the user creates are stored in the /.ipython/ directory on their machine. 

IPython also allows you to create a profile that can be distributed with a project for other users to use for the notebooks belonging to that project. The custom profile directory is placed in the same working directory as the notebooks belonging to a particular project. In this case, in order for a user to use the custom profile that was distributed with the project they will have to navigate to that project directory using their terminal (or command prompt in windows) and then launch the ipython notebook from that project directory with the custom profile. e.g.

   ipython notebook --profile=vpython

John
Reply all
Reply to author
Forward
0 new messages