2017-07-25 157 views
0

我想定制IPython保存历史记录的方式。 IPython创建一个包含类型命令历史的sqlite3数据库,但我想对其进行自定义,以便我可以保存其他信息,例如执行代码行时的当前工作目录。这方面的一个表想这样的:使用自定义信息配置IPython(即Jupyter)历史记录

Command  Directory 
import sys  /Users/home 
import os  /Users/home 
os.chdir("~/data") /Users/home 
data = open("input_file", 'r').read() /Users/home/data 

另外,我想有作为被执行的每一行这个工作(而不是保存为在会议结束手动)。

有谁知道如何做到这一点? IPython似乎没有可配置的选项,允许您将自定义信息添加到我可以在其文档中找到的历史记录中,或者查看IPython中config HistoryManager的可用选项

回答

0

经过大量工作后,我找到了答案。关键是修改运行IPython交互式shell的IPython代码。在该脚本interactiveshell.py(对我来说,这个位于~/anaconda/lib/python3.6/site-packages/IPython/core),把下面的代码块在脚本interactiveshell.py,在功能run_cell一开始(这大约是线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 those lines with just 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() 

在该脚本的顶部,把下面的:

from time import gmtime, strftime 

现在,当您使用IPython中,您的自定义历史文件将充满蟒蛇命令和信息