2017-07-25 2 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対話シェルを実行するIPythonコードを変更することです。

#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() 

時:(私にとっては、これは~/anaconda/lib/python3.6/site-packages/IPython/coreに位置しています)スクリプトinteractiveshell.pyの中で、(これは約2617行です)関数run_cellの先頭に、スクリプトinteractiveshell.pyに次のコードブロックを置きますスクリプトの先頭には、次を置く:あなたはIPythonを使用するよう

from time import gmtime, strftime 

は今、あなたのカスタム履歴ファイルには、Pythonのコマンドおよび情報

で満たされます
関連する問題