Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing session information to kernel #44

Closed
wants to merge 13 commits into from
99 changes: 99 additions & 0 deletions examples/sessions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Session Manager"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipylab import JupyterFrontEnd\n",
"app = JupyterFrontEnd()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## show all sessions from the global `SessionManager` instance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"app.sessions.list_sessions()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Show current session"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"app.sessions.current_session"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Force update current session\n",
"current_session should be updated automatically, you can force a call if you really want to"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"app.sessions.get_current_session()\n",
"app.sessions.current_session"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
10 changes: 9 additions & 1 deletion ipylab/jupyterfrontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from .commands import CommandRegistry
from .shell import Shell
from .sessions import SessionManager


@register
Expand All @@ -23,9 +24,16 @@ class JupyterFrontEnd(Widget):
version = Unicode(read_only=True).tag(sync=True)
shell = Instance(Shell).tag(sync=True, **widget_serialization)
commands = Instance(CommandRegistry).tag(sync=True, **widget_serialization)
sessions = Instance(SessionManager).tag(sync=True, **widget_serialization)

def __init__(self, *args, **kwargs):
super().__init__(*args, shell=Shell(), commands=CommandRegistry(), **kwargs)
super().__init__(
*args,
shell=Shell(),
commands=CommandRegistry(),
sessions=SessionManager(),
**kwargs
)
self._ready_event = asyncio.Event()
self._on_ready_callbacks = CallbackDispatcher()
self.on_msg(self._on_frontend_msg)
Expand Down
77 changes: 77 additions & 0 deletions ipylab/sessions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'''Expose current and all sessions to python kernel
'''

from ipywidgets import Widget, register, widget_serialization
from traitlets import List, Unicode, Bool, Instance, HasTraits, Dict

from ._frontend import module_name, module_version


def _noop():
pass


class Kernel(HasTraits):
'''Maps Kernel.IModel

TODO: Not Used Currently
'''
_id = Unicode(readonly=True)
name = Unicode(readonly=True)


class KernelPreference(HasTraits):
'''Maps ISessionContext.IKernelPreference

TODO: Not Used Currently
'''
_id = Unicode(read_only=True)
name = Unicode(read_only=True)
language = Unicode(read_only=True)
shouldStart = Bool(True, read_only=True)
canStart = Bool(True, read_only=True)
shutdownOnDispose = Bool(False, read_only=True)
autoStartDefault = Bool(True, read_only=True)


class SessionContext(HasTraits):
'''Partially Map @jupyterlab/apputils SessionContext.IOptions

TODO: Not Used Currently
'''
path = Unicode(read_only=True)
basePath = Unicode(read_only=True)
name = Unicode(read_only=True)
kernelPreference = Instance(KernelPreference).tag(**widget_serialization)


class Session(HasTraits):
'''Maps @jupyterlab/services Session.IModel

TODO: Not Used Currently
'''
_id = Unicode(readonly=True)
name = Unicode(readonly=True)
path = Unicode(readonly=True)
_type = Unicode(readonly=True)
kernel = Instance(Kernel).tag(**widget_serialization)


@register
class SessionManager(Widget):
'''Expose JupyterFrontEnd.serviceManager.sessions'''
_model_name = Unicode("SessionManagerModel").tag(sync=True)
_model_module = Unicode(module_name).tag(sync=True)
_model_module_version = Unicode(module_version).tag(sync=True)

# keeps track of alist of sessions
current_session = Dict(read_only=True).tag(sync=True)
sessions = List([], read_only=True).tag(sync=True)

def get_current_session(self):
'''Force a call to update current session'''
self.send({"func":"get_current"})

def list_sessions(self):
'''List all running sessions managed in the manager'''
return self.sessions
5 changes: 5 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {

import { ICommandPalette } from '@jupyterlab/apputils';

import { FocusTracker } from '@lumino/widgets';

import { IJupyterWidgetRegistry } from '@jupyter-widgets/base';

import * as widgetExports from './widget';
Expand Down Expand Up @@ -36,6 +38,9 @@ const extension: JupyterFrontEndPlugin<void> = {
widgetExports.ShellModel.shell = shell;
widgetExports.CommandRegistryModel.commands = app.commands;
widgetExports.CommandPaletteModel.palette = palette;
widgetExports.SessionManagerModel.sessions = app.serviceManager.sessions;
const focusTracker: FocusTracker<any> = (app.shell as any)._tracker;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering whether this should be the lab shell focus tracker, or the INotebookTracker and IConsoleTracker?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to clean that up I feel.

But I was also considering the super edge-case where the user wants to use, say, CodeCell in a custom extension not tracked by INotebookTracker or IConsoleTracker, and hope to inject some code there with ipylab, then this won't work.

But then again, a SUPER edge case.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but it could indeed be useful! Maybe we could track that in a separate issue?

But I was also considering the super edge-case where the user wants to use, say, CodeCell in a custom extension not tracked by INotebookTracker or IConsoleTracker, and hope to inject some code there with ipylab, then this won't work.

The custom extension could provide its own track (ICustomTracker), and then ipylab could require this token as optional in the activate function. Although that would mean making a change to ipylab everytime we need to support a new extension. So yes a more general focus tracker could probably scale better.

Copy link
Contributor Author

@TK-21st TK-21st May 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but it could indeed be useful! Maybe we could track that in a separate issue?

Sure!

So yes a more general focus tracker could probably scale better.

Agreed! So would you say we go with INotebookTracker and IConsoleTracker for now or keep using the generic FocusTracker as it is now?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! So would you say we go with INotebookTracker and IConsoleTracker for now or keep using the generic FocusTracker as it is now?

I'll give it a try locally to see what it looks like with the FocusTracker.

At first it felt more natural to go with INotebookTracker and IConsoleTracker as those are the "public" tracker exposed from the notebook and console extensions. Also we can add them as optional in the activate function. Which means they are available only if the environment provides them (would be interesting to try in a custom lab build some day! #33)

But if the focus tracker might just keep things simpler actually! (checking for widget.sessionContext.session? directly).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also ILabShell has a currentWidget and currentChanged (and is already exposed as Shell in ipylab).

Maybe this could be a substitute to the focus tracker?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also ILabShell has a currentWidget and currentChanged (and is already exposed as Shell in ipylab).

Maybe this could be a substitute to the focus tracker?

Ooo, I did not know that.. that'd certainly be better than exposing a private attribute..
Let me change that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to using shell.

widgetExports.SessionManagerModel.tracker = focusTracker;

registry.registerWidget({
name: MODULE_NAME,
Expand Down
2 changes: 2 additions & 0 deletions src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '../css/widget.css';

import { CommandRegistryModel } from './widgets/commands';
import { CommandPaletteModel } from './widgets/palette';
import { SessionManagerModel } from './widgets/sessions';
import { JupyterFrontEndModel } from './widgets/frontend';
import { PanelModel } from './widgets/panel';
import { ShellModel } from './widgets/shell';
Expand All @@ -21,4 +22,5 @@ export {
SplitPanelModel,
SplitPanelView,
TitleModel,
SessionManagerModel,
};
131 changes: 131 additions & 0 deletions src/widgets/sessions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// SessionManager exposes `JupyterLab.serviceManager.sessions` to user python kernel

import { SessionManager } from '@jupyterlab/services';
import { ISerializers, WidgetModel } from '@jupyter-widgets/base';
import { toArray } from '@lumino/algorithm';
import { FocusTracker } from '@lumino/widgets';
import { MODULE_NAME, MODULE_VERSION } from '../version';
import { Session } from '@jupyterlab/services';

/**
* The model for a Session Manager
*/
export class SessionManagerModel extends WidgetModel {
/**
* The default attributes.
*/
defaults(): any {
return {
...super.defaults(),
_model_name: SessionManagerModel.model_name,
_model_module: SessionManagerModel.model_module,
_model_module_version: SessionManagerModel.model_module_version,
current_session: null,
sessions: [],
};
}

/**
* Initialize a CommandRegistryModel instance.
*
* @param attributes The base attributes.
* @param options The initialization options.
*/
initialize(attributes: any, options: any): void {
this._sessions = SessionManagerModel.sessions;
this._tracker = SessionManagerModel.tracker;
super.initialize(attributes, options);
this.on('msg:custom', this._onMessage.bind(this));
this._sessions.runningChanged.connect(this._sendSessions, this);
this._tracker.currentChanged.connect(this._currentChanged, this);
this._tracker.activeChanged.connect(this._currentChanged, this);
this._sendSessions();
this._sendCurrent();
}

/**
* Handle a custom message from the backend.
*
* @param msg The message to handle.
*/
private _onMessage(msg: any): void {
switch (msg.func) {
case 'get_current':
this._sendCurrent();
break;
default:
break;
}
}

/**
* get sessionContext from a given widget instance
*
* @param widget widget tracked by app.shell._track (FocusTracker)
*/
private _getSessionContext(widget: any): Session.IModel | {} {
if (widget?.sessionContext) {
// covers both ConsolePanel and NotebookPanle
if (widget.sessionContext.session?.model) {
return widget.sessionContext.session.model;
}
}
return {}; // empty object serializes to empty dict in python
TK-21st marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Handle focus change in JLab
*
* NOTE: currentChange fires on two situations that we are concerned about here:
* 1. when user focuses on a widget in browser, which the `change.newValue` will
* be the current Widget
* 2. when user executes a code in console/notebook, where the `changed.newValue` will be null since
* we lost focus due to execution.
* To solve this problem, we interrogate `this._tracker.currentWidget` directly.
* We also added a simple fencing to reduce the number of Comm sync calls between Python/JS
*/
private _currentChanged(): void {
this._current_session = this._getSessionContext(
this._tracker.currentWidget
);
this.set('current_session', this._current_session);
this.set('sessions', toArray(this._sessions.running()));
this.save_changes();
}

/**
* Send the list of commands to the backend.
*/
private _sendSessions(): void {
this.set('sessions', toArray(this._sessions.running()));
this.save_changes();
}

/**
* send current session to backend
*/
private _sendCurrent(): void {
this._current_session = this._getSessionContext(
this._tracker.currentWidget
);
this.set('current_session', this._current_session);
this.save_changes();
}

static serializers: ISerializers = {
...WidgetModel.serializers,
};

static model_name = 'SessionManagerModel';
static model_module = MODULE_NAME;
static model_module_version = MODULE_VERSION;
static view_name: string = null;
static view_module: string = null;
static view_module_version = MODULE_VERSION;

private _current_session: Session.IModel | {};
private _sessions: SessionManager;
static sessions: SessionManager;
private _tracker: FocusTracker<any>;
static tracker: FocusTracker<any>;
}