How can I get my images and videos to render without the user having to run all cells?

For my educational project, I am inspired by this notebook series: https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python

When I open those notebooks on Binder, all the cells are run, I see all the plots.

But in my notebook, where I have a link to a video for example, the video only shows up when I run the cell. On opening the notebooks I just see the code of the cell. But I want my potential students to only see the embedded video.

from IPython.display import YouTubeVideo
YouTubeVideo('roqumrTB9g0')

How can I make my notebooks behave like the ‘Kalman-and-Bayesian’ one?

Run the cell once and save the resulting notebook. It’ll have the display outputs persisted in the document without the kernel having to recompute them. For displaying outputs only there’s some options but not all interfaces support them. e.g. nbextensions using these metadata keywords

1 Like

I’m pasting a screenshot here of what I’m seeing when I open my notebook in Binder. I did run the cells, save, commit, push to my repo, launch from binder link. Simple code output does persist, but not video.

The output is blank. I need to run the cell again to see the clickable video thumbnail.

I do have a workaround which is to display the video from markdown when clicking on a thumbnail image, but that requires me to store the thumbnail image of the video

Have you tried viewing the notebook via nbviewer ? (I would have tried but couldn’t find your repo from you user name and screenshot was devoid of information.) Here makes me think that will work (direct link to the notebook rendered via nbviewer with video embedded). I imagine for security or performance, when you open the active notebook in ‘default’ mode via Binder, you have to opt in to video play ability by running the cell. It is this way for some plot software, too. For example, the javascript based Plotly. But they render just fine via nbviewer. Example: https://nbviewer.jupyter.org/github/fomightez/3Dscatter_plot-binder/blob/master/Plotly3d-scatter-plots.ipynb . By contrast, if you go to https://github.com/fomightez/3Dscatter_plot-binder and launch Binder and then go into that notebook, you need to run the cells again to see the plots (unless you mark the notebook containing the plot as trusted, see example here).

Have you considered using Voila via Binder, if you are trying to not require students to run code to see output? It freshly runs the code upon launch. I haven’t tried video in Voila, though. I see that indeed there is a video widget, see here. The example Voila pages launched from the Voila gallery are rendered via MyBinder as well.

Just saw this reply, thank-you haven’t tried those things, as I just now figured out that I need a postBuild file and I can put stuff there like enabling extensions, so I added jupyter trust … and it worked!!
I noticed that the metadata for the cell said ‘trusted: false’ in the Binder rendered notebook, but it was trusted in my local version.
I don’t understand how this works, and why 2*3 is run without trust but displaying a video is not, but happy I found a cleaner solution than my previous horrible markdown hack.

1 Like

Glad you’ve found trusting is way to go to get what you need. (I wonder if that works for Plotly plots, too? --> UPDATE: it indeed does, see example here.)

Just to clarify something…
It isn’t re-running 2*3 when you launch a session and open your previously saved notebook via MyBinder. Just displaying the previously made output. Certain output isn’t handled upon reopening. I am assuming something did get saved in the output cell from the original run since it seems to make a space for when it isn’t trusted but you’d have to verify by looking at the output for that cell while viewing the notebook in a text editor. For example, .png images displayed in an output cell via from IPython.display import Image get saved as Base64 code upon saving the Jupyter notebook and you’ll see the image rendered when you reopen it. I don’t know what from video gets saved as in an output cell.
The notebook code is not re-run upon opening in general. That is why you can see most output instantly when opening the notebook on MyBinder.org. Voila can re-run code before it renders the output though.

Just as another handy trick, the init_cell notebook extension looks like it can be used to autorun selected cells on notebook load.

1 Like

BTW the trusted code aspect is to protect your browser from running arbitrary javascript without your knowledge. Basically it’s to prevent someone sending you a notebook that tries to infect your computer unknowingly by embeding dangerous javascript in the outputs. This is why 2*3 is trusted by default but a video output is not when executed elsewhere and reshared, because the former has no javascript needed to render.

1 Like