Saturday, August 1, 2015

Running VPython in a Browser with Anaconda, Jupyter, and iVisual

Installing iPython Jupyter and iVisual

In a previous post, I described how to install and run VPython using iPython Notebook. However, the previous instructions are now outdated due to the upgrade of Notebook to Jupyter. The instructions below are for Jupyter.

When you install Anaconda, it will automatically install iPython and Jupyter. The only additional package you need for running VPython programs in a notebook is iVisual. Here are the steps to install Anaconda and iVisual (more detailed instructions with screen captures are available):

  1. Install Anaconda. (http://continuum.io/downloads)
  2. From a command line, type:  pip install ivisual
  3. From a command line, type: ipython notebook
  4. The Jupyter home screen will open in your browser. The header of the page is shown below.




  5. Click the "New" menu and select "Python 2" as shown below.




    You will see a header for the new Jupyter notebook as shown below.


  6. Type the following program into the first cell.

from __future__ import division, print_function
from ivisual import *
#create scene
scene = canvas(title='3D scene')

sphere()

You will see a VPython scene with a white sphere as shown below.



My Classical Mechanics class used iPython Notebook and iVisual for an entire semester, and it worked wonderfully. Here's a presentation I gave to Guilford College with a notebook.

4 comments:

  1. To display the scene while a simulation is running you can call the notebook display method and pass it the scene name. For instance.

    from ivisual import *

    scene2 = canvas(title="My 3D Scene")

    s = sphere()

    display(scene2)
    while True:
    rate(30)
    # your code to simulate the objects in the scene

    By calling display(sceneX) prior to the simulation loop, the 3D scene will be visible during the simulation.

    ReplyDelete
  2. Is there any way to interact with the display inside the Jupyter notebook?

    ReplyDelete
  3. That is really good and informative post. Going to share it with my friends circle so tht they can get benefit out of it. Thank you for sharing it with us

    ReplyDelete