Skip to content

Commit

Permalink
Support widget factory
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Oct 5, 2024
1 parent b5ce4dd commit 3d14ba3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
4 changes: 3 additions & 1 deletion samples/jp_app_launcher_config_2.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
- title: Notebook example in config 2
description: Example of opening a notebook in dashboard mode without Voila
description: Example of opening a notebook with Voila Preview
source: ./sample.ipynb
args:
widget-type: 'Voila Preview'
cwd: '.'
type: notebook
catalog: Config 2
Expand Down
48 changes: 26 additions & 22 deletions src/factories/notebook/notebook_factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { JupyterFrontEnd } from '@jupyterlab/application';
import { NotebookPanel } from '@jupyterlab/notebook';
import { IDocumentManager } from '@jupyterlab/docmanager';
import { IDefaultFileBrowser } from '@jupyterlab/filebrowser';
import { NotebookPanel } from '@jupyterlab/notebook';

import { ILauncherConfiguration } from '../../schema';
import { IDict, IPanelFactory } from '../../token';

Expand All @@ -10,42 +12,44 @@ export class NotebookFactory implements IPanelFactory {
if (!config.sourceCode) {
return;
}
const { documentManager } = this.options;
const cwd = args['cwd'];
const app = this.options.app;
const { documentManager, fileBrowser } = this.options;

const model = await documentManager.newUntitled({
path: cwd,
type: 'notebook',
ext: '.ipynb'
});
let renamed = false;
let found = false;
let index = 0;
let newName = model.path;
while (!renamed) {
try {
newName = `${config.title}${index === 0 ? '' : '-' + index}.ipynb`;
await documentManager.rename(model.path, newName);
renamed = true;
} catch {
let newName = config.title;
const allFiles = new Set([...fileBrowser.model.items()].map(it => it.name));
while (!found) {
newName = `${config.title}${index === 0 ? '' : '-' + index}.ipynb`;
if (!allFiles.has(newName)) {
found = true;
} else {
index += 1;
}
}
const doc: NotebookPanel = await app.commands.execute('docmanager:open', {
path: newName
});
const fileBlob = new File([config.sourceCode], newName);
await fileBrowser.model.upload(fileBlob);
const configArgs: IDict = config.args ?? {};
const factory = configArgs['widget-type'] ?? 'default';
const doc = await documentManager.openOrReveal(newName, factory);
if (!doc) {
console.error(`Failed to create widget with ${factory} factory`);
return;
}
await doc.context.ready;
doc.context.model.fromString(config.sourceCode);

await doc.context.save();
await doc.sessionContext.ready;
doc.content.activeCellIndex = 1;
if (doc instanceof NotebookPanel) {
await doc.sessionContext.ready;
doc.content.activeCellIndex = 1;
}
}
}

export namespace NotebookFactory {
export interface IOptions {
app: JupyterFrontEnd;
documentManager: IDocumentManager;
fileBrowser: IDefaultFileBrowser;
}
}
9 changes: 6 additions & 3 deletions src/manager_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IEditorServices } from '@jupyterlab/codeeditor';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { IThemeManager } from '@jupyterlab/apputils';

import { IDefaultFileBrowser } from '@jupyterlab/filebrowser';
import { CommandsFactory } from './factories/commands';
import { LocalServerFactory } from './factories/local_server';
import { MarkdownFactory } from './factories/markdown';
Expand All @@ -27,7 +27,8 @@ export const panelFactoryPlugin: JupyterFrontEndPlugin<IPanelFactoryManager> = {
INotebookTracker,
IEditorServices,
NotebookPanel.IContentFactory,
IDocumentManager
IDocumentManager,
IDefaultFileBrowser
],
optional: [IThemeManager],
activate: (
Expand All @@ -37,6 +38,7 @@ export const panelFactoryPlugin: JupyterFrontEndPlugin<IPanelFactoryManager> = {
editorServices: IEditorServices,
contentFactory: NotebookPanel.IContentFactory,
documentManager: IDocumentManager,
fileBrowser: IDefaultFileBrowser,
themeManager?: IThemeManager
): IPanelFactoryManager => {
const manager = new PanelFactoryManager();
Expand All @@ -52,7 +54,8 @@ export const panelFactoryPlugin: JupyterFrontEndPlugin<IPanelFactoryManager> = {

const notebookFactory = new NotebookFactory({
app,
documentManager
documentManager,
fileBrowser
});
manager.registerFactory('notebook', notebookFactory);

Expand Down

0 comments on commit 3d14ba3

Please sign in to comment.