|
| 1 | +import os |
1 | 2 | import weakref |
2 | 3 |
|
3 | 4 | import ipywidgets as widgets |
@@ -606,3 +607,45 @@ def _choose_merge(*args, **kwargs): |
606 | 607 |
|
607 | 608 | def add_widget(self, widget, label=None, tab=None): |
608 | 609 | 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