How to write IPython history to a text file?

After some searching and trawling through IPython documentation and some code , I can’t understand if it is possible to save the command history (and not the output log) to a text file , not a SQLite database. ipython --help-all seems to indicate that this parameter does not exist.

This would be very good for versioning frequently used commands, such as in .bash_history .

Edit : Working solution based on @minrk answer.

+11
source share
4 answers

You can emulate the behavior of bash by adding this to one of the startup scripts (e.g. $(ipython locate profile)/startup/log_history.py :

 import atexit import os ip = get_ipython() LIMIT = 1000 # limit the size of the history def save_history(): """save the IPython history to a plaintext file""" histfile = os.path.join(ip.profile_dir.location, "history.txt") print("Saving plaintext history to %s" % histfile) lines = [] # get previous lines # this is only necessary because we truncate the history, # otherwise we chould just open with mode='a' if os.path.exists(histfile): with open(histfile, 'r') as f: lines = f.readlines() # add any new lines from this session lines.extend(record[2] + '\n' for record in ip.history_manager.get_range()) with open(histfile, 'w') as f: # limit to LIMIT entries f.writelines(lines[-LIMIT:]) # do the save at exit atexit.register(save_history) 

Note that this emulates the behavior of the bash / readline history in that it will fail if the interpreter fails, etc.

in fact

update: alternative

If what you really want is to have only a few hand-held favorite commands available for reading (completion, ^ search R, etc.) that you can control versions of, this boot file allows you to save this file is on its own, which is purely in addition to the actual history of IPython commands:

 import os ip = get_ipython() favfile = "readline_favorites" def load_readline_favorites(): """load profile_dir/readline_favorites into the readline history""" path = os.path.join(ip.profile_dir.location, favfile) if not os.path.exists(path): return with open(path) as f: for line in f: ip.readline.add_history(line.rstrip('\n')) if ip.has_readline: load_readline_favorites() 

Put this in your profile_default/startup/ directory and edit profile_default/readline_favorites , or anywhere else to save this file, and it will appear at the end of readline, etc. in every IPython session.

+5
source

You can export your entire story in IPython to a text file like this.

 %history -g -f filename 

One way to get what you want is to do this export in a git hook . Usually I do these “synchronize external resource” actions in the post-checkout git method.

+31
source

Using a clean text file to store history will result in the alternation of commands from different sessions, as well as overly complex and slow addition of features such as "run the third command from session-x" or search the history. Hence the sqlite database,

However, it should be fairly easy to write the history of dumping the script into a file and make a stat at the same time. Everything you do with a text file should be done using sqlite.

0
source

You can also choose which lines you want to keep. for example

 %history 1 7-8 10 -f /tmp/bar.py 

This will save lines 1, 7–8, and 10 to the temporary bar.py file. If you need an integer, just skip part of the line.

 %history -f /tmp/foo.py 
0
source

All Articles