From ae565573d4a3ecc0087d9ba62d46af9f39416b2a Mon Sep 17 00:00:00 2001 From: David Brochart Date: Fri, 6 Jun 2025 14:49:36 +0200 Subject: [PATCH 1/4] Support transient layers --- python/jupytergis_core/jupytergis_core/jgis_ydoc.py | 3 ++- .../jupytergis_lab/notebook/gis_document.py | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/python/jupytergis_core/jupytergis_core/jgis_ydoc.py b/python/jupytergis_core/jupytergis_core/jgis_ydoc.py index 10ca54912..5622efaaf 100644 --- a/python/jupytergis_core/jupytergis_core/jgis_ydoc.py +++ b/python/jupytergis_core/jupytergis_core/jgis_ydoc.py @@ -26,7 +26,8 @@ def get(self) -> str: :return: Document's content. :rtype: Any """ - layers = self._ylayers.to_py() + # don't save transient layers to disk + layers = {key: val for key, val in self._ylayers.to_py().items() if not val["transient"]} sources = self._ysources.to_py() options = self._yoptions.to_py() meta = self._ymetadata.to_py() diff --git a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py index b0f1d4772..21ccfe5c6 100644 --- a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py +++ b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py @@ -94,7 +94,7 @@ def layers(self) -> Dict: """ Get the layer list """ - return self._layers.to_py() + return {key: val for key, val in self._layers.to_py().items() if not val["transient"]} @property def layer_tree(self) -> List[str | Dict]: @@ -144,6 +144,7 @@ def add_raster_layer( name: str = "Raster Layer", attribution: str = "", opacity: float = 1, + transient: bool = False, ): """ Add a Raster Layer to the document. @@ -174,6 +175,7 @@ def add_raster_layer( "type": LayerType.RasterLayer, "name": name, "visible": True, + "transient": transient, "parameters": {"source": source_id, "opacity": opacity}, } @@ -783,7 +785,7 @@ def _make_comm(cls, *, path: Optional[str]) -> Dict: def to_py(self) -> dict: """Get the document structure as a Python dictionary.""" return { - "layers": self._layers.to_py(), + "layers": {key: val for key, val in self._layers.to_py().items() if not val["transient"]}, "sources": self._sources.to_py(), "layerTree": self._layerTree.to_py(), "options": self._options.to_py(), @@ -861,7 +863,8 @@ def create_layer( ) -> Optional[JGISLayer]: object_type = data.get("type", None) name: str = data.get("name", None) - visible: str = data.get("visible", True) + visible: bool = data.get("visible", True) + transient: bool = data.get("transient", False) filters = data.get("filters", None) if object_type and object_type in self._factories: Model = self._factories[object_type] @@ -874,6 +877,7 @@ def create_layer( parent=parent, name=name, visible=visible, + transient=transient, type=object_type, parameters=obj_params, filters=filters, From 3a7a281b785a426130c4a574ba290ca263eb6a87 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Jun 2025 13:01:06 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- python/jupytergis_core/jupytergis_core/jgis_ydoc.py | 6 +++++- .../jupytergis_lab/notebook/gis_document.py | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/python/jupytergis_core/jupytergis_core/jgis_ydoc.py b/python/jupytergis_core/jupytergis_core/jgis_ydoc.py index 5622efaaf..7d346e8c7 100644 --- a/python/jupytergis_core/jupytergis_core/jgis_ydoc.py +++ b/python/jupytergis_core/jupytergis_core/jgis_ydoc.py @@ -27,7 +27,11 @@ def get(self) -> str: :rtype: Any """ # don't save transient layers to disk - layers = {key: val for key, val in self._ylayers.to_py().items() if not val["transient"]} + layers = { + key: val + for key, val in self._ylayers.to_py().items() + if not val["transient"] + } sources = self._ysources.to_py() options = self._yoptions.to_py() meta = self._ymetadata.to_py() diff --git a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py index 21ccfe5c6..d5490121b 100644 --- a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py +++ b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py @@ -94,7 +94,11 @@ def layers(self) -> Dict: """ Get the layer list """ - return {key: val for key, val in self._layers.to_py().items() if not val["transient"]} + return { + key: val + for key, val in self._layers.to_py().items() + if not val["transient"] + } @property def layer_tree(self) -> List[str | Dict]: @@ -785,7 +789,11 @@ def _make_comm(cls, *, path: Optional[str]) -> Dict: def to_py(self) -> dict: """Get the document structure as a Python dictionary.""" return { - "layers": {key: val for key, val in self._layers.to_py().items() if not val["transient"]}, + "layers": { + key: val + for key, val in self._layers.to_py().items() + if not val["transient"] + }, "sources": self._sources.to_py(), "layerTree": self._layerTree.to_py(), "options": self._options.to_py(), From 40dcbdab6b0df664a4c47445e82efa9dc951d31f Mon Sep 17 00:00:00 2001 From: David Brochart Date: Tue, 10 Jun 2025 11:48:27 +0200 Subject: [PATCH 3/4] Allow a source to be transient too --- .../jupytergis_core/jupytergis_core/jgis_ydoc.py | 14 ++++++++++---- .../jupytergis_lab/notebook/gis_document.py | 16 +++++----------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/python/jupytergis_core/jupytergis_core/jgis_ydoc.py b/python/jupytergis_core/jupytergis_core/jgis_ydoc.py index 7d346e8c7..af156a24c 100644 --- a/python/jupytergis_core/jupytergis_core/jgis_ydoc.py +++ b/python/jupytergis_core/jupytergis_core/jgis_ydoc.py @@ -26,16 +26,22 @@ def get(self) -> str: :return: Document's content. :rtype: Any """ - # don't save transient layers to disk + # don't save transient layers and sources to disk + transient_layers = [key for key, val in self._ylayers.to_py().items() if val["transient"]] + transient_sources = [key for key, val in self._ysources.to_py().items() if val["transient"]] layers = { key: val for key, val in self._ylayers.to_py().items() - if not val["transient"] + if not key in transient_layers + } + sources = { + key: val + for key, val in self._ysources.to_py().items() + if not key in transient_sources } - sources = self._ysources.to_py() options = self._yoptions.to_py() meta = self._ymetadata.to_py() - layers_tree = self._ylayerTree.to_py() + layers_tree = [idx for idx in self._ylayerTree.to_py() if idx not in transient_layers] return json.dumps( dict( schemaVersion=SCHEMA_VERSION, diff --git a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py index d5490121b..4d4bff641 100644 --- a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py +++ b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py @@ -94,11 +94,7 @@ def layers(self) -> Dict: """ Get the layer list """ - return { - key: val - for key, val in self._layers.to_py().items() - if not val["transient"] - } + return self._layers.to_py() @property def layer_tree(self) -> List[str | Dict]: @@ -161,6 +157,7 @@ def add_raster_layer( source = { "type": SourceType.RasterSource, "name": f"{name} Source", + "transient": transient, "parameters": { "url": url, "minZoom": 0, @@ -789,11 +786,7 @@ def _make_comm(cls, *, path: Optional[str]) -> Dict: def to_py(self) -> dict: """Get the document structure as a Python dictionary.""" return { - "layers": { - key: val - for key, val in self._layers.to_py().items() - if not val["transient"] - }, + "layers": self._layers.to_py(), "sources": self._sources.to_py(), "layerTree": self._layerTree.to_py(), "options": self._options.to_py(), @@ -898,6 +891,7 @@ def create_source( ) -> Optional[JGISSource]: object_type = data.get("type", None) name: str = data.get("name", None) + transient: bool = data.get("transient", False) if object_type and object_type in self._factories: Model = self._factories[object_type] args = {} @@ -906,7 +900,7 @@ def create_source( args[field] = params.get(field, None) obj_params = Model(**args) return JGISSource( - parent=parent, name=name, type=object_type, parameters=obj_params + parent=parent, name=name, transient=transient, type=object_type, parameters=obj_params ) return None From c1a56eb31678c00c201f1238ce05adcc3efa776c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Jun 2025 09:51:32 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../jupytergis_core/jupytergis_core/jgis_ydoc.py | 16 +++++++++++----- .../jupytergis_lab/notebook/gis_document.py | 6 +++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/python/jupytergis_core/jupytergis_core/jgis_ydoc.py b/python/jupytergis_core/jupytergis_core/jgis_ydoc.py index af156a24c..5231bba4a 100644 --- a/python/jupytergis_core/jupytergis_core/jgis_ydoc.py +++ b/python/jupytergis_core/jupytergis_core/jgis_ydoc.py @@ -27,21 +27,27 @@ def get(self) -> str: :rtype: Any """ # don't save transient layers and sources to disk - transient_layers = [key for key, val in self._ylayers.to_py().items() if val["transient"]] - transient_sources = [key for key, val in self._ysources.to_py().items() if val["transient"]] + transient_layers = [ + key for key, val in self._ylayers.to_py().items() if val["transient"] + ] + transient_sources = [ + key for key, val in self._ysources.to_py().items() if val["transient"] + ] layers = { key: val for key, val in self._ylayers.to_py().items() - if not key in transient_layers + if key not in transient_layers } sources = { key: val for key, val in self._ysources.to_py().items() - if not key in transient_sources + if key not in transient_sources } options = self._yoptions.to_py() meta = self._ymetadata.to_py() - layers_tree = [idx for idx in self._ylayerTree.to_py() if idx not in transient_layers] + layers_tree = [ + idx for idx in self._ylayerTree.to_py() if idx not in transient_layers + ] return json.dumps( dict( schemaVersion=SCHEMA_VERSION, diff --git a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py index 4d4bff641..a614a9bbb 100644 --- a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py +++ b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py @@ -900,7 +900,11 @@ def create_source( args[field] = params.get(field, None) obj_params = Model(**args) return JGISSource( - parent=parent, name=name, transient=transient, type=object_type, parameters=obj_params + parent=parent, + name=name, + transient=transient, + type=object_type, + parameters=obj_params, ) return None