-
Notifications
You must be signed in to change notification settings - Fork 524
Feature: fetch all widgets in one single comm message using the control channel #766
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
trungleduc
merged 1 commit into
voila-dashboards:main
from
maartenbreddels:refactor_widget_fetching
Dec 22, 2021
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -194,7 +194,123 @@ export class WidgetManager extends JupyterLabManager { | |
}); | ||
} | ||
|
||
/** | ||
* This is the implementation of building widgets models making use of the | ||
* jupyter.widget.control comm channel | ||
*/ | ||
async _build_models(): Promise<{ [key: string]: base.WidgetModel }> { | ||
const models: { [key: string]: base.WidgetModel } = {}; | ||
const commId = base.uuid(); | ||
const initComm = await this._create_comm( | ||
'jupyter.widget.control', | ||
commId, | ||
{ widgets: null }, | ||
{ version: '1.0.0' } | ||
); | ||
|
||
// Fetch widget states | ||
let data: any; | ||
let buffers: any; | ||
try { | ||
await new Promise((resolve, reject) => { | ||
initComm.on_msg(msg => { | ||
data = msg['content']['data']; | ||
|
||
if (data.method !== 'update_states') { | ||
console.warn(` | ||
Unknown ${data.method} message on the Control channel | ||
`); | ||
return; | ||
} | ||
|
||
buffers = (msg.buffers || []).map((b: any) => { | ||
if (b instanceof DataView) { | ||
return b; | ||
} else { | ||
return new DataView(b instanceof ArrayBuffer ? b : b.buffer); | ||
} | ||
}); | ||
|
||
resolve(null); | ||
}); | ||
|
||
initComm.on_close(reject); | ||
|
||
// Send a states request msg | ||
initComm.send({ method: 'request_states' }, {}); | ||
}); | ||
} catch (error) { | ||
console.warn( | ||
martinRenou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'Failed to open "jupyter.widget.control" comm channel, fallback to slow fetching of widgets.', | ||
error | ||
); | ||
// Fallback to the old implementation for old ipywidgets versions (<=7.6) | ||
return this._build_models_slow(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the backend for ipywidgets does not implement the control channel, we fallback to the old implementation |
||
} | ||
|
||
initComm.close(); | ||
|
||
const states: any = data.states; | ||
|
||
// Extract buffer paths | ||
// Why do we have to do this? Is there another way? | ||
const bufferPaths: any = {}; | ||
for (const bufferPath of data.buffer_paths) { | ||
if (!bufferPaths[bufferPath[0]]) { | ||
bufferPaths[bufferPath[0]] = []; | ||
} | ||
bufferPaths[bufferPath[0]].push(bufferPath.slice(1)); | ||
} | ||
|
||
const widgetPromises: Promise<base.WidgetModel>[] = []; | ||
|
||
// Start creating all widgets | ||
for (const [widget_id, state] of Object.entries(states) as any) { | ||
try { | ||
const comm = await this._create_comm('jupyter.widget', widget_id); | ||
|
||
// Put binary buffers | ||
if (widget_id in bufferPaths) { | ||
const nBuffers = bufferPaths[widget_id].length; | ||
base.put_buffers( | ||
state, | ||
bufferPaths[widget_id], | ||
buffers.splice(0, nBuffers) | ||
); | ||
} | ||
|
||
const modelPromise = this.new_model( | ||
{ | ||
model_name: state.model_name, | ||
model_module: state.model_module, | ||
model_module_version: state.model_module_version, | ||
model_id: widget_id, | ||
comm: comm | ||
}, | ||
state.state | ||
); | ||
widgetPromises.push(modelPromise); | ||
} catch (error) { | ||
// Failed to create a widget model, we continue creating other models so that | ||
// other widgets can render | ||
console.error(error); | ||
} | ||
} | ||
|
||
// Wait for widgets to be created | ||
const widgets = await Promise.all(widgetPromises); | ||
for (const model of widgets) { | ||
models[model.model_id] = model; | ||
} | ||
|
||
return models; | ||
} | ||
|
||
/** | ||
* This is the old implementation of building widgets models | ||
* We keep it around for supporting old ipywidgets versions (<=7.6) | ||
*/ | ||
async _build_models_slow(): Promise<{ [key: string]: base.WidgetModel }> { | ||
const comm_ids = await this._get_comm_info(); | ||
const models: { [key: string]: base.WidgetModel } = {}; | ||
/** | ||
|
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.