|
3 | 3 | import warnings |
4 | 4 | from pathlib import Path |
5 | 5 | from typing import TYPE_CHECKING, Dict, Optional, Tuple |
6 | | - |
7 | 6 | from glue.core.link_helpers import LinkSame |
8 | 7 | from glue.core.state import GlueSerializer |
9 | 8 | import glue_jupyter as gj |
@@ -356,29 +355,40 @@ def add_data(self, file_path: str) -> None: |
356 | 355 | assert os.path.exists(relative_path) |
357 | 356 |
|
358 | 357 | data = self.app.load_data(str(relative_path)) |
359 | | - self._data[data.label] = data |
360 | 358 |
|
361 | | - # We generate the data representation and merge it into our ycontent |
362 | | - serializer = GlueSerializer(data) |
363 | | - serialized_data = serializer.dumpo() |
| 359 | + # Serialize the data on its own (without other context). |
| 360 | + data_serializer = GlueSerializer(data) |
| 361 | + data_serializer.dumpo() |
| 362 | + |
| 363 | + # Serialize the previous data without the new one to create a context. |
| 364 | + previous_data_serializer = GlueSerializer(self._data) |
| 365 | + previous_data_serializer.dumpo() |
| 366 | + |
| 367 | + # We generate the data representation in the previous data context. |
| 368 | + serialized_data = [ |
| 369 | + (previous_data_serializer.id(obj), previous_data_serializer.do(obj)) |
| 370 | + for oid, obj in list(data_serializer._objs.items()) |
| 371 | + ] |
| 372 | + serialized_data = dict(serialized_data) |
364 | 373 |
|
365 | 374 | contents = self._document.contents |
366 | 375 |
|
367 | | - # Inject the main data repr |
368 | | - contents[data.label] = serialized_data["__main__"] |
369 | 376 | # Inject all components |
370 | 377 | for key, value in serialized_data.items(): |
371 | 378 | if key != "__main__": |
372 | 379 | contents[key] = value |
| 380 | + |
373 | 381 | # Inject the label in the data collection |
374 | | - contents["DataCollection"]["data"].append(data.label) |
375 | | - contents["DataCollection"]["cids"].extend( |
376 | | - cid for cid, comp in serialized_data["__main__"]["components"] |
| 382 | + data_collection_name: str = contents.get("__main__", {}).get("data", "") |
| 383 | + contents[data_collection_name]["data"].append(data.label) |
| 384 | + contents[data_collection_name]["cids"].extend( |
| 385 | + cid for cid, comp in serialized_data[data.label]["components"] |
377 | 386 | ) |
378 | | - contents["DataCollection"]["components"].extend( |
379 | | - comp for cid, comp in serialized_data["__main__"]["components"] |
| 387 | + contents[data_collection_name]["components"].extend( |
| 388 | + comp for cid, comp in serialized_data[data.label]["components"] |
380 | 389 | ) |
381 | 390 |
|
| 391 | + self._data[data.label] = data |
382 | 392 | self._document.set(json.dumps(contents)) |
383 | 393 |
|
384 | 394 | def _load_data(self) -> None: |
|
0 commit comments