### Development Setup for jupyterlab-vpython Source: https://github.com/vpython/vpython-jupyter/blob/master/labextension/vpython/README.md Guides through setting up a development environment for jupyterlab-vpython. This involves creating a dedicated conda environment, cloning the repository, installing dependencies with jlpm, building the extension, and installing it locally. ```bash conda create -n jupyterlab-ext --override-channels --strict-channel-priority -c conda-forge -c nodefaults jupyterlab=4 nodejs=18 git copier=7 jinja2-time conda activate jupyterlab-ext git clone https://github.com/jcoady/jupyterlab_vpython.git cd jupyterlab_vpython cp -r ../../vpython/vpython_{libraries,data} . jlpm install jlpm add @jupyterlab/application @jupyterlab/apputils @jupyterlab/coreutils @jupyterlab/docregistry @jupyterlab/notebook @lumino/disposable file-loader plotly.js-dist-min script-loader jlpm run build pip install -ve . conda activate jupyterlab-ext jupyter lab ``` -------------------------------- ### Setup VPython Scene and Create Extrusion Shapes Source: https://github.com/vpython/vpython-jupyter/blob/master/Demos/Extrusions.ipynb Initializes the VPython canvas for Jupyter notebooks and demonstrates the creation of multiple 3D objects using the `extrusion` primitive with various shapes and paths. Includes setting up scene properties and defining object appearances. ```python from vpython import * scene = canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable scene.background = color.gray(0.8) scene.forward = vec(0,-0.2,-1) scene.fov = 0.2 scene.range = 3.8 scene.caption = """Right button drag or Ctrl-drag to rotate \"camera\" to view scene. To zoom, drag with middle button or Alt/Option depressed, or use scroll wheel. On a two-button mouse, middle is left + right. Touch screen: pinch/extend to zoom, swipe or two-finger rotate.\n""" E1 = extrusion(path=[vec(0,0,0), vec(0,0,-0.7)], texture=textures.wood_old, shape=[ shapes.circle(radius=1.5), shapes.triangle(pos=[0,-0.6], length=1.2), shapes.trapezoid(pos=[0,0.6], width=1.6, height=1, top=0.6) ], pos=vec(-4,1.5,0)) copper = vec(0.722,0.451,0.200) E2 = extrusion(path=paths.arc(radius=1.7, angle2=pi), texture=textures.metal, shape=[ [shapes.triangle(length=2), shapes.circle(pos=[0,.5], radius=0.2), shapes.trapezoid(pos=[0,-0.2], width=0.6, height=0.4)], [shapes.rectangle(pos=[0,1.8], width=1,height=0.3)] ], start_face_color=copper, end_face_color=copper) E2center = E2.pos # initial pos of center of extrusion E2.pos = vec(3,2,0) # new pos of center of extrsion E2rot = E2.pos+vec(0,0,-E2center.z) # a location at the front of the extrusion halo = ring(pos=vec(0,0,1), radius=0.8, thickness=0.2, color=color.cyan ) rect = extrusion(path=paths.rectangle(width=5, height=2), shape=shapes.hexagon(length=0.3), color=color.red) bottom = extrusion(path=paths.cross(width=4, thickness=1), shape=shapes.circle(radius=0.2), color=color.green) tube = extrusion(path=[vec(0,0,0), vec(2,0,0)], shape=shapes.circle(radius=0.6, thickness=0.2), pos=vec(-1,1.7,0), axis=vec(0,0,2), color=color.yellow, end_face_color=color.blue) text(pos=tube.pos+vec(0,0,1), text='tube', align='center', height=0.25, depth=0, color=color.blue) ``` -------------------------------- ### Initialize VPython Scene and Caption Source: https://github.com/vpython/vpython-jupyter/blob/master/Demos/Stonehenge.ipynb Sets up the VPython canvas for use in Jupyter notebooks and configures the scene's caption to display user instructions for navigating the surreal environment. It disables default rotation and zoom for a guided experience. ```python import time from vpython import * scene = canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable import time # Bruce Sherwood # A surreal scene that illustrates many of the features of GlowScript # Add instructions below the display s = "Fly through the scene:
" s += " drag the mouse or your finger above or below the center of the scene to move forward or backward;
" s += " drag the mouse or your finger right or left to turn your direction of motion.
" s += "(Normal GlowScript rotate and zoom are turned off in this program.)" scene.caption = s ycenter = 2 scene.width = 800 scene.height = 400 scene.range = 12 scene.center = vector(0,ycenter,0) scene.userspin = False scene.userzoom = False scene.background = color.gray(0.5) scene.ambient = color.gray(0.4) ``` -------------------------------- ### VPython: Setup Scene and Create 3D Objects Source: https://github.com/vpython/vpython-jupyter/blob/master/Demos/VPythonObjects.ipynb Initializes a VPython canvas for use in Jupyter notebooks and demonstrates the creation of various 3D objects such as boxes, spheres, cylinders, cones, and labels. It configures scene properties like title, dimensions, background color, and camera orientation. ```python from vpython import * scene = canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable # Bruce Sherwood, August 2011; GlowScript VPython version November 2014; Jupyter VPython version Feb. 2016 scene.title = "A display of most VPython 3D objects" scene.width = 640 scene.height = 400 #scene.range = 5 scene.background = color.gray(0.7) scene.center = vector(0,0.5,0) scene.forward = vector(-.3,0,-1) gslabel = label(pos=vector(1.1,2,0), text='VPython', xoffset=40, height=16, color=color.yellow) box(pos=vector(-2,0,0), size=vector(.3,2.5,2.5), color=color.red) box(pos=vector(.25,-1.4,0), size=vector(4.8,.3,2.5), color=color.red) cylinder(pos=vector(-2,2,1.25), radius=0.7, axis=vector(0,0,-2.5), color=color.blue) ball = sphere(pos=vector(2,1,0), radius=0.5, color=color.cyan) ptr = arrow(pos=vector(0,0,2), axis=vector(2,0,0), color=color.yellow) cone(pos=vector(-2,0,0), radius=1, length=3, color=color.green, opacity=0.5) ring(pos=vector(.2,0,0), radius=.6, axis=vector(1,0,0), thickness=0.12, color=color.gray(0.4)) ellipsoid(pos=vector(-.3,2,0), color=color.orange, size=vector(.3,1.5,1.5)) pyramid(pos=vector(.3,2,0), color=vector(0,0.5,.25), size=vector(0.8,1.2,1.2)) spring = helix(pos=vector(2,-1.25,0), radius=0.3, axis=vector(0,1.8,0), color=color.orange, thickness=.1) ``` -------------------------------- ### VPython Jupyter Setup and Scene Configuration Source: https://github.com/vpython/vpython-jupyter/blob/master/Demos/Plot3D.ipynb Initializes the VPython canvas for Jupyter environments and configures scene properties like dimensions, center, range, and caption. This setup is crucial for making VPython programs rerunnable in notebooks. ```python from vpython import * scene = canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable scene.width = scene.height = 600 # There is an L by L grid of vertex objects, numbered 0 through L-1 by 0 through L-1. # Only the vertex operators numbered L-2 by L-2 are used to create quads. # The extra row and extra column of vertex objects simplifies edge calculations. # The stride length from y = 0 to y = 1 is L. L = 25 scene.center = vec(0.05*L,0.2*L,0) scene.range = 1.3*L ## The next line contains LaTeX math notation. See http://www.glowscript.org/docs/VPythonDocs/MathJax.html #scene.caption = "\\( f(x,y,t) = 0.7+0.2\\sin{(10x)}\\cos{(10y)}\\cos{(2t)} \)" scene.caption = "f(x,y,t) = 0.7+0.2sin(10x)cos(10y)cos(2t) Click to toggle between pausing or running. In GlowScript programs: Right button drag or Ctrl-drag to rotate "camera" to view scene. To zoom, drag with mid1e button or Alt/Option depressed, or use scroll wheel. On a two-button mouse, mid1e is left + right. Touch screen: pinch/extend to zoom, swipe or two-finger rotate." #MathJax.Hub.Queue ["Typeset",MathJax.Hub]) # format the LaTeX; see http://www.glowscript.org/docs/VPythonDocs/MathJax.html ``` -------------------------------- ### Install jupyterlab-vpython Source: https://github.com/vpython/vpython-jupyter/blob/master/labextension/vpython/README.md Installs the jupyterlab-vpython extension using pip. This is the standard method for adding the extension to your Python environment. ```bash pip install jupyterlab_vpython ``` -------------------------------- ### Python Hard-Sphere Gas Simulation Setup Source: https://github.com/vpython/vpython-jupyter/blob/master/Demos/HardSphereGas.ipynb Initializes a hard-sphere gas simulation using the vpython library. It sets up simulation parameters such as window dimensions and the number of atoms. ```python from vpython import * # Hard-sphere gas. # Bruce Sherwood win = 400 Natoms = 100 # change this to have more or fewer atoms ``` -------------------------------- ### Package jupyterlab-vpython for Distribution Source: https://github.com/vpython/vpython-jupyter/blob/master/labextension/vpython/README.md Details the process of packaging the jupyterlab-vpython extension for distribution. It involves installing the 'build' package and then using it to create source archives (.tar.gz) or wheel packages (.whl). ```bash pip install build python -m build -s python -m build ``` -------------------------------- ### Build Python Binary Wheel Source: https://github.com/vpython/vpython-jupyter/blob/master/RELEASE.md Generates a platform-specific binary wheel distribution for the Python package. This can speed up installation for users on compatible systems. ```shell python setup.py wheel ``` -------------------------------- ### Upload Python Source Distribution (sdist) Source: https://github.com/vpython/vpython-jupyter/blob/master/RELEASE.md Uploads the generated source distribution archive to a package index, typically PyPI. Requires the 'twine' tool to be installed. ```shell twine upload dist/*.tar.gz ``` -------------------------------- ### VPython Jupyter Scene Setup and 3D Object Rendering Source: https://github.com/vpython/vpython-jupyter/blob/master/Demos/Transparency.ipynb This snippet initializes a VPython canvas for use in Jupyter environments, configures scene properties such as dimensions, background, and range, and then renders several 3D objects including boxes and spheres with varying visual attributes. It highlights the `canvas()` initialization required for Jupyter and demonstrates basic object creation and property setting. ```python from vpython import * scene = canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable scene.width = 600 scene.height = 600 scene.background = color.gray(0.9) scene.range = 1.3 s = 'Pixel-level transparency using "depth-peeling." ' s += "Note the correct transparencies of the intersecting slabs." scene.title = s box(pos=vec(0,0,0), opacity=1, size=vec(1,1,1), texture=textures.flower) sphere(pos=vector(0,0,.9), opacity=0.3, shininess=0, radius=0.2, color=color.green) s = sphere(pos=vector(0.1,0,1.2), opacity=0.2, shininess=0, radius=0.1, color=color.cyan) box(pos=s.pos, size=0.06*vector(1,1,1), color=color.gray(.2)) box(pos=vector(0,.5,1), color=color.red, opacity=0.2, size=vector(.05,.2,.8), axis=vector(1,0,1) ) box(pos=vector(0,.5,1), color=color.cyan, opacity=0.2, size=vector(.05,.2,.8), axis=vector(1,0,-1)) scene.caption = """Right button drag or Ctrl-drag to rotate "camera" to view scene. To zoom, drag with middle button or Alt/Option depressed, or use scroll wheel. On a two-button mouse, middle is left + right. Touch screen: pinch/extend to zoom, swipe or two-finger rotate.""" ``` -------------------------------- ### Install VPython from Source Source: https://github.com/vpython/vpython-jupyter/blob/master/README.md Installs VPython from its source code directory using pip with the editable flag. This allows changes in the source code to be reflected without reinstallation. ```bash pip install -e . ``` -------------------------------- ### Build Python Source Distribution (sdist) Source: https://github.com/vpython/vpython-jupyter/blob/master/RELEASE.md Generates the source distribution archive for the Python package. This archive contains the source code and metadata necessary for installation on various platforms. ```shell python setup.py sdist ``` -------------------------------- ### Get Current Hour and Minute Source: https://github.com/vpython/vpython-jupyter/blob/master/Demos/Stonehenge.ipynb A utility function to retrieve the current hour and minute using Python's time module. It formats the hour to a 12-hour clock. ```python def hourminute(): #GlowScript JavaScript date machinery #d = Date() #hour = d.getHours() % 12 #minute = d.getMinutes() now = time.localtime(time.time()) hour = now[3] % 12 minute = now[4] return (hour, minute) ``` -------------------------------- ### Initialize Simulation Environment and Container Source: https://github.com/vpython/vpython-jupyter/blob/master/Demos/HardSphereGas.ipynb Sets up the simulation parameters such as container size, physical constants, and atom properties. It initializes the 3D visualization canvas and draws the boundaries of the cubic container using VPython's curve and vector objects. ```python L = 1 # container is a cube L on a side gray = color.gray(0.7) # color of edges of container mass = 4E-3/6E23 # helium mass Ratom = 0.03 # wildly exaggerated size of helium atom k = 1.4E-23 # Boltzmann constant T = 300 # around room temperature dt = 1E-5 animation = canvas( width=win, height=win, align='left') d = L/2+Ratom r = 0.005 boxbottom = curve(color=gray, radius=r) boxbottom.append(pos=[vector(-d,-d,-d), vector(-d,-d,d), vector(d,-d,d), vector(d,-d,-d), vector(-d,-d,-d)]) boxtop = curve(color=gray, radius=r) boxtop.append([vector(-d,d,-d), vector(-d,d,d), vector(d,d,d), vector(d,d,-d), vector(-d,d,-d)]) vert1 = curve(color=gray, radius=r) vert2 = curve(color=gray, radius=r) vert3 = curve(color=gray, radius=r) vert4 = curve(color=gray, radius=r) vert1.append([vector(-d,-d,-d), vector(-d,d,-d)]) vert2.append([vector(-d,-d,d), vector(-d,d,d)]) vert3.append([vector(d,-d,d), vector(d,d,d)]) vert4.append([vector(d,-d,-d), vector(d,d,-d)]) ``` -------------------------------- ### VPython Lorenz Attractor Visualization Source: https://github.com/vpython/vpython-jupyter/blob/master/Demos/Lorenz.ipynb This Python script uses the VPython library to render and animate the Lorenz differential equation. It includes setup for Jupyter notebooks, drawing a 3D grid, and dynamically appending points to a curve based on the equation's solution, with points colored by the magnitude of the rate of change. ```python from vpython import * scene = canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable from numpy import arange, clip # David Scherer scene.caption = """Right button drag or Ctrl-drag to rotate \"camera\" to view scene. Middle button to drag or Alt-drag to zoom in or out. On a two-button mouse, middle is left + right.""" sene.title = "Lorenz differential equation" scene.center = vector(25,0,0) scene.background = color.white scene.forward = vector(0,-.2,-1) scene.range = 35 lorenz = curve( color = color.black, radius=0.3 ) # Draw grid for x in arange(0,51,10): box(pos=vector(x,0,0), axis=vector(0,0,50), height=0.4, width=0.4, color=color.gray(0.6) ) for z in arange(-25,26,10): box(pos=vector(25,0,z), axis=vector(50,0,0), height=0.4, width=0., color=color.gray(0.6) ) dt = 0.01 y = vector(35, -10, -7) for t in arange(0,10,dt): # Integrate a funny differential equation dydt = vector( - 8.0/3*y.x + y.y*y.z, - 10*y.y + 10*y.z, - y.y*y.x + 28*y.y - y.z ); y = y + dydt*dt # Draw lines colored by speed c = clip( [mag(dydt) * 0.005], 0, 1 )[0] lorenz.append( pos=y, color=vector(c,0, 1-c) ) rate( 500 ) ``` -------------------------------- ### Install VPython with Pip Source: https://github.com/vpython/vpython-jupyter/blob/master/README.md Installs the VPython package using the pip package manager, suitable for users of other Python distributions. ```bash pip install vpython ``` -------------------------------- ### Install VPython with Anaconda Source: https://github.com/vpython/vpython-jupyter/blob/master/README.md Installs the VPython package using the conda package manager, typically for users of the Anaconda Python distribution. ```bash conda install -c vpython vpython ``` -------------------------------- ### VPython Gyroscope Simulation Setup Source: https://github.com/vpython/vpython-jupyter/blob/master/Demos/Gyroscope.ipynb Initializes the VPython scene and defines physical parameters for a gyroscope simulation. Sets up visual elements like the shaft, rotor, and pedestal, and configures scene properties for display. ```python from vpython import * scene = canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable # Gyroscope sitting on a pedestal # The analysis is in terms of Lagrangian mechanics. # The Lagrangian variables are polar angle theta, # azimuthal angle phi, and spin angle psi. scene.width = 800 scene.height = 600 scene.range = 1.2 scene.title = "A precessing, nutating gyroscope" Lshaft = 1 # length of gyroscope shaft r = Lshaft/2 # distance from support to center of mass Rshaft = 0.03 # radius of gyroscope shaft M = 1 # mass of gyroscope (massless shaft) Rrotor = 0.4 # radius of gyroscope rotor Drotor = 0.1 # thickness of gyroscope rotor I3 = 0.5*M*Rrotor**2 # moment of inertia of gyroscope about its own axis I1 = M*r**2 + .5*I3 # moment of inertia about a line through the support, perpendicular to the axis hpedestal = Lshaft # height of pedestal wpedestal = 0.1 # width of pedestal tbase = 0.05 # thickness of base wbase = 3*wpedestal # width of base g = 9.8 Fgrav = vector(0,-M*g,0) top = vector(0,0,0) # top of pedestal shaft = cylinder(length=Lshaft, radius=Rshaft, color=color.orange) rotor = cylinder(pos=vector(Lshaft/2-Drotor/2,0,0), axis=vector(Drotor, 0, 0), radius=Rrotor, color=color.gray(0.9)) base = sphere(color=shaft.color, radius=Rshaft) end = sphere(pos=vector(Lshaft,0,0), color=shaft.color, radius=Rshaft) gyro = compound([shaft, rotor, base, end]) gyro_center = gyro.pos gyro.texture = textures.metal tip = sphere(pos=shaft.axis, radius=shaft.radius/2, make_trail=True, retain=250) tip.trail_color = color.green tip.trail_radius = 0.15*Rshaft pedestal = box(pos=top-vector(0,hpedestal/2+shaft.radius/2,0), height=hpedestal-shaft.radius, length=wpedestal, width=wpedestal, texture=textures.wood) pedestal_base = box(pos=top-vector(0,hpedestal+tbase/2,0), height=tbase, length=wbase, width=wbase, texture=textures.wood) ```