|
| 1 | +import { |
| 2 | + JupyterFrontEnd, |
| 3 | + JupyterFrontEndPlugin |
| 4 | +} from '@jupyterlab/application'; |
| 5 | +import React from 'react'; |
| 6 | +import { IToolbarWidgetRegistry, ReactWidget } from '@jupyterlab/apputils'; |
| 7 | +import { |
| 8 | + INotebookWidgetFactory, |
| 9 | + NotebookPanel, |
| 10 | + NotebookWidgetFactory |
| 11 | +} from '@jupyterlab/notebook'; |
| 12 | +import { Widget } from '@lumino/widgets'; |
| 13 | +import { HTMLSelect } from '@jupyterlab/ui-components'; |
| 14 | + |
| 15 | +const plugin: JupyterFrontEndPlugin<void> = { |
| 16 | + id: 'jupyterlab-deepnote:plugin', |
| 17 | + description: 'Open .deepnote files as notebooks.', |
| 18 | + autoStart: true, |
| 19 | + requires: [INotebookWidgetFactory, IToolbarWidgetRegistry], |
| 20 | + activate: ( |
| 21 | + app: JupyterFrontEnd, |
| 22 | + notebookWidgetFactory: NotebookWidgetFactory, |
| 23 | + toolbarRegistry: IToolbarWidgetRegistry |
| 24 | + ) => { |
| 25 | + app.docRegistry.addFileType( |
| 26 | + { |
| 27 | + name: 'deepnote', |
| 28 | + displayName: 'Deepnote Notebook', |
| 29 | + extensions: ['.deepnote'], |
| 30 | + mimeTypes: ['text/yaml', 'application/x-yaml'], |
| 31 | + fileFormat: 'text', |
| 32 | + contentType: 'file' |
| 33 | + }, |
| 34 | + [notebookWidgetFactory.name] |
| 35 | + ); |
| 36 | + |
| 37 | + app.docRegistry.setDefaultWidgetFactory( |
| 38 | + 'deepnote', |
| 39 | + notebookWidgetFactory.name |
| 40 | + ); |
| 41 | + |
| 42 | + toolbarRegistry.addFactory<NotebookPanel>( |
| 43 | + notebookWidgetFactory.name, |
| 44 | + 'deepnote:switch-notebook', |
| 45 | + panel => { |
| 46 | + if (!panel.context.path.endsWith('.deepnote')) { |
| 47 | + return new Widget(); // don’t render for .ipynb or others |
| 48 | + } |
| 49 | + |
| 50 | + return new NotebookPicker(panel); |
| 51 | + } |
| 52 | + ); |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +class NotebookPicker extends ReactWidget { |
| 57 | + constructor(private panel: NotebookPanel) { |
| 58 | + super(); |
| 59 | + // when the context becomes ready, trigger re-render |
| 60 | + void panel.context.ready.then(() => { |
| 61 | + this.update(); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + render(): JSX.Element { |
| 66 | + const metadataNames = |
| 67 | + this.panel.context.model.getMetadata('notebook_names'); |
| 68 | + const names = |
| 69 | + Array.isArray(metadataNames) && |
| 70 | + metadataNames.every(n => typeof n === 'string') |
| 71 | + ? metadataNames |
| 72 | + : []; |
| 73 | + |
| 74 | + return ( |
| 75 | + <HTMLSelect |
| 76 | + value={names[0] ?? '-'} |
| 77 | + onChange={() => {}} |
| 78 | + onKeyDown={() => {}} |
| 79 | + aria-label="Select active notebook" |
| 80 | + title="Select active notebook" |
| 81 | + > |
| 82 | + <option value="-">-</option> |
| 83 | + {names.map(n => ( |
| 84 | + <option key={n} value={n}> |
| 85 | + {n} |
| 86 | + </option> |
| 87 | + ))} |
| 88 | + </HTMLSelect> |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export default plugin; |
0 commit comments