Is there any way to force IPython not to save the current session's history to the history file? A similar question has been asked about not saving a single command, but it's not practical to do that individually for every command in a session.
The HistoryManager.enabled setting enables or disables history management. You can set it to False on the command line to disable history for that session:
ipython --HistoryManager.enabled=False
Alternatively, to maintain history in memory only, without saving to a history file, you can set HistoryManager.hist_file=:memory:
ipython --HistoryManager.hist_file=:memory:
#user2357112 answer's although is very useful has an issue: running IPython that way won't let you use your history at all! E.g. you can't search and use what you did before like declaring a function! So I find an approach that offers best of both worlds. You can access other sessions' histories and the current one won't save it in DB, just run this as the first command in your session:
get_ipython().history_manager.enabled = False
Related
I have been editing a Jupyter Notebook for the past week, and tried saving it today. While attempting to save it, I got an error, so I refreshed the page and successfully saved it.
However, to my dismay almost all of my command history was lost! I still have access to the variables (the kernel never died), but I don't have access to any of the code.
Is there any way to recover the code? The kernel is still running, but I do not see any checkpoints in my notebook.
You can get the IPython history, which Jupyter Notebook uses as its kernel. In one of the cells run:
%history
This will dump a history (good, bad, and ugly) of every command you have run in the current IPython session. It is probably more than you want, but it is better than losing all your work.
Something similar happened to me where I could not save what I did, refreshed the page, and lost all my commands.
You can get your entire notebook history and redirect the output to any given file by running
%history -g -f filename
In my case, I redirected it to a (.py) file. You can find more documentation here https://ipython.readthedocs.io/en/stable/interactive/magics.html under "%history".
If your kernel hasn't restarted you can get your code from globals like _i{1,2,3,...}.
The simplest way to find your code is to run something like
print([k for k, v in globals.items() if 'some unique code snippet' in str(v)])
Say you got something like ['_i2', '_i5', '_i9'], then you can
print(_i9)
and copypaste it to a new cell.
Within an interactive pyspark session you can import python files via sc.addPyFile('file_location'). If you need to make changes to that file and save them, is there any way to "re-broadcast" the updated file without having to shut down your spark session and start a new one?
Simply adding the file again doesn't work. I'm not sure if renaming the file works, but I don't want to do that anyways.
As far as I can tell from the spark documentation there is only a method to add a pyfile, not update one. I'm hoping that I missed something!
Thanks
I don't think it's feasible during an interactive session. You will have to restart your session to use the modified module.
I have successfully achieved this using the method documented at Run IPython Notebook in Iframe from another Domain . However, this required editing the user config file. I was really hoping to be able to set this up via the command-line instead (for reasons).
http://ipython.org/ipython-doc/1/config/overview.html indicates that configuration via the command line is possible. However, all the examples are for simple true/false value assignment. To set the server up to allow embedding, it is necessary to set a value inside a dictionary. I can't work out how to pass a dictionary in through the command-line.
Another acceptable option would be a configuration overrides file.
Some people will wonder -- why all this trouble!?!
First of all, this isn't for production. I'm trying to support non-developers by writing a web-based application which integrates Ipython notebooks within it using iframes. Despite being on the same machine, it appears that the different port number used is enough to mean that I can't do simple iframe embedding without setting the x-frame insecurity bit.
Being able to do this via the command line lets me set the behaviour in the launch script rather than having to bundle a special configuration file inside my app, and also write an installer.
I really hope I've make the question clear enough! Thanks for any and all suggestions and help!
Looking over the IPython source for the loaders, it seems like it will execute whatever python code you put on the right hand side. I've not tested it, but based on the link you provided, you can probably pass something like
--NotebookApp.webapp_settings=dict('headers'=dict('X-Frame-Options'='ALLOW-FROM https://example.com/'))
This question already has answers here:
How to save a Python session, including input and output, as a text?
(4 answers)
Closed 1 year ago.
All of the ways which discussed this question save the history of your commands in a file or you have to use an IDE ,I am using vim with python-mode (no mouse using) what I would like to do is to save my session as code I wrote and the python output ,So I dont have to use paper and pen to write my input and python output all what I have to do is to print out my session , I tried the code (.pystartup)
and it only save my input and I tried to redirect the output to a file and it only save the python output , is there a way to have both in one file ready to be printed out .
I really cherish vim, it'S a fantastic piece of work. Nevertheless, your requirements are easier with other tools.
Probably the best choice, in my oppinion, would be to use the ipython notebook. It offers really rich features, including graphics with mpl and much more, and for me is the perfect tool for "reproducible experiments". The full state of a notebook can be saved to disk, reloaded, exported, printed etc.
You should really give it a try.
The old "I use $EDITOR instead of $IDE because I don't want to use the mouse" bullshit… IDEs have shortcuts for everything and they all allow you to customize them to your liking. Learn them all and forget about your mouse.
Anyway, what you want is neither an editor nor an IDE; you want a REPL like bpython or ipython (possibly with its notebook feature mentioned above). Both tools allow you to save and restore your sessions and are far better at "getting" your python code than Vim.
I've been using iPython (0.13.2) more frequently lately, and the logging seems like a great feature -- if I could get it to work right.
Currently, I'm able to start and specify a log file either through ipython --logfile ~/path/fake.log, or even in the middle of an iPython session with the %magic command %logstart ~/path/fake.log.
However, I can't seem to resume the session from the logfile, which seems to defeat the purpose in part. I've scoured Google and SO, but none of the recommended solutions here at SO or in the docs seem to work quite right.
I have tried:
from Bash
ipython -log ~/path/fake.log (source, result: [TerminalIPythonApp] Unrecognized flag: '-log')
ipython -logplay ~/path/fake.log (source and numerous others, result: [TerminalIPythonApp] Unrecognized flag: '-logplay')
ipython --logfile=~/path/fake.log (source, result: new log started, variables from previous session undefined)
from iPython
%logstart ~/path/fake.log append (source, result: old log continued but not loaded, variables from previous session undefined)
Two that are partially working (in that they try to import the logfile) but don't seem to be intended for this purpose are:
from bash: ipython -i ~/path/fake.log (source, result: if there were no errors in the session imports and works. If there were any errors, not imported and variables still unavailable. Logging not resumed.).
from ipython: %run ~/path/fake.log (no source, just guessed and tried. Result: same as above. Runs the file if there were no errors and variables are GTG. If there were errors, does not work. Does not resume logging.)
Is there any way in iPython 0.13.2 to resume a session that effectively "starts where you left off"? Thanks for any help you can provide.
All these save/restore sessions work by saving the interactions as py files and then trying to run the py file during restore. If an error like undefined variable happens, that prompts a python error and restore halts there, but it does restore values stored upto the error condition.
To avoid storing error conditions, use the suggestion at chosen answer of How to save a Python interactive session? :
save my_session_name 1-4 6 9
Where my session will get the commands in In[1] through In[4] and skip In[5], save In[6], skip In[7], In[8] and save In[9]. This way you avoid offending interactions.
Restore the session later:
%run my_session_name.py