diff --git a/samples/test-jupyter-app-launcher-ipylab.ipynb b/samples/test-jupyter-app-launcher-ipylab.ipynb new file mode 100644 index 0000000..5b915c6 --- /dev/null +++ b/samples/test-jupyter-app-launcher-ipylab.ipynb @@ -0,0 +1,148 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "d1e4bfe1-76c5-4148-b145-e89fe942584a", + "metadata": { + "extensions": { + "jupyter_dashboards": { + "activeView": "grid_default", + "views": { + "grid_default": { + "col": 0, + "height": 2, + "hidden": false, + "locked": true, + "row": 0, + "width": 12 + } + } + } + }, + "tags": [] + }, + "outputs": [], + "source": [ + "from ipylab import JupyterFrontEnd # type: ignore\n", + "app = JupyterFrontEnd()\n", + "import ipywidgets" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "044dd3e7-5507-4fe2-9867-6333ce8950ef", + "metadata": { + "extensions": { + "jupyter_dashboards": { + "activeView": "grid_default", + "views": { + "grid_default": { + "col": 0, + "height": 4, + "hidden": false, + "locked": true, + "row": 2, + "width": 12 + } + } + } + }, + "tags": [] + }, + "outputs": [], + "source": [ + "def open_console(_):\n", + " app.commands.execute(\n", + " 'console:create', \n", + " app.sessions.current_session)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8ba9fcdc-7193-46b0-94f8-d73d26247f19", + "metadata": {}, + "outputs": [], + "source": [ + "b = ipywidgets.Button(description='Open console')\n", + "b.on_click(open_console)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "01ee6611-5f02-4296-98ec-a4cc1c8f5c29", + "metadata": {}, + "outputs": [], + "source": [ + "b" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12533fe9-b8d2-4c37-aa13-1dd25940e44d", + "metadata": {}, + "outputs": [], + "source": [ + "a = app.sessions.current_session" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "057ecd0b-fb11-4b30-89d7-8232270040a9", + "metadata": {}, + "outputs": [], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f361e001-244b-45cd-8a31-8f582d8b3356", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "extensions": { + "jupyter_dashboards": { + "activeView": "grid_default", + "version": 1, + "views": { + "grid_default": { + "cellMargin": 2, + "defaultCellHeight": 40, + "maxColumns": 12, + "name": "grid", + "type": "grid" + } + } + } + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/documents/factory.ts b/src/documents/factory.ts index 12e312f..48463c4 100644 --- a/src/documents/factory.ts +++ b/src/documents/factory.ts @@ -21,10 +21,11 @@ export class NotebookGridWidgetFactory extends ABCWidgetFactory< protected createNewWidget( context: DocumentRegistry.IContext ): NotebookGridDocWidget { - return new NotebookGridDocWidget({ + const widget = new NotebookGridDocWidget({ context, content: new NotebookGridPanel(context, this._notebookGridFactory) }); + return widget; } private _notebookGridFactory: NotebookGridFactory; diff --git a/src/documents/widget.ts b/src/documents/widget.ts index 7ab282f..db7bf17 100644 --- a/src/documents/widget.ts +++ b/src/documents/widget.ts @@ -7,11 +7,13 @@ import { } from '@jupyterlab/notebook'; import { IRenderMimeRegistry } from '@jupyterlab/rendermime'; import { ServiceManager } from '@jupyterlab/services'; +import { ISessionContext } from '@jupyterlab/apputils'; import { Signal } from '@lumino/signaling'; import { Widget } from '@lumino/widgets'; import { NotebookGridFactory } from '../factories/notebook_grid/notebook_grid_factory'; import { ILauncherConfiguration } from '../schema'; +import { AppWidget } from '../factories/notebook_grid/app_widget'; export interface IGridOptions { manager: ServiceManager; @@ -26,7 +28,7 @@ export class NotebookGridDocWidget extends DocumentWidget< INotebookModel > { constructor( - options: DocumentWidget.IOptions + private options: DocumentWidget.IOptions ) { super(options); } @@ -35,6 +37,10 @@ export class NotebookGridDocWidget extends DocumentWidget< this.content.dispose(); super.dispose(); } + + get sessionContext(): ISessionContext { + return this.options.content.sessionContext; + } } export class NotebookGridPanel extends Widget { @@ -74,5 +80,13 @@ export class NotebookGridPanel extends Widget { super.dispose(); } - private _widget: Widget; + get sessionContext(): ISessionContext { + return this._widget.model.context.sessionContext; + } + + get model(): INotebookModel | null { + return this._widget.model.context.model; + } + + private _widget: AppWidget; } diff --git a/src/factories/notebook_grid/notebook_grid_factory.ts b/src/factories/notebook_grid/notebook_grid_factory.ts index 11c1bc5..f8fc6da 100644 --- a/src/factories/notebook_grid/notebook_grid_factory.ts +++ b/src/factories/notebook_grid/notebook_grid_factory.ts @@ -15,9 +15,11 @@ import { ILauncherApp } from './../../token'; import { AppModel } from './app_model'; import { AppWidget } from './app_widget'; -export class NotebookGridFactory implements IPanelFactory { +export class NotebookGridFactory implements IPanelFactory { constructor(private options: NotebookGridFactory.IOptions) {} - async create(config: ILauncherConfiguration): Promise { + async create( + config: ILauncherConfiguration + ): Promise | void> { if (!config.sourceCode) { console.error('Notebook source is missing'); return; @@ -67,7 +69,6 @@ export class NotebookGridFactory implements IPanelFactory { editorConfig: StaticNotebook.defaultEditorConfig, notebookConfig: StaticNotebook.defaultNotebookConfig }); - const panel = new AppWidget({ id: UUID.uuid4(), label: config.title, diff --git a/src/token.ts b/src/token.ts index 7919df6..06810a5 100644 --- a/src/token.ts +++ b/src/token.ts @@ -42,16 +42,16 @@ export interface IAppTracker extends IWidgetTracker { instanceChanged: ISignal; } -export interface ILauncherApp { - panel: Widget; +export interface ILauncherApp { + panel: T; ready?: Promise; } -export interface IPanelFactory { +export interface IPanelFactory { create( config: ILauncherConfiguration, args?: IDict - ): Promise; + ): Promise | void>; } export interface IPanelFactoryManager extends IDisposable {