Jupyter Tips and Tricks
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 Shared Files.
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.
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:
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.
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.
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'))
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
Create a file on disk, e.g.,
tuba.wav. If you’re using
simpleaudioor something else to create sound, make sure to figure out how to save that sound to a local file.
Put this in the markdown cell and hit shift+enter:
<audio controls=true src="tuba.wav"/>
You’ll see an embedded audio controller appear and you can play your audio.
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.
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.