Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save workspace in IPython

Tags:

Is it possible to save an IPython workspace (defined functions, different kinds of variables, etc) so that it can be loaded later?

This would be a similar function to save.image() in MATLAB or R. Similar questions has been asked before, such as:

Save session in IPython like in MATLAB?

However, since a few years passed, I am wondering if there is a good solution now.

like image 389
qkhhly Avatar asked Sep 29 '14 18:09

qkhhly


People also ask

How do I save my IPython session?

In XWindows, run iPython from the Xfce terminal app. click Terminal in the top menu bar and look for save contents in the dropdown.

Can you save the state of a Jupyter Notebook?

Meet Dill. Dill enables you to save the state of a Jupyter Notebook session and restore it with a single command. Dill also works with python interpreter sessions.

Does Jupyter Notebook save variables?

A practical example of how you can store variables in Jupyter Notebook Database. With the storemagic command %store we can store variables in IPython's database. More particularly, the variables are stored under ~/. ipython/profile_default/db/autorestore/<variable_name> .

How do I set an environment variable in IPython?

Environment variables environ directly, you may like to use the magic %env command. With no arguments, this displays all environment variables and values. To get the value of a specific variable, use %env var . To set the value of a specific variable, use %env foo bar , %env foo=bar .


2 Answers

You can use the dill python package:

import dill                             filepath = 'session.pkl' dill.dump_session(filepath) # Save the session dill.load_session(filepath) # Load the session 

To install it:

pip install dill 
like image 190
Franck Dernoncourt Avatar answered Sep 23 '22 06:09

Franck Dernoncourt


EDIT: this answer (and gist) has been modified to work for IPython 6

I added a somewhat ad-hoc solution that automates the process of storing/restoring user space variables using the underlying code from IPython's %store magic which is from what I understand what you wanted. See the gist here. Note that this only works for objects that can be pickled.

I can't guarantee its robustness, especially if any of the autorestore mechanisms in IPython change in the future, but it has been working for me with IPython 2.1.0. Hopefully this will at least point you in the right direction.

To reiterate the solution here:

  1. Add the save_user_variables.py script below to your ipython folder (by default $HOME/.ipython). This script takes care of saving user variables on exit.
  2. Add this line to your profile's ipython startup script (e.g., $HOME/.ipython/profile_default/startup/startup.py):

    get_ipython().ex("import save_user_variables;del save_user_variables")

  3. In your ipython profile config file (by default $HOME/.ipython/profile_default/ipython_config.py) find the following line:

    # c.StoreMagics.autorestore = False

    Uncomment it and set it to true. This automatically reloads stored variables on startup. Alternatively you can reload the last session manually using %store -r.

save_user_variables.py

def get_response(quest,default=None,opts=('y','n'),please=None,fmt=None):     try:         raw_input = input     except NameError:         pass     quest += " ("     quest += "/".join(['['+o+']' if o==default else o for o in opts])     quest += "): "      if default is not None: opts = list(opts)+['']     if please is None: please = quest     if fmt is None: fmt = lambda x: x      rin = input(quest)     while fmt(rin) not in opts: rin = input(please)      return default if default is not None and rin == '' else fmt(rin)  def get_user_vars():     """     Get variables in user namespace (ripped directly from ipython namespace     magic code)     """     import IPython     ip = IPython.get_ipython()         user_ns = ip.user_ns     user_ns_hidden = ip.user_ns_hidden     nonmatching = object()     var_hist = [ i for i in user_ns                  if not i.startswith('_') \                  and (user_ns[i] is not user_ns_hidden.get(i, nonmatching)) ]     return var_hist  def shutdown_logger():     """     Prompts for saving the current session during shutdown     """     import IPython, pickle     var_hist = get_user_vars()     ip = IPython.get_ipython()     db = ip.db      # collect any variables that need to be deleted from db     keys = map(lambda x: x.split('/')[1], db.keys('autorestore/*'))     todel = set(keys).difference(ip.user_ns)     changed = [db[k] != ip.user_ns[k.split('/')[1]]                for k in db.keys('autorestore/*') if k.split('/')[1] in ip.user_ns]      try:         if len(var_hist) == 0 and len(todel) == 0 and not any(changed): return         if get_response("Save session?", 'n', fmt=str.lower) == 'n': return     except KeyboardInterrupt:         return      # Save interactive variables (ignore unsaveable ones)     for name in var_hist:         obj = ip.user_ns[name]         try:             db[ 'autorestore/' + name ] = obj         except pickle.PicklingError:             print("Could not store variable '%s'. Skipping..." % name)             del db[ 'autorestore/' + name ]      # Remove any previously stored variables that were deleted in this session     for k in todel:         del db['autorestore/'+k]  import atexit atexit.register(shutdown_logger) del atexit 
like image 21
toes Avatar answered Sep 25 '22 06:09

toes