Jupyter Tips and Tricks

Run Jupyter notebook with GPU support

CoCalc allows you to use basic and advanced GPUs for a few pennies an hour! See Compute Servers for details.

Customize Jupyter notebook LaTeX export settings

See Customize Jupyter notebook LaTeX export settings.

Embed a CoCalc project in your website

Suppose you have an interactive application running in a CoCalc project and you would like to share it. You can share documents publicly and embed them, see Collaborate. Share! Publish!!!.

Embedding the CoCalc interactive website directly in any other website is restricted because it is a security risk (see clickjacking, etc.). Beyond that, users who are not collaborators won’t be able to see the project.

Notebook too large? Remove output.

file-too-large error

Jupyter notebook file too large error

If the size of your notebook exceeds 50 MB, you will not be able to open it in the usual way; instead, you will see the error message shown above. Usually, the problem occurs when the notebook has created large amounts of output. In that case, there is a command you can run from the Linux Terminal to remove output. If removing output results in a small enough notebook, you will be able to open the “-no-output” version of the notebook normally.

# run this from a CoCalc Linux terminal (.term file)
# use the actual name of your notebook for "myfile.ipynb"
cc-jupyter-no-output myfile.ipynb
# the above command creates myfile-no-output.ipynb

If your Jupyter notebook is creating an image file from a plot that exceeds the size limit, here are some things you can do:

  1. If you are using a CoCalc Jupyter notebook (which we generally recommend), it may be possible to open the notebook with JupyterLab server. Once you have the file open, you can modify the code to produce a smaller plot. Then you can go back to using the CoCalc notebook.

  2. Revert the notebook to an earlier version, before the large plot was created. Click the Backups button in the file listing and copy over an earlier version, then modify your code to produce a smaller plot.

  3. The default image file format for plots with the “R (R Project)” Jupyter kernel is SVG. For large plots, smaller files may be produced if the format is set to PNG, because SVG plots (the default) grow in size proportionally to the data they are supposed to show, wherease PNG plots are rasterized, so file size does not have the same proportionality relationship to amount of data. To set image output format in an R Jupyter notebook to PNG, run the following in a compute cell before creating the plot:

    options(jupyter.plot_mimetypes = c('text/plain', 'image/png'))
    

Play a .wav file in a Jupyter notebook

CoCalc Jupyter notebooks now support embedding audio files. There are several ways you can embed an audio file so it plays nicely in a Jupyter notebook:

wav file on disk

  1. Create a file on disk, e.g., tuba.wav. If you’re using simpleaudio or something else to create sound, make sure to figure out how to save that sound to a local file.

  2. Put this in the markdown cell and hit shift+enter:

    <audio controls=true src="tuba.wav"/>
    
  3. You’ll see an embedded audio controller appear and you can play your audio.

audio controls for wav playback

audio controls to play wav file

wav file embedded in notebook

Alternatively, you might want the file to be embedded in the notebook itself. To do this, click the picture icon on the right of a markdown cell, or click “Edit –> Insert images in …”, then drag and drop to copy the wav file as an attachment to that cell. Then replace the image attachment code that is generated by:

<audio controls=true src="attachment:tuba.wav"/>

and again you’ll see a player and can play your file.

Remove Local Files to Troubleshoot a Notebook

If you have a Jupyter notebook that suddenly stops working, especially with extensions or widgets, you can try removing local files in a Linux Terminal, then restarting and running the notebook:

cd
rm -rf .sage .ipython/ .config/ .local/ .jupyter .cache/

You will need to reinstall packages you added locally after doing the above.

Use the Halt Button to Conserve Memory

Each running Jupyter Notebook spawns a session in your project. This uses up memory, which could cause troubles running all your processes in your project.

You can either restart the kernel to clean up its current memory (i.e. all variables are deleted), or if you’ve finished working on that notebook, click the Halt button to stop the kernel and close the notebook.

halt button

protect cells from deletion

You can protect markdown and code cells from deletion in a Jupyter notebook using “Delete protection – toggle whether cells are deletable” in the “Edit” menu. Note that this only blocks deletion using the Jupyter user interface; it is possible using other means to circumvent this.

protecting cells from deletion

protecting cells from being deleted

protect cells from editing

You can prevent editing of markdown and code cells in a Jupyter notebook using “Edit protect – toggle whether cells are editable” in the “Edit” menu. Note that this only blocks editing using the Jupyter user interface; it is possible using other means to circumvent this.

protecting cells from editing

protecting cells from being edited

slideshow

CoCalc notebooks offer you a shortcut for making a slideshow. Select “View” > “Cell Toolbar…” > “Slideshow” to add a Slide button above the right of each cell. For each cell, you can specify whether it is a slide, subslide, or fragment. To view the slideshow, click the “Slideshow” in the “Notebook” menu at the top of a notebook, or select “File” > “Slideshow”, or split the frame and change one of the resulting frames to “Slideshow”. The latter allows you to view the original notebook side-by-side with the slides.

slide button in cell toolbar

enabling “Slide” button in cell toolbar

selecting slide type

selecting slide type for each cell

When presenting, the next slide is to the right, while the next subslide is below. Fragments are revealed within the present slide. Click in the slideshow and then click “?” to see a list of keyboard shortcuts. If you modify the notebook, you can update the slideshow by clicking in the toolbar above the show and clicking “Build”, or by clicking “File” in the toolbar above the notebook and again selecting “Slideshow”.

notebook side-by-side with slideshow

original notebook side by side with slideshow

widgets in CoCalc

Jupyter Widgets are Python objects that let you build interactive GUIs for your Jupyter notebooks. CoCalc Jupyter notebooks combine the interactive capabilities of Jupyter widgets with the usual advanced features of the CoCalc platform, including real-time collaboration, TimeTravel, and Side Chat.

A good way to get started using Jupyter widgets is to go through the Widget List in the main widgets documentation.

notebook with widgets

CoCalc Jupyter notebook with Jupyter Widgets

For more information on the implementation of Jupyter Widgets in Cocalc, including support for real-time collaboration, see this video presentation: Jupyter Widgets in CoCalc.