-
Notifications
You must be signed in to change notification settings - Fork 13
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
Closed
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9eecebc
expose global sessionmanager. still need to find current session
TK-21st 55312a1
cleanup sessions notebook
TK-21st dbc9539
enable getting current session
TK-21st 85be982
adding some docstring stuff
TK-21st e8b8ea8
reverting last commit..not sure about what's the rules with copy righ…
TK-21st 2c6c5f6
eslint
TK-21st 45e920a
pylint
TK-21st de9e933
use public ILabShell instead of FocusTracker
TK-21st 91ea7fa
remove unused trait definitions
TK-21st 412108e
lint
TK-21st 50d9158
eslint
TK-21st aa357c8
first commit
TK-21st fdd8700
Revert "first commit"
TK-21st File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andIConsoleTracker
?There was a problem hiding this comment.
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 byINotebookTracker
orIConsoleTracker
, and hope to inject some code there withipylab
, then this won't work.But then again, a SUPER edge case.
There was a problem hiding this comment.
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?
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!
Agreed! So would you say we go with
INotebookTracker
andIConsoleTracker
for now or keep using the genericFocusTracker
as it is now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
andIConsoleTracker
as those are the "public" tracker exposed from the notebook and console extensions. Also we can add them asoptional
in theactivate
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).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also
ILabShell
has acurrentWidget
andcurrentChanged
(and is already exposed asShell
in ipylab).Maybe this could be a substitute to the focus tracker?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooo, I did not know that.. that'd certainly be better than exposing a private attribute..
Let me change that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to using shell.