Skip to content

Commit bff074f

Browse files
Conditionally render toolbar item
Signed-off-by: Andy Jakubowski <hello@andyjakubowski.com>
1 parent 2d166eb commit bff074f

File tree

6 files changed

+48
-32
lines changed

6 files changed

+48
-32
lines changed

.copier-answers.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ _src_path: https://github.com/jupyterlab/extension-template
44
author_email: andy@deepnote.com
55
author_name: Andy Jakubowski
66
has_binder: false
7-
has_settings: false
7+
has_settings: true
88
kind: frontend-and-server
99
labextension_name: jupyterlab-deepnote
1010
project_short_description: A Deepnote extension for JupyterLab

jupyterlab_deepnote/contents.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def get(self, path, content=True, type=None, format=None, require_hash=False):
6363
model["type"] = "notebook"
6464
model["format"] = "json"
6565
model["content"] = nb_node
66+
model["writable"] = False
6667
self.mark_trusted_cells(nb_node, path)
6768
self.validate_notebook_model(model, validation_error={})
6869

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"files": [
2020
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
2121
"style/**/*.{css,js,eot,gif,html,jpg,json,png,svg,woff2,ttf}",
22-
"src/**/*.{ts,tsx}"
22+
"src/**/*.{ts,tsx}",
23+
"schema/*.json"
2324
],
2425
"main": "lib/index.js",
2526
"types": "lib/index.d.ts",
@@ -59,7 +60,9 @@
5960
"@jupyterlab/application": "^4.0.0",
6061
"@jupyterlab/coreutils": "^6.0.0",
6162
"@jupyterlab/notebook": "^4.4.7",
62-
"@jupyterlab/services": "^7.0.0"
63+
"@jupyterlab/services": "^7.0.0",
64+
"@jupyterlab/settingregistry": "^4.0.0",
65+
"@lumino/widgets": "^2.7.1"
6366
},
6467
"devDependencies": {
6568
"@jupyterlab/builder": "^4.0.0",
@@ -109,7 +112,8 @@
109112
}
110113
},
111114
"extension": true,
112-
"outputDir": "jupyterlab_deepnote/labextension"
115+
"outputDir": "jupyterlab_deepnote/labextension",
116+
"schemaDir": "schema"
113117
},
114118
"eslintIgnore": [
115119
"node_modules",

schema/plugin.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"jupyter.lab.shortcuts": [],
3+
"jupyter.lab.toolbars": {
4+
"Notebook": [{ "name": "deepnote:switch-notebook", "rank": 1 }]
5+
},
6+
"title": "jupyterlab-deepnote",
7+
"description": "jupyterlab-deepnote settings.",
8+
"type": "object",
9+
"properties": {},
10+
"additionalProperties": false
11+
}

src/index.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@ import {
33
JupyterFrontEndPlugin
44
} from '@jupyterlab/application';
55

6-
import { NotebookPanel, NotebookWidgetFactory } from '@jupyterlab/notebook';
7-
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
8-
import { IEditorServices } from '@jupyterlab/codeeditor';
9-
10-
const factoryName = 'Deepnote Notebook';
6+
import { IToolbarWidgetRegistry, ToolbarButton } from '@jupyterlab/apputils';
7+
import { NotebookPanel } from '@jupyterlab/notebook';
8+
import { Widget } from '@lumino/widgets';
119

1210
const plugin: JupyterFrontEndPlugin<void> = {
1311
id: 'jupyterlab-deepnote:plugin',
1412
description: 'Open .deepnote files as notebooks.',
1513
autoStart: true,
16-
requires: [IRenderMimeRegistry, IEditorServices],
17-
activate: (
18-
app: JupyterFrontEnd,
19-
rendermime: IRenderMimeRegistry,
20-
editorServices: IEditorServices
21-
) => {
14+
requires: [IToolbarWidgetRegistry],
15+
activate: (app: JupyterFrontEnd, toolbarRegistry: IToolbarWidgetRegistry) => {
2216
// 1) File type
2317
app.docRegistry.addFileType(
2418
{
@@ -27,27 +21,31 @@ const plugin: JupyterFrontEndPlugin<void> = {
2721
extensions: ['.deepnote'],
2822
mimeTypes: ['text/yaml', 'application/x-yaml'],
2923
fileFormat: 'text',
30-
contentType: 'notebook'
24+
contentType: 'file'
3125
},
32-
[factoryName]
26+
['Notebook']
3327
);
3428

35-
// 2) Widget factory that reuses the stock notebook UI
36-
const contentFactory = new NotebookPanel.ContentFactory({
37-
editorFactory: editorServices.factoryService.newInlineEditor
38-
});
29+
app.docRegistry.setDefaultWidgetFactory('deepnote', 'Notebook');
3930

40-
const widgetFactory = new NotebookWidgetFactory({
41-
name: factoryName,
42-
modelName: 'notebook', // built-in notebook model
43-
fileTypes: ['deepnote'],
44-
defaultFor: ['deepnote'],
45-
rendermime,
46-
contentFactory,
47-
mimeTypeService: editorServices.mimeTypeService
48-
});
31+
toolbarRegistry.addFactory<NotebookPanel>(
32+
'Notebook',
33+
'deepnote:switch-notebook',
34+
panel => {
35+
if (!panel.context.path.endsWith('.deepnote')) {
36+
return new Widget(); // don’t render for .ipynb or others
37+
}
4938

50-
app.docRegistry.addWidgetFactory(widgetFactory);
39+
return new ToolbarButton({
40+
className: 'debug-deepnote-button',
41+
label: 'Deepnote',
42+
tooltip: 'Do a Deepnote action',
43+
onClick: () => {
44+
console.log('clicked for', panel.context.path);
45+
}
46+
});
47+
}
48+
);
5149
}
5250
};
5351

yarn.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2502,7 +2502,7 @@ __metadata:
25022502
languageName: node
25032503
linkType: hard
25042504

2505-
"@jupyterlab/settingregistry@npm:^4.4.7":
2505+
"@jupyterlab/settingregistry@npm:^4.0.0, @jupyterlab/settingregistry@npm:^4.4.7":
25062506
version: 4.4.7
25072507
resolution: "@jupyterlab/settingregistry@npm:4.4.7"
25082508
dependencies:
@@ -6799,7 +6799,9 @@ __metadata:
67996799
"@jupyterlab/coreutils": ^6.0.0
68006800
"@jupyterlab/notebook": ^4.4.7
68016801
"@jupyterlab/services": ^7.0.0
6802+
"@jupyterlab/settingregistry": ^4.0.0
68026803
"@jupyterlab/testutils": ^4.0.0
6804+
"@lumino/widgets": ^2.7.1
68036805
"@types/jest": ^29.2.0
68046806
"@types/json-schema": ^7.0.11
68056807
"@types/react": ^18.0.26

0 commit comments

Comments
 (0)