Save ipython session

Asked by user1827356

I want to save my ipython session (input/output etc upto 500 Mb) into a file and have it reloaded into the buffer on ipython restart. IPython has a few recording mechanisms which do not add up to what would be useful to me.

http://stackoverflow.com/questions/15842991/ipython-rolling-log

I figured it should be possible to save the file as described above like this

(write-region (max (- (point-max) 500000) 1) (point-max) "/tmp/test.txt")

I can have this run with a timer as well. But I do not know how I can have it loaded (not executed) in the ipython buffer on startup

Question information

Language:
English Edit question
Status:
Solved
For:
python-mode.el Edit question
Assignee:
No assignee Edit question
Solved by:
user1827356
Solved:
Last query:
Last reply:
Revision history for this message
Andreas Roehler (a-roehler) said :
#1

Citing the answer of Thoms Kluyver:

IPython already logs your input - it's stored in history.sqlite in your profile folder (run ipython locate profile to see where that is). To turn on output logging as well, edit ipython_config.py and search for 'db_log_output'. This captures output resulting from the displayhook (with the Out [n]: prompt), not printed output.

To look at history from a previous session, you can use %hist ~1/1-10 (lines 1-10 of the session before the current one). It also works with magic commands like %rerun, %recall and %save.

If you want it recorded to a text file, have a look at the %logstart magic.

Revision history for this message
user1827356 (vn1861) said :
#2

This is the problem I have with the solution/ipython features available

If ipython were used as a 'system' scripting terminal like bash, then info(commands) >> info(output). So storing cmd(s) and having them easily recallable and re-executable (%hist, %rerun, %recall and %save) makes sense.

But if you want to use it as a 'data processing' terminal like R/matlab (which is how I use it) info(commands) ~ info(output). I have no particular use for a command without the associated output and vice-versa. In fact, what I want when I restart my work after day/week end is to recollect where I was in the course of solving a problem. Looking at command history alone is not useful for this.

Having a logfile (with %logstart) is useful, but the logfile grows to large proportions over a week of work. Hence, I need it to be 'rolling'. AFAIK there is no way to accomplish this.

I also need the file saved on a timer as there are instances when ipython misbehaves and needs to be killed

I figured, it might just be easier getting emacs to do this. As I mentioned I'm able to save *IPython* buffer to a file on timer and can load it back on ipython startup (I load the saved file, rename buffer to *IPython* and then call ipython)

However there are 2 problems I'm facing are
1. ipython attempts to execute buffer contents on startup
2. ipython doesn't seem to recognize the In[xx] and Out[xx] terminal prompts as 'special' text

Revision history for this message
Andreas Roehler (a-roehler) said :
#3

Am 08.04.2013 18:56, schrieb user1827356:
> Question #226203 on python-mode.el changed:
> https://answers.launchpad.net/python-mode/+question/226203
>
> Status: Answered => Open
>
> user1827356 is still having a problem:
> This is the problem I have with the solution/ipython features available
>
> If ipython were used as a 'system' scripting terminal like bash, then
> info(commands) >> info(output). So storing cmd(s) and having them easily
> recallable and re-executable (%hist, %rerun, %recall and %save) makes
> sense.
>
> But if you want to use it as a 'data processing' terminal like R/matlab
> (which is how I use it) info(commands) ~ info(output). I have no
> particular use for a command without the associated output and vice-
> versa. In fact, what I want when I restart my work after day/week end is
> to recollect where I was in the course of solving a problem. Looking at
> command history alone is not useful for this.
>
> Having a logfile (with %logstart) is useful, but the logfile grows to
> large proportions over a week of work. Hence, I need it to be 'rolling'.
> AFAIK there is no way to accomplish this.
>
> I also need the file saved on a timer as there are instances when
> ipython misbehaves and needs to be killed
>
> I figured, it might just be easier getting emacs to do this.

That's possible, depending from your case.

Basically, you may store everything you have sent and send it again at startup,

Have a look at py-send-file, py-execute-file-ipython etc.

   As I
> mentioned I'm able to save *IPython* buffer to a file on timer and can
> load it back on ipython startup (I load the saved file, rename buffer to
> *IPython* and then call ipython)
>
> However there are 2 problems I'm facing are
> 1. ipython attempts to execute buffer contents on startup
> 2. ipython doesn't seem to recognize the In[xx] and Out[xx] terminal prompts as 'special' text
>

Using IPython's logging should give you some clean code which to send with the commands mentioned.
Alternatively use Python itself, Emacs or sed to clean-up your storage.

Andreas

Revision history for this message
user1827356 (vn1861) said :
#4

Andreas, thanks for the quick response. I want to clarify that I don't need the file 're-executed'. I just want the contents to appear as 'older' content in the *IPython* buffer

Revision history for this message
Thomas Caswell (tcaswell-gmail) said :
#5

The problem with this is that you will need to serialize the _entire_
local python workspace to be able to re-construct like you want.
Pickle will do a decent job on _most_ things, but not everything can
be pickled (matplotlib figrures for example).

That is, it is easy to get back the commands you executed (and hence
can re-construct exactly what you did), you can not get back a snap
shot of all of the data and results you have stored.

If you want to be able to dump and reload something this you will need
to write a protocol for doing so and manage it your self.

Tom

On Mon, Apr 8, 2013 at 12:55 PM, user1827356
<email address hidden> wrote:
> Question #226203 on python-mode.el changed:
> https://answers.launchpad.net/python-mode/+question/226203
>
> Status: Answered => Open
>
> user1827356 is still having a problem:
> Andreas, thanks for the quick response. I want to clarify that I don't
> need the file 're-executed'. I just want the contents to appear as
> 'older' content in the *IPython* buffer
>
> --
> You received this question notification because you are a member of
> python-mode.el developers, which is an answer contact for python-
> mode.el.

--
Thomas Caswell
<email address hidden>

Revision history for this message
user1827356 (vn1861) said :
#6

Thomas, I don't need the data to be restored. I can understand how this could get complicated.

I only want to restore the the 'logged' output i.e. buffer state (not process state). This will give me two advantages:

1. I can scroll through and get an overview of the direction in which I was working (after a break)
2. I can search through the buffer to find commands and view their outputs (Don't really care for graphs, which I don't inline anyway)

Revision history for this message
Andreas Roehler (a-roehler) said :
#7

Don't understand for what reasons you need this stuff in your comint IPython buffer. You may open it with any buffer, have a look, scroll, copy commands from there.

If sure, you need this inside the *IPython* buffer, M-x insert-file RET YOUR-FILE should fill saved stuff in.
However, doesn't look recommendable that way...

Revision history for this message
user1827356 (vn1861) said :
#8

Andreas, if I open the history file in different buffer, I would have to switch buffers to search. Also have to copy/paste commands instead of a simple 'return' key

Doing this in emacs is definitely not my first choice. Looks like ipython 1.0 might have some features that will help accomplish what I need (refer stackoverflow question in original mail). I'm going to proceed in that direction

Thanks, for all the responses