diff --git a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py index b5e8a74a4..a5fa558b9 100644 --- a/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py +++ b/python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py @@ -43,11 +43,9 @@ 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. """ - 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, ) @@ -109,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 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..fee6a182c 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,13 @@ 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()) + GISDocument() + assert len(list(tmp_path.iterdir())) == 1 + assert (tmp_path / "untitled.jGIS").is_file() - fn = "test.jgis" - doc.save_as(fn) - assert (tmp_path / fn).is_file() + GISDocument() + assert len(list(tmp_path.iterdir())) == 2 + assert (tmp_path / "untitled1.jGIS").is_file() diff --git a/python/jupytergis_lab/src/notebookrenderer.ts b/python/jupytergis_lab/src/notebookrenderer.ts index cea775ad3..caf0fa19a 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 + const model = await app.serviceManager.contents.newUntitled({ + path: PathExt.dirname(currentWidgetPath), + type: 'file', + ext: '.jGIS' + }); + localPath = model.path; } const sharedModel = drive!.sharedModelFactory.createNew({