Search code examples
pythongithubjupyter-notebookvpython

nbviewer can not display vpython result


If a Jupyter Notebook file is rendering too long in GitHub, it is recommended here to show it in nbviewer, since it is an issue with GitHub backend.

From here, which I do not understand

nbviewer does not execute notebooks. It only renders the inputs and outputs saved in a notebook document as a web page.

I make a notebook importing vpython, which is rendered locally, but can not be rendered in GitHub here nor in nbviewer here.

It also does not work with binder as here.

Is there any solution to display it correctly online?


Solution

  • Your code can be seen to work in a MyBinder-served session with VPython installed

    Go to the VPython-Jupyter repo page here and click on the 'launch binder' badge. A session will launch where VPython is installed and working.

    Make a new cell in the index page that comes up. Now you can copy your code out of your two cells here and paste it in and run it. Here's the code to make that easy:

    from vpython import*
    scene = canvas()
    ball1 = sphere(pos=vector(-5,-3,0), radius=0.5,color=color.blue)
    ball2 = sphere(pos=vector(5,-3,0), radius=0.5,color=color.red)
    ball3 = sphere(pos=vector(0,5,0), radius=0.5,color=color.green)
    

    It will work in that temporary session and you'll see three colored balls. You should be able to use your middle mouse button to zoom in out of the triangle of three spheres.


    Alternative for online VPython: Glowscript / Web VPython

    If you just want to share VPython output separate from Jupyter and Python, there is a Glowscript / Web VPython that allows you to develop and share them.

    Example, here.
    Based on looking at the bottom of the source HTML for that page there is a way to share links without having to sign in.
    I haven't used it.



    Clarifications related to your post:

    How MyBinder knows what to provide you

    The MyBinder service needs to know what to install so it uses configuration files found in the repo you point at it. You cannot just go to MyBinder.org and spin up a vanilla session and expect complex things like VPython to work. Most code uses one or a few extra packages to work and those need to be specified so they environment in the session matches your needs. Your repo at https://github.com/dudung/py-jupyter-nb has no configuration files and so it is going to be the basic, vanilla set up that binder makes. Essentially just Python connected to Jupyter notebook interface and JupyterLab. It's not going to run VPython.

    How could you make it so you can share your code and the session will have VPython installed? I suspect if you made a file in the root of your repository named requirements.txt and put the contents of the VPython repo 'requirements.txt' file in there. (Or just copy that file to your repo.) Then launches from https://mybinder.org/v2/gh/dudung/py-jupyter-nb/main?filepath=src%2Fimport%2Fexternal%2Fvpython%2Fbasic%2Fsphere_3_static.ipynb might work.

    Note that I say 'suspect' and 'might'. I haven't tried it recently myself, and so I cannot be certain. There's a lot of moving pieces involved and so sometimes one part has a minor bug that makes the whole process not work as expected.

    There's also Glowscript / Web VPython that should allow working and sharing online, see section above.

    How to share MyBinder launch URLs

    Don't share the links to a session because they won't work later. You want to share the launch URL. It will have https://mybinder.org/v2/ near the start of it, such as:

    https://mybinder.org/v2/gh/vpython/vpython-jupyter/7.6.1?filepath=index.ipynb
    

    You shared in your post:

    https://hub.gke2.mybinder.org/user/dudung-py-jupyter-nb-qnn1mtpg/doc/tree/src/import/external/vpython/basic/sphere_3.ipynb
    

    (Try putting that in your browser and you'll see what others trying it see.).
    You may be able to note the difference at the start now that I gave you some idea about the pattern.

    The other big signal that your link was to a temporary is that qnn1mtpg tag in there. That's part of the access code for it. You'd not want to normally share that but the session is temporary and so it is already gone like several minutes after you no longer use it anyway. (I actually put the launch URL for your repo and notebook in the section 'How MyBinder knows what to provide you'; it's https://mybinder.org/v2/gh/dudung/py-jupyter-nb/main?filepath=src%2Fimport%2Fexternal%2Fvpython%2Fbasic%2Fsphere_3_static.ipynb )