Auto insert %%time in beginning of all Jupyter notebook cell? - jupyter-notebook

I would like to automatically insert the statement %%time in beginning of every Jupyter cell.
How can I do that?

You could use execute_time Jupyter Notebook extension to view the time taken in execution for every cell. Read the instructions here.

Related

how to go about inserting a table into Markdown Jupyter Notebook

I have a table of about 40 rows that I want to enter into a Jupyter notebook, I know the usual method but it's gonna take time to write down. Is there a more efficient way?

Control print to cell in Jupyter Notebook

Is there any way or magic word that can direct the code to print to specific location (output below the cells or console). I'm imagining something like below.
The reason I'm asking is, I have a sets of asynchronous co-routine started running from jupypter notebook, which constantly print some output to show the result which I want them constantly showing in the console. Meanwhile I want to using jupyter notebook to see some of object for debugging, and I only want to print some values in the cell output.
%print_here
print('hello')
%print_to_console
print('hello')

jupiter notebook and editors

While working with ipython I used to edit an object with:
ed my_obj
And the editor opened the code defining the class of the object. I cannot find how to do the same thing in a jupyter notebook. Is it any possible?
The magic command %ed and %edit won't work in jupyter notebook.
I'm not quite sure why exactly jupyter doesn't support this edition, but probably because it can't know exactly when the user finished editing the file and the edited data should back to the cell.
If you want to edit the content of a string in a cell, you can use the following command:
a = 'jupyter'
get_ipython().set_next_input(a)
You can also use the %%writefile to save the content of the cell into a file and edit it outside jupyter.
%%writefile test.txt
[1, 2, 3]
And also use %load to bring the content to the cell:
%load test.txt

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?
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.
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.
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.
Now you generate a simple script linked to the notebook with jupytext which others can rerun.
If you need to keep the images within (because, for example, you are sharing the notebook with someone who does not want to/can not rerun it) you might want to try to reduce the images.
I found this module ipynbcompress which seems to do exactly this, but so far I could not install it.

How to load a notebook without the outputs?

I mistakenly printed to much to the output during a single cell's execution and now the browser tab completely freezes every time that notebook is opened. I tried restarting ipython and it didn't help (I am guessing that each time it is loaded, also all the chunk of text is loaded with it).
Is there a way to load a notebook with outputs suspended or clear?
One hack if you're desperate: open the .ipynb file, which is a text file. Scroll down to the lengthy cell output and delete it. Of course, you need to be careful that the result is still a valid .ipynb file.
nbstripout is a simple tool that removes all output from a notebook (without needing to open the notebook in your browser).
your code will be saved in the form of JSON. open it with json viewer and carefully delete the unwanted output cell and save it back.

Resources