Package: jupyter-save-load-vars
jupyter-save-load-varsSaves and loads variables, typically to and from anIPython/Jupyterinteractive notebook or other interactive workspace.dillis often used forsaving and loading from python notebooksbut it fails for objects that cannot be pickled, e.g. hardware objects or generators. It also requires users to wrap thedill.dump()in a with open(file): call and does not handle restoring the variables to the workspace from the returned data fromdill.load(). _jupyter-save-load-vars_ is an attempt to make this process as simple as possible.Usagejupyter-save-load-varsis available frompypiand can be installed from a terminal withpipinstalljupyter-save-load-varsThe pypi version may not be the lastest version.In your code:fromjupyter_save_load_varsimportsavevars,loadvars# Note the use of _ for importsavevars()# saves all local variables to a default filename in local folderloadvars()# loads them back into workspacejupyter-save-load-varssupplies two functionssavevars(filename=_DEFAULT_FILENAME, vars=None, overwrite='prompt')finds all local variables, excludes In and Out and any variable that starts with '_' and just skips objects that cannot be picked.By default all variables are saved.varsspecifies which variables are saved.varsis either:an optional list of strings, e.g.vars=['a','b']that are saved.a single unix-type wildcard string, e.g.vars='a*'saves all variables starting with the letter a.overwritecan be 'prompt' (the default), 'yes' orTrue(to silently overwrite), or 'no' orFalseto not overwrite existing data file.loadvars(filename=_DEFAULT_FILENAME, overwrite='prompt', warn=True)loads the variables back into the workspace.overwritecan be 'prompt' (the default), 'yes' orTrue(to silently overwrite), or 'no' orFalseto not overwrite existing variables.warn=Truewarns about dangers of loading data from pickles once a day; setwarn=Falseto suppress this warning.The filename has.dillappended if no suffix is provided.WarningLiike any unpickling operation, users should notloadvarsfrom any file whose provenance is unknown. Users are warned once every 24h about this danger.Examples:fromjupyter_save_load_varsimportsavevars,loadvars,printvarsa=1b=[2,3]c='string'o=(iforiin[])# make generator that cannot be pickled# define a classclassMyClass:name=""l=[1,2,3]cl=MyClass()# create object with this classimportfunctools,sysprint=functools.partial(print,flush=True,file=sys.stderr)# flush and put print() on stderr so logging comes in sequenceimporttimeprintvars()# printvars() function is also supplied to print the local variablestime.sleep(3)print('saving variables')savevars('testvars')print('deleting b,c')delb,cprint('loading variables with prompt (default)')loadvars('testvars')# load with prompt for overwriting existing a variable# loadvars(filename,warn=False) # suppresses warning about unsafe unpicklingprint('loading variables with no overwrite')loadvars('testvars',overwrite='no')print('loading variables with overwrite')loadvars('testvars',overwrite='yes')print([a,b,c,cl])Output:variables: a,b,c,o,cl,
saving variables
file testvars.dill already exists, overwrite it?  [Yes/no/always]: 
[INFO]: 2023-10-05 20:25:32,410 - saveloadvars - saved to testvars.dill variables [ a b c cl ] (File "F:/tobi/Dropbox (Personal)/GitHub/tobidelbruck/jupyter-save-load-vars/jupyter_save_load_vars.py", line 141, in savevars)
[WARNING]: 2023-10-05 20:25:32,410 - saveloadvars - could not pickle: ['o'] (File "F:/tobi/Dropbox (Personal)/GitHub/tobidelbruck/jupyter-save-load-vars/jupyter_save_load_vars.py", line 143, in savevars)
deleting b,c
loading variables with prompt (default)
[INFO]: 2023-10-05 20:25:32,412 - saveloadvars - from testvars.dill loaded variables ['a', 'b', 'c', 'cl'] (File "F:/tobi/Dropbox (Personal)/GitHub/tobidelbruck/jupyter-save-load-vars/jupyter_save_load_vars.py", line 198, in loadvars)
Overwrite existing variable "a" ?  [Yes/no/always]: a
[INFO]: 2023-10-05 20:25:37,640 - saveloadvars - overwrote existing variables ['a', 'cl'] (File "F:/tobi/Dropbox (Personal)/GitHub/tobidelbruck/jupyter-save-load-vars/jupyter_save_load_vars.py", line 232, in loadvars)
loading variables with no overwrite
[INFO]: 2023-10-05 20:25:37,641 - saveloadvars - from testvars.dill loaded variables ['a', 'b', 'c', 'cl'] (File "F:/tobi/Dropbox (Personal)/GitHub/tobidelbruck/jupyter-save-load-vars/jupyter_save_load_vars.py", line 198, in loadvars)
[INFO]: 2023-10-05 20:25:37,641 - saveloadvars - did not overwrite existing variables ['a', 'b', 'c', 'cl'] (File "F:/tobi/Dropbox (Personal)/GitHub/tobidelbruck/jupyter-save-load-vars/jupyter_save_load_vars.py", line 234, in loadvars)
loading variables with overwrite
[INFO]: 2023-10-05 20:25:37,641 - saveloadvars - from testvars.dill loaded variables ['a', 'b', 'c', 'cl'] (File "F:/tobi/Dropbox (Personal)/GitHub/tobidelbruck/jupyter-save-load-vars/jupyter_save_load_vars.py", line 198, in loadvars)
[INFO]: 2023-10-05 20:25:37,641 - saveloadvars - overwrote existing variables ['a', 'b', 'c', 'cl'] (File "F:/tobi/Dropbox (Personal)/GitHub/tobidelbruck/jupyter-save-load-vars/jupyter_save_load_vars.py", line 232, in loadvars)
[1, [2, 3], 'string', <__main__.MyClass object at 0x0000023A8AD03FA0>]