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 the classical jupyter server or JupyterLab. 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