|
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 | | - const model = await super.get(localPath, options); |
24 | | - const isDeepnoteFile = |
25 | | - localPath.endsWith('.deepnote') && model.type === 'notebook'; |
| 11 | + const isDeepnoteFile = localPath.endsWith('.deepnote'); |
26 | 12 |
|
27 | 13 | if (!isDeepnoteFile) { |
28 | 14 | // Not a .deepnote file, return as-is |
29 | | - return model; |
| 15 | + const nonDeepnoteModel = await super.get(localPath, options); |
| 16 | + return nonDeepnoteModel; |
30 | 17 | } |
31 | 18 |
|
32 | | - const validatedModelContent = deepnoteFileFromServerSchema.safeParse( |
33 | | - model.content |
34 | | - ); |
35 | | - |
36 | | - if (!validatedModelContent.success) { |
37 | | - console.error( |
38 | | - 'Invalid .deepnote file content:', |
39 | | - validatedModelContent.error |
40 | | - ); |
41 | | - // Return an empty notebook instead of throwing an error |
42 | | - model.content.cells = []; |
43 | | - return model; |
44 | | - } |
| 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; |
45 | 22 |
|
46 | 23 | // Transform the Deepnote YAML to Jupyter notebook content |
47 | | - const transformedModelContent = |
48 | | - await transformDeepnoteYamlToNotebookContent( |
49 | | - validatedModelContent.data.metadata.deepnote.rawYamlString |
50 | | - ); |
| 24 | + const notebookContent = await transformDeepnoteYamlToNotebookContent( |
| 25 | + modelData.content |
| 26 | + ); |
51 | 27 |
|
52 | | - const transformedModel = { |
53 | | - ...model, |
54 | | - 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 |
55 | 38 | }; |
56 | 39 |
|
57 | | - return transformedModel; |
| 40 | + return model; |
58 | 41 | } |
59 | 42 | } |
0 commit comments