Skip to content

Commit

Permalink
Merge pull request #14 from trungleduc/fix-missing-session
Browse files Browse the repository at this point in the history
Fix missing session of grid app
  • Loading branch information
trungleduc authored Oct 13, 2022
2 parents 3c22cd6 + 61de777 commit 7040059
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 10 deletions.
148 changes: 148 additions & 0 deletions samples/test-jupyter-app-launcher-ipylab.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion src/documents/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export class NotebookGridWidgetFactory extends ABCWidgetFactory<
protected createNewWidget(
context: DocumentRegistry.IContext<INotebookModel>
): NotebookGridDocWidget {
return new NotebookGridDocWidget({
const widget = new NotebookGridDocWidget({
context,
content: new NotebookGridPanel(context, this._notebookGridFactory)
});
return widget;
}

private _notebookGridFactory: NotebookGridFactory;
Expand Down
18 changes: 16 additions & 2 deletions src/documents/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +28,7 @@ export class NotebookGridDocWidget extends DocumentWidget<
INotebookModel
> {
constructor(
options: DocumentWidget.IOptions<NotebookGridPanel, INotebookModel>
private options: DocumentWidget.IOptions<NotebookGridPanel, INotebookModel>
) {
super(options);
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
7 changes: 4 additions & 3 deletions src/factories/notebook_grid/notebook_grid_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppWidget> {
constructor(private options: NotebookGridFactory.IOptions) {}
async create(config: ILauncherConfiguration): Promise<ILauncherApp | void> {
async create(
config: ILauncherConfiguration
): Promise<ILauncherApp<AppWidget> | void> {
if (!config.sourceCode) {
console.error('Notebook source is missing');
return;
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ export interface IAppTracker extends IWidgetTracker<Widget> {
instanceChanged: ISignal<IAppTracker, void>;
}

export interface ILauncherApp {
panel: Widget;
export interface ILauncherApp<T extends Widget = Widget> {
panel: T;
ready?: Promise<void>;
}

export interface IPanelFactory {
export interface IPanelFactory<T extends Widget = Widget> {
create(
config: ILauncherConfiguration,
args?: IDict
): Promise<ILauncherApp | void>;
): Promise<ILauncherApp<T> | void>;
}

export interface IPanelFactoryManager extends IDisposable {
Expand Down

0 comments on commit 7040059

Please sign in to comment.