Skip to content

Commit d795baf

Browse files
committed
Added initial code to restore a session
1 parent 4220992 commit d795baf

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

glue_jupyter/app.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import weakref
23

34
import ipywidgets as widgets
@@ -606,3 +607,45 @@ def _choose_merge(*args, **kwargs):
606607

607608
def add_widget(self, widget, label=None, tab=None):
608609
pass
610+
611+
@staticmethod
612+
def restore_session(path):
613+
"""
614+
Reload a previously-saved session
615+
616+
Parameters
617+
----------
618+
path : `str`
619+
Path to the file to load.
620+
621+
Returns
622+
-------
623+
app : :class:`Application`
624+
The loaded application.
625+
"""
626+
from glue.core.state import GlueUnSerializer
627+
628+
# In case relative paths are needed in the session file, we do the
629+
# loading while setting the current directory to the directory
630+
# in which the session file is so that relative paths are interpreted
631+
# as relative to the session file.
632+
start_dir = os.path.abspath('.')
633+
session_dir = os.path.dirname(path) or '.'
634+
session_file = os.path.basename(path)
635+
636+
try:
637+
os.chdir(session_dir)
638+
with open(session_file) as infile:
639+
state = GlueUnSerializer.load(infile)
640+
return state.object('__main__')
641+
finally:
642+
os.chdir(start_dir)
643+
644+
@classmethod
645+
def __setgluestate__(cls, rec, context):
646+
self = super(JupyterApplication, cls).__setgluestate__(rec, context)
647+
for tab in rec['viewers']:
648+
for v in tab:
649+
viewer = context.object(v)
650+
self._viewer_refs.append(weakref.ref(viewer))
651+
return self

0 commit comments

Comments
 (0)