From fab541941b54dde0ae3ad61c79b694d633fd4739 Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Mon, 14 Apr 2025 12:28:05 -0600 Subject: [PATCH 1/6] Create GISDocuments as untitled when no path provided --- .../jupytergis_lab/notebook/gis_document.py | 15 ++++++--------- python/jupytergis_lab/src/notebookrenderer.ts | 12 +++++++----- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py index b5e8a74a4..f987e223a 100644 --- a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py +++ b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py @@ -46,8 +46,6 @@ class GISDocument(CommWidget): :param path: the path to the file that you would like to open. If not provided, a new empty document will be created. """ - path: Optional[Path] - def __init__( self, path: Optional[str | Path] = None, @@ -59,17 +57,16 @@ def __init__( pitch: Optional[float] = None, projection: Optional[str] = None, ): - if isinstance(path, str): - path = Path(path) - - self.path = path - - comm_metadata = GISDocument._path_to_comm(str(self.path) if self.path else None) + if isinstance(path, Path): + path = str(path) ydoc = Doc() super().__init__( - comm_metadata=dict(ymodel_name="@jupytergis:widget", **comm_metadata), + comm_metadata={ + "ymodel_name": "@jupytergis:widget", + **self._path_to_comm(path), + }, ydoc=ydoc, ) diff --git a/python/jupytergis_lab/src/notebookrenderer.ts b/python/jupytergis_lab/src/notebookrenderer.ts index cea775ad3..0295e6b58 100644 --- a/python/jupytergis_lab/src/notebookrenderer.ts +++ b/python/jupytergis_lab/src/notebookrenderer.ts @@ -171,11 +171,13 @@ export const notebookRendererPlugin: JupyterFrontEndPlugin = { }); } } else { - // If the user did not provide a path, do not create - localPath = PathExt.join( - PathExt.dirname(currentWidgetPath), - 'unsaved_project' - ); + // If the user did not provide a path, create an untitled document + let model = await app.serviceManager.contents.newUntitled({ + path: PathExt.dirname(currentWidgetPath), + type: 'file', + ext: '.jGIS' + }); + localPath = model.path; } const sharedModel = drive!.sharedModelFactory.createNew({ From 4786734d39d051284bf5f99336d60a4cd05890b7 Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Mon, 14 Apr 2025 12:31:19 -0600 Subject: [PATCH 2/6] Remove `save_as()` method from GISDocument API, re-add `export_to_qgis()` We now create untitled documents when no path is passed in, so users can use the JupyterLab facilities to rename the document. --- .../jupytergis_lab/notebook/gis_document.py | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py index f987e223a..72b9ed2e3 100644 --- a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py +++ b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py @@ -106,23 +106,7 @@ def layer_tree(self) -> List[str | Dict]: """ return self._layerTree.to_py() - def save_as(self, path: str | Path) -> None: - """Save the document at a new path.""" - if isinstance(path, str): - path = Path(path) - - if path.name.lower().endswith(".qgz"): - _export_to_qgis(path) - self.path = path - return - - if not path.name.lower().endswith(".jgis"): - path = Path(str(path) + ".jGIS") - - path.write_text(json.dumps(self.to_py())) - self.path = path - - def _export_to_qgis(self, path: str | Path) -> bool: + def export_to_qgis(self, path: str | Path) -> bool: # Lazy import, jupytergis_qgis of qgis may not be installed from jupytergis_qgis.qgis_loader import export_project_to_qgis From a7ca2466bc63a764920f1c6d97a292517d16a3f6 Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Mon, 14 Apr 2025 12:40:52 -0600 Subject: [PATCH 3/6] Prefer `const` --- python/jupytergis_lab/src/notebookrenderer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/jupytergis_lab/src/notebookrenderer.ts b/python/jupytergis_lab/src/notebookrenderer.ts index 0295e6b58..caf0fa19a 100644 --- a/python/jupytergis_lab/src/notebookrenderer.ts +++ b/python/jupytergis_lab/src/notebookrenderer.ts @@ -172,7 +172,7 @@ export const notebookRendererPlugin: JupyterFrontEndPlugin = { } } else { // If the user did not provide a path, create an untitled document - let model = await app.serviceManager.contents.newUntitled({ + const model = await app.serviceManager.contents.newUntitled({ path: PathExt.dirname(currentWidgetPath), type: 'file', ext: '.jGIS' From 7ffe1f8a317bdf72ba7a7f77ee7c33ec3596b290 Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Mon, 14 Apr 2025 13:04:07 -0600 Subject: [PATCH 4/6] Fix unit test --- .../jupytergis_lab/notebook/tests/test_api.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/python/jupytergis_lab/jupytergis_lab/notebook/tests/test_api.py b/python/jupytergis_lab/jupytergis_lab/notebook/tests/test_api.py index 8694d4af9..5f759efc3 100644 --- a/python/jupytergis_lab/jupytergis_lab/notebook/tests/test_api.py +++ b/python/jupytergis_lab/jupytergis_lab/notebook/tests/test_api.py @@ -46,12 +46,11 @@ def test_remove_nonexistent_layer_raises(self): self.doc.remove_layer("foo") -def test_save_as(tmp_path): +def test_untitled_doc(tmp_path): os.chdir(tmp_path) doc = GISDocument() - assert not list(tmp_path.iterdir()) + assert len(list(tmp_path.iterdir())) == 1 - fn = "test.jgis" - doc.save_as(fn) + fn = "untitled.jGIS" assert (tmp_path / fn).is_file() From a46b478f44e3a0aca58cec9bf71e786a1fe310a0 Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Mon, 14 Apr 2025 13:05:09 -0600 Subject: [PATCH 5/6] Test untitled document numbering --- .../jupytergis_lab/notebook/tests/test_api.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/python/jupytergis_lab/jupytergis_lab/notebook/tests/test_api.py b/python/jupytergis_lab/jupytergis_lab/notebook/tests/test_api.py index 5f759efc3..fee6a182c 100644 --- a/python/jupytergis_lab/jupytergis_lab/notebook/tests/test_api.py +++ b/python/jupytergis_lab/jupytergis_lab/notebook/tests/test_api.py @@ -49,8 +49,10 @@ def test_remove_nonexistent_layer_raises(self): def test_untitled_doc(tmp_path): os.chdir(tmp_path) - doc = GISDocument() + GISDocument() assert len(list(tmp_path.iterdir())) == 1 + assert (tmp_path / "untitled.jGIS").is_file() - fn = "untitled.jGIS" - assert (tmp_path / fn).is_file() + GISDocument() + assert len(list(tmp_path.iterdir())) == 2 + assert (tmp_path / "untitled1.jGIS").is_file() From 78b13e10abd668012ae56f138ec89617725a6d3b Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Mon, 14 Apr 2025 13:09:56 -0600 Subject: [PATCH 6/6] Update docstring --- python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py index 72b9ed2e3..a5fa558b9 100644 --- a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py +++ b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py @@ -43,7 +43,7 @@ class GISDocument(CommWidget): """ Create a new GISDocument object. - :param path: the path to the file that you would like to open. If not provided, a new empty document will be created. + :param path: the path to the file that you would like to open. If not provided, a new untitled document will be created. """ def __init__(