1+ import os
2+ import json
13import warnings
24from pathlib import Path
35from typing import TYPE_CHECKING , Dict , Optional , Tuple
46
7+ from glue .core .link_helpers import LinkSame
8+ from glue .core .state import GlueSerializer
59import glue_jupyter as gj
610from glue_jupyter .view import IPyWidgetView
711from glue_jupyter .widgets .layer_options import LayerOptionsWidget
12+
813import y_py as Y
9- from glue . core . link_helpers import LinkSame
14+
1015from IPython .display import display
16+
1117from ipywidgets import Output
18+
1219from jupyter_ydoc import ydocs
1320from ypywidgets import Widget
1421
@@ -140,6 +147,13 @@ def render_viewer(self) -> None:
140147 )
141148
142149 saved_viewer ["widget" ] = widget
150+
151+ # This may be the error widget
152+ if widget is None or not hasattr (widget , "viewer_options" ):
153+ with output :
154+ display (widget )
155+ return
156+
143157 viewer_options = widget .viewer_options
144158 try :
145159 layer_options = LayerOptionsWidget (widget )
@@ -156,6 +170,10 @@ def render_viewer(self) -> None:
156170 viewer_state = state ,
157171 )
158172
173+ # This may be the error widget
174+ if widget is None or not hasattr (widget , "viewer_options" ):
175+ return
176+
159177 viewer_options = widget .viewer_options
160178 try :
161179 layer_options = LayerOptionsWidget (widget )
@@ -309,6 +327,37 @@ def _init_ydoc(self) -> None:
309327 ydoc = self ._sessionYDoc ,
310328 )
311329
330+ def add_data (self , file_path : str ) -> None :
331+ """Add a new data file to the session"""
332+ relative_path = Path (file_path ).relative_to (Path (self ._path ).parent )
333+ assert os .path .exists (relative_path )
334+
335+ data = self .app .load_data (str (relative_path ))
336+ self ._data [data .label ] = data
337+
338+ # We generate the data representation and merge it into our ycontent
339+ serializer = GlueSerializer (data )
340+ serialized_data = serializer .dumpo ()
341+
342+ contents = self ._document .contents
343+
344+ # Inject the main data repr
345+ contents [data .label ] = serialized_data ["__main__" ]
346+ # Inject all components
347+ for key , value in serialized_data .items ():
348+ if key != "__main__" :
349+ contents [key ] = value
350+ # Inject the label in the data collection
351+ contents ["DataCollection" ]["data" ].append (data .label )
352+ contents ["DataCollection" ]["cids" ].extend (
353+ cid for cid , comp in serialized_data ["__main__" ]["components" ]
354+ )
355+ contents ["DataCollection" ]["components" ].extend (
356+ comp for cid , comp in serialized_data ["__main__" ]["components" ]
357+ )
358+
359+ self ._document .set (json .dumps (contents ))
360+
312361 def _load_data (self ) -> None :
313362 """Load data defined in the glue session"""
314363 data_paths = {}
0 commit comments