Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reduce the file size of my iPython notebook?

I have an IPython notebook which is several megabytes big although the code inside is just about 100 lines. I think it is that huge because I load several images inside.

I would like to add this notebook to a git repository. However, I don't want to upload something that big which can easily be generated again.

Is it possible to save just the code of an IPython notebook to reduce its size?

like image 737
Martin Thoma Avatar asked Jun 14 '16 08:06

Martin Thoma


People also ask

What is the IPython Notebook extension?

ipynb file extension is used for computational notebooks that can be open with Jupyter Notebook. The Jupyter Notebook was formerly named IPython Notebook. The extension *. ipynb is from letters IPython Notebook.

How do I open IPython Notebook file?

you can open it using the command ipython notebook filename. ipynb from the directory it is downloaded on to. If you are on a newer machine, open the file as jupyter notebook filename.

Where are Jupyter files stored in Windows?

On Linux and other free desktop platforms, these runtime files are stored in $XDG_RUNTIME_DIR/jupyter by default. On other platforms, it's a runtime/ subdirectory of the user's data directory (second row of the table above). An environment variable may also be used to set the runtime directory.


2 Answers

You can try following steps since it worked for me:

Select the "Cell" -> then select "All Outputs" -> There you will find "Clear" option select that.

enter image description here
And then save the file.

This will reduce the size of your file (From MBs to kbs). It will also reduce the time to load the notebook next time you open it in your browser.

As per my understanding this will clear all the output created after execution of the code. Since Notebook is holding code+images+comments in addition to this its also holding the out put in that file therefore it will increase the size of the notebook.

like image 193
Yogesh Awdhut Gadade Avatar answered Sep 20 '22 14:09

Yogesh Awdhut Gadade


I run into the exact same problem with one of my notebooks, which I solved by changing my df to df.head(5). I did this instead of clearing all outputs as I still wanted to show on GitHub how my code changed data inside the columns in my df.

You also can run !ls -lh in the last cell of your notebook to check size of your notebook before saving. This will give you an idea if you need to clear outputs/replace df with df.head()/remove images in order to reduce the size and be able to save on the GitHub.

like image 37
The smell of roses Avatar answered Sep 18 '22 14:09

The smell of roses