|
1 | 1 | import { Contents, RestContentProvider } from '@jupyterlab/services'; |
2 | | -import { z } from 'zod'; |
3 | 2 | import { transformDeepnoteYamlToNotebookContent } from './transform-deepnote-yaml-to-notebook-content'; |
| 3 | +import { requestAPI } from './handler'; |
4 | 4 |
|
5 | 5 | export const deepnoteContentProviderName = 'deepnote-content-provider'; |
6 | | - |
7 | | -const deepnoteFileFromServerSchema = z.object({ |
8 | | - cells: z.array(z.any()), // or refine further with nbformat |
9 | | - metadata: z.object({ |
10 | | - deepnote: z.object({ |
11 | | - rawYamlString: z.string() |
12 | | - }) |
13 | | - }), |
14 | | - nbformat: z.number(), |
15 | | - nbformat_minor: z.number() |
16 | | -}); |
17 | | - |
18 | 6 | export class DeepnoteContentProvider extends RestContentProvider { |
19 | 7 | async get( |
20 | 8 | localPath: string, |
21 | 9 | options?: Contents.IFetchOptions |
22 | 10 | ): Promise<Contents.IModel> { |
23 | 11 | const isDeepnoteFile = localPath.endsWith('.deepnote'); |
24 | 12 |
|
25 | | - if (isDeepnoteFile) { |
26 | | - await super.get(`resolve?path=${localPath}`, options); |
27 | | - } |
28 | | - |
29 | | - const model = await super.get(localPath, options); |
30 | | - |
31 | 13 | if (!isDeepnoteFile) { |
32 | 14 | // Not a .deepnote file, return as-is |
33 | | - return model; |
| 15 | + const nonDeepnoteModel = await super.get(localPath, options); |
| 16 | + return nonDeepnoteModel; |
34 | 17 | } |
35 | 18 |
|
36 | | - const validatedModelContent = deepnoteFileFromServerSchema.safeParse( |
37 | | - model.content |
38 | | - ); |
39 | | - |
40 | | - if (!validatedModelContent.success) { |
41 | | - console.error( |
42 | | - 'Invalid .deepnote file content:', |
43 | | - validatedModelContent.error |
44 | | - ); |
45 | | - // Return an empty notebook instead of throwing an error |
46 | | - model.content.cells = []; |
47 | | - return model; |
48 | | - } |
| 19 | + // Call custom API route to fetch the Deepnote file content |
| 20 | + const data = await requestAPI<any>(`file?path=${localPath}`); |
| 21 | + const modelData = data.deepnoteFileModel; |
49 | 22 |
|
50 | 23 | // Transform the Deepnote YAML to Jupyter notebook content |
51 | | - const transformedModelContent = |
52 | | - await transformDeepnoteYamlToNotebookContent( |
53 | | - validatedModelContent.data.metadata.deepnote.rawYamlString |
54 | | - ); |
| 24 | + const notebookContent = await transformDeepnoteYamlToNotebookContent( |
| 25 | + modelData.content |
| 26 | + ); |
55 | 27 |
|
56 | | - const transformedModel = { |
57 | | - ...model, |
58 | | - content: transformedModelContent |
| 28 | + const model: Contents.IModel = { |
| 29 | + name: modelData.name, |
| 30 | + path: modelData.path, |
| 31 | + type: 'notebook', |
| 32 | + writable: false, |
| 33 | + created: modelData.created, |
| 34 | + last_modified: modelData.last_modified, |
| 35 | + mimetype: 'application/x-ipynb+json', |
| 36 | + format: 'json', |
| 37 | + content: notebookContent |
59 | 38 | }; |
60 | 39 |
|
61 | | - return transformedModel; |
| 40 | + return model; |
62 | 41 | } |
63 | 42 | } |
0 commit comments