History: keep track of your work in IPython

This post shows the different ways of keeping track of your work in IPython: the command history, saving the history (or parts of the history to file) for later use, and storing variables so you don’t have to create them next time you use IPython.

History

Note: In the history, you may see _ip.magic().  This is the wrapper used to run magic commands

 

In[5]: The prompt shows the number of the command in the history
In[3] The list, In, contains the history.  So this command shows the third item in the history  (item 0 is a newline) 
Out.keys() The output cache, Out, is a dictionary.  Use this to see the keys in the output cache
_i show the last command
_ii show the second-to-last command
_iii show the third-to-last command (and so
on)
_i10 show the tenth command in history (same as In[10])
exec _i execute the last command
exec _i4 execute the fourth command in the history
hist display the last 20 or so commands
hist 100 display the last 100 commands
hist 3 10 display commands 3 through 10 in the
history
hist -r hide the wrappers around magic commands
macro mac 1-4 7 create a macro called mac out of lines 1, 2, 3, 4, and 7 of the history
mac runs the macro called mac
print mac prints the commands in "c">mac
store mac stores the macro in the profile, it will be available next time you start IPython
store x store x in the profile. It will be loaded next time you start IPython.
store x > /tmp/a.txt store x in the file, a.txt
store -r restore into the workspace the variables that have been stored. Overwrites exisiting variables in workspace.
store -d x delete just x from
storage
store -z remove ALL variables from storage

Logging your session for later use

logstate show state of the logger (on or off)
logstart start logging (default log file is ipython_log.py, in the present working directory
logstart filename store history up to this point, and continue logging history, in filename
logstart -r filename same as above, but use the raw input: don’t put the _ip.magic() wrapper around magic commands
logon start logging after stopping
logoff stop logging after starting
runlog log1 log2 run the log file log1, then run the log file log2 (this executes the logged histories)

0 Responses to “History: keep track of your work in IPython”


  • No Comments

Leave a Reply