-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add custom API route for .deepnote files #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andyjakubowski
merged 7 commits into
devin/1760105835-add-lint-staged
from
andy/grn-4926-load-backend-extension-config-without-explicit-cli-flag
Oct 15, 2025
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7f654bc
Delete noop assignment
andyjakubowski 87d62f6
Add custom API route for .deepnote files
andyjakubowski 689036e
Update src/deepnote-content-provider.ts
andyjakubowski 83879a8
Add error handling
andyjakubowski 2c44ef3
Validate API response against Zod schema
andyjakubowski 16222c2
Lowercase path in .deepnote file check
andyjakubowski 63b3519
Drop unused exception aliases
andyjakubowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,68 @@ | ||
| from datetime import datetime | ||
| import json | ||
|
|
||
| from jupyter_server.base.handlers import APIHandler | ||
| from jupyter_server.utils import url_path_join | ||
| from jupyter_core.utils import ensure_async | ||
| import tornado | ||
|
|
||
|
|
||
| class RouteHandler(APIHandler): | ||
| # The following decorator should be present on all verb methods (head, get, post, | ||
| # patch, put, delete, options) to ensure only authorized user can request the | ||
| # Jupyter server | ||
| @tornado.web.authenticated | ||
| def get(self): | ||
| self.finish(json.dumps({ | ||
| "data": "This is /jupyterlab-deepnote/get-example endpoint!" | ||
| })) | ||
| async def get(self): | ||
| path = self.get_query_argument("path", default=None) | ||
| if not path: | ||
| self.set_status(400) | ||
| self.set_header("Content-Type", "application/json") | ||
| self.finish( | ||
| json.dumps( | ||
| { | ||
| "code": 400, | ||
| "message": "Missing required 'path' parameter", | ||
| } | ||
| ) | ||
| ) | ||
| return | ||
| try: | ||
| model = await ensure_async( | ||
| self.contents_manager.get( | ||
| path, type="file", format="text", content=True | ||
| ) | ||
| ) | ||
| except FileNotFoundError as e: | ||
| self.set_status(404) | ||
| self.set_header("Content-Type", "application/json") | ||
| self.finish(json.dumps({"code": 404, "message": "File not found"})) | ||
| return | ||
| except PermissionError as e: | ||
| self.set_status(403) | ||
| self.set_header("Content-Type", "application/json") | ||
| self.finish(json.dumps({"code": 403, "message": "Permission denied"})) | ||
| return | ||
| except Exception as e: | ||
| self.log.exception("Error retrieving file") | ||
| self.set_status(500) | ||
| self.set_header("Content-Type", "application/json") | ||
| self.finish(json.dumps({"code": 500, "message": "Internal server error"})) | ||
andyjakubowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return | ||
| # Convert datetimes to strings so JSON can handle them | ||
| for key in ("created", "last_modified"): | ||
| if isinstance(model.get(key), datetime): | ||
| model[key] = model[key].isoformat() | ||
|
|
||
| # Return everything, including YAML content | ||
| result = {"deepnoteFileModel": model} | ||
|
|
||
| self.finish(json.dumps(result)) | ||
andyjakubowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def setup_handlers(web_app): | ||
| host_pattern = ".*$" | ||
|
|
||
| base_url = web_app.settings["base_url"] | ||
| route_pattern = url_path_join(base_url, "jupyterlab-deepnote", "get-example") | ||
| route_pattern = url_path_join(base_url, "jupyterlab-deepnote", "file") | ||
| handlers = [(route_pattern, RouteHandler)] | ||
| web_app.add_handlers(host_pattern, handlers) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,58 @@ | ||
| import { Contents, RestContentProvider } from '@jupyterlab/services'; | ||
| import { z } from 'zod'; | ||
| import { transformDeepnoteYamlToNotebookContent } from './transform-deepnote-yaml-to-notebook-content'; | ||
| import { requestAPI } from './handler'; | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export const deepnoteContentProviderName = 'deepnote-content-provider'; | ||
|
|
||
| const deepnoteFileFromServerSchema = z.object({ | ||
| cells: z.array(z.any()), // or refine further with nbformat | ||
| metadata: z.object({ | ||
| deepnote: z.object({ | ||
| rawYamlString: z.string() | ||
| }) | ||
| }), | ||
| nbformat: z.number(), | ||
| nbformat_minor: z.number() | ||
| }); | ||
|
|
||
| export class DeepnoteContentProvider extends RestContentProvider { | ||
| async get( | ||
| localPath: string, | ||
| options?: Contents.IFetchOptions | ||
| ): Promise<Contents.IModel> { | ||
| const model = await super.get(localPath, options); | ||
| const isDeepnoteFile = | ||
| localPath.endsWith('.deepnote') && model.type === 'notebook'; | ||
| const isDeepnoteFile = localPath.endsWith('.deepnote'); | ||
andyjakubowski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (!isDeepnoteFile) { | ||
| // Not a .deepnote file, return as-is | ||
| return model; | ||
| const nonDeepnoteModel = await super.get(localPath, options); | ||
| return nonDeepnoteModel; | ||
| } | ||
|
|
||
| const validatedModelContent = deepnoteFileFromServerSchema.safeParse( | ||
| model.content | ||
| ); | ||
| // Call custom API route to fetch the Deepnote file content | ||
| let data: any; | ||
|
|
||
| if (!validatedModelContent.success) { | ||
| console.error( | ||
| 'Invalid .deepnote file content:', | ||
| validatedModelContent.error | ||
| try { | ||
| data = await requestAPI<any>( | ||
| `file?path=${encodeURIComponent(localPath)}` | ||
| ); | ||
| // Return an empty notebook instead of throwing an error | ||
| model.content.cells = []; | ||
| return model; | ||
| } catch (error) { | ||
| console.error(`Failed to fetch Deepnote file: ${localPath}`, error); | ||
| throw new Error(`Failed to fetch .deepnote file: ${error}`); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Transform the Deepnote YAML to Jupyter notebook content | ||
| const transformedModelContent = | ||
| await transformDeepnoteYamlToNotebookContent( | ||
| validatedModelContent.data.metadata.deepnote.rawYamlString | ||
| if (!data.deepnoteFileModel) { | ||
| throw new Error( | ||
| `Invalid API response: missing deepnoteFileModel for ${localPath}` | ||
| ); | ||
| } | ||
|
|
||
| const modelData = data.deepnoteFileModel; | ||
|
|
||
| // Transform the Deepnote YAML to Jupyter notebook content | ||
| const notebookContent = await transformDeepnoteYamlToNotebookContent( | ||
| modelData.content | ||
| ); | ||
|
|
||
| const transformedModel = { | ||
| ...model, | ||
| content: transformedModelContent | ||
| const model: Contents.IModel = { | ||
| name: modelData.name, | ||
| path: modelData.path, | ||
| type: 'notebook', | ||
| writable: false, | ||
| created: modelData.created, | ||
| last_modified: modelData.last_modified, | ||
| mimetype: 'application/x-ipynb+json', | ||
| format: 'json', | ||
| content: notebookContent | ||
| }; | ||
|
|
||
| return transformedModel; | ||
| return model; | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.