Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I save ipython command line history to a notebook file?

I was using iPython command line interface and after some operations I want to save my operation history to a notebook file. But I was not using iPython notebook from the beginning. Can I still make it?

like image 327
Yan Yang Avatar asked Jul 18 '16 12:07

Yan Yang


2 Answers

From @Thomas K (I don't know why he didn't post an answer):

%notebook -e myhistory.ipynb
like image 199
kristianp Avatar answered Jan 01 '23 22:01

kristianp


The short answer is in a couple of ways, the slightly longer answer is Yes - but you might not get what you expect!

Really long answer: The explanation is that when you are working in a notebook, now called a jupyter notebook of course, your work is stored in a series of cells each of which has one or more lines of code or markdown while when you are working in a console all of your work is a series of lines of python code.

From within a console session you can save, using %save some or all of your work to one or more python files that you can then paste, import, etc, into notebook cells. You can also save using %save -r to .ipy files your work including the magics as magics rather than the results of magics that again you can use from within your notebook later.

You can also use the %notebook magic to save all of your current history in one of an ipynb json file or a python .py text file with the -e export flag. However, it is not clear from the documentation if the history will end up in a single cell, one cell per command or some other division. A little testing suggests one cell per numbered line of your console, so a single command or definition, per cell.

Personally I will stick with outputting anything useful into python files using the %save command - or better yet start a notebook when I think I might be doing something that I would need later.

like image 37
Steve Barnes Avatar answered Jan 01 '23 23:01

Steve Barnes