-
Notifications
You must be signed in to change notification settings - Fork 15
Exposing session
information to kernel (fix a commit error in #44)
#46
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
Merged
Merged
Changes from 11 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 b954e34
fix a bunch of typos. use running instead of list_sessions. change ge…
TK-21st 6a60acb
lint
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 hidden or 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 hidden or 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 hidden or 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,32 @@ | ||
"""Expose current and all sessions to python kernel | ||
""" | ||
|
||
from ipywidgets import Widget, register | ||
from traitlets import List, Unicode, Dict | ||
|
||
from ._frontend import module_name, module_version | ||
|
||
|
||
def _noop(): | ||
pass | ||
|
||
TK-21st marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@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 | ||
TK-21st marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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): | ||
TK-21st marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""List all running sessions managed in the manager""" | ||
return self.sessions |
This file contains hidden or 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 hidden or 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 hidden or 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,123 @@ | ||
// 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 { MODULE_NAME, MODULE_VERSION } from '../version'; | ||
import { Session } from '@jupyterlab/services'; | ||
import { ILabShell } from '@jupyterlab/application'; | ||
|
||
/** | ||
* 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. | ||
TK-21st marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param attributes The base attributes. | ||
* @param options The initialization options. | ||
*/ | ||
initialize(attributes: any, options: any): void { | ||
const { sessions, shell } = SessionManagerModel; | ||
this._sessions = sessions; | ||
this._shell = shell; | ||
sessions.runningChanged.connect(this._sendSessions, this); | ||
shell.currentChanged.connect(this._currentChanged, this); | ||
|
||
super.initialize(attributes, options); | ||
this.on('msg:custom', this._onMessage.bind(this)); | ||
this._shell.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 | {} { | ||
return widget?.sessionContext?.session?.model ?? {}; | ||
} | ||
|
||
/** | ||
* 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._shell.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. | ||
TK-21st marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
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._shell.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 _shell: ILabShell; | ||
static shell: ILabShell; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.