python - Configure IPython (i.e. Jupyter) History with Custom Information -


i want customize way ipython saves history. ipython creates sqlite3 database contains history of typed commands, want customize allow me save additional information, such current working directory when line of code executed. table of like:

command       directory import sys     /users/home import os      /users/home os.chdir("~/data")    /users/home data = open("input_file", 'r').read()   /users/home/data 

also, want have done each line executed (as opposed saving @ end of session manually).

does know how this? ipython not seem have configurable options allow add custom information history find on documentation, or looking @ option available in config historymanager within ipython

after playing around, figured out. key modify ipython code runs ipython interactive shell. within script interactiveshell.py (for me, located in ~/anaconda/lib/python3.6/site-packages/ipython/core), put following block of code in script interactiveshell.py, @ beginning of function run_cell (this approximately lines 2617):

#location of command cwd=os.getcwd()  file_1=open("~/.custom_history_file.txt", "a") #time of command showtime = strftime("%y-%m-%d %h:%m:%s", gmtime()) showtime=showtime.rstrip() #ignore lines empty spaces if raw_cell.strip():     print (raw_cell,file=file_1,sep="",end="")      print ("#command info:\t","cwd: ",cwd,"\tdate: ",showtime,file=file_1)     #print("\n",file=file_1)     #print (raw_cell,cwd,sep="\t",file=file_1)  file_1.close() 

at top of script, put following:

from time import gmtime, strftime 

now use ipython, custom history file filled python commands , information


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -