Skip to content

Commit 8181441

Browse files
committed
Handle file operations in the front-end
1 parent 33e5184 commit 8181441

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ def __init__(
6363

6464
comm_metadata = GISDocument._path_to_comm(path)
6565

66-
# Create an empty project file if it does not exist
67-
if comm_metadata["path"] and not os.path.isfile(comm_metadata["path"]):
68-
with open(comm_metadata["path"], "w") as fd:
69-
fd.write("{}")
70-
7166
ydoc = Doc()
7267

7368
super().__init__(

python/jupytergis_lab/src/notebookrenderer.ts

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import * as Y from 'yjs';
2929
import {
3030
IJupyterYWidget,
3131
IJupyterYWidgetManager,
32+
// JupyterYDoc,
3233
JupyterYModel
3334
} from 'yjs-widgets';
3435

@@ -135,7 +136,7 @@ export const notebookRendererPlugin: JupyterFrontEndPlugin<void> = {
135136
}
136137

137138
class YJupyterGISModelFactory extends YJupyterGISModel {
138-
ydocFactory(commMetadata: ICommMetadata): Y.Doc {
139+
async ydocFactory(commMetadata: ICommMetadata): Promise<Y.Doc> {
139140
const { path, format, contentType } = commMetadata;
140141
const fileFormat = format as Contents.FileFormat;
141142

@@ -144,11 +145,46 @@ export const notebookRendererPlugin: JupyterFrontEndPlugin<void> = {
144145
'Error using the JupyterGIS Python API',
145146
'You cannot use the JupyterGIS Python API without a collaborative drive. You need to install a package providing collaboration features (e.g. jupyter-collaboration).'
146147
);
147-
throw new Error('Failed to create the YDoc without a collaborative drive');
148+
throw new Error(
149+
'Failed to create the YDoc without a collaborative drive'
150+
);
151+
}
152+
153+
// The path of the project is relative to the path of the notebook
154+
let currentWidgetPath: string = '';
155+
const currentWidget = app.shell.currentWidget;
156+
if (
157+
currentWidget instanceof NotebookPanel ||
158+
currentWidget instanceof ConsolePanel
159+
) {
160+
currentWidgetPath = currentWidget.sessionContext.path;
161+
}
162+
163+
let localPath = '';
164+
let fileless = false;
165+
if (path) {
166+
localPath = PathExt.join(PathExt.dirname(currentWidgetPath), path);
167+
168+
// If the file does not exist yet, create it
169+
try {
170+
await app.serviceManager.contents.get(localPath);
171+
} catch (e) {
172+
await app.serviceManager.contents.save(localPath, {
173+
content: btoa('{}'),
174+
format: 'base64'
175+
});
176+
}
177+
} else {
178+
// If the user did not provide a path, do not create
179+
localPath = PathExt.join(
180+
PathExt.dirname(currentWidgetPath),
181+
'unsaved_project'
182+
);
183+
fileless = true;
148184
}
149185

150186
const sharedModel = drive!.sharedModelFactory.createNew({
151-
path,
187+
path: localPath,
152188
format: fileFormat,
153189
contentType,
154190
collaborative: true
@@ -159,25 +195,10 @@ export const notebookRendererPlugin: JupyterFrontEndPlugin<void> = {
159195

160196
this.jupyterGISModel.contentsManager = app.serviceManager.contents;
161197

162-
if (!sharedModel) {
163-
// The path of the project is set to the path of the notebook, to be able to
164-
// add local geoJSON/shape file in a "file-less" project.
165-
let currentWidgetPath: string | undefined = undefined;
166-
const currentWidget = app.shell.currentWidget;
167-
if (
168-
currentWidget instanceof NotebookPanel ||
169-
currentWidget instanceof ConsolePanel
170-
) {
171-
currentWidgetPath = currentWidget.sessionContext.path;
172-
}
173-
174-
if (currentWidgetPath) {
175-
this.jupyterGISModel.filePath = PathExt.join(
176-
PathExt.dirname(currentWidgetPath),
177-
'unsaved_project'
178-
);
179-
}
198+
if (fileless) {
199+
this.jupyterGISModel.filePath = localPath;
180200
}
201+
181202
return this.jupyterGISModel.sharedModel.ydoc;
182203
}
183204
}

0 commit comments

Comments
 (0)