ipython: print complete history (not just current session)

in ipython, I can use %hist or %history to print recent history, but this only prints history from current session.

I would like to print all history, similar as bash historycommand does.

What I am trying to do is to get complete history, so that I can then search with regex, see what commands followed after specified commands, and so on

Speaking of history, can it also print timecodes?

SIDE NOTE: In bash, I have written a simple script which prints history and I can grep it for keywords. I see times when certain commands were executed. I can specify -A n or -B n, where n is a number of lines AFTER or BEFORE a given command. This is very handy, because I can easily find what I did when, and what followed, ...

I am looking for something similar for ipython


Solution 1:

In ipython enter:

%history -g

It does not print time codes but it does print session/line number.

Solution 2:

First use %hist -o -g -f ipython_history.md to output the history (input and output) to a text file. (http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-history)

Then you can use the the get_session_info function to retreive the date and time for the session you are interested in.(http://ipython.readthedocs.io/en/stable/api/generated/IPython.core.history.html?highlight=hist#IPython.core.history.HistoryAccessor.get_session_info)

profile_hist=IPython.core.history.HistoryAccessor(profile='default')
profile_hist.get_session_info(100)

This will print out something like

(100, datetime.datetime(2018, 2, 13, 19, 8, 30, 40691), None, None, '')

This means that the session 100 started on the 13th of Feb 2018 19:08:30.

Solution 3:

Here's the Firefox extension that @larssend found: SQLite Manager

It has a GUI for opening a database file and issuing various sqlite commands from a menu. You get the added bonus of seeing the SQL commands that generated the output. Here's mine for my ipython %history in ~/.ipython/profile_default/history.sqlite:

enter image description here

And it even has a menu for generating plots (scatter, line, bar, etc) from your data!