Skip to content

Backport PR #3370 on branch 7.x (Allow Output widget in html manager to render state updates) #3372

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions examples/web3/jupyter-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 14 additions & 9 deletions examples/web3/widget_code.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
[
"from ipywidgets import IntSlider, Text, VBox",
"from IPython.display import display",
"from ipywidgets import IntSlider, Output, Text, VBox",
"from IPython.display import Image, display",
"",
"s = IntSlider(max=200, value=100)",
"t = Text()",
"s = IntSlider(min=1, max=500, value=200, description='Width')",
"t = Text(description='Area', disabled=True)",
"o = Output()",
"display(VBox([s, t, o]))",
"",
"def update_text(change=None):",
" t.value = str(s.value ** 2)",
"def update(change=None):",
" width = s.value",
" t.value = str(width ** 2)",
" o.outputs = ()",
" img = Image(url='jupyter-logo.svg', width=width)",
" o.append_display_data(img)",
"",
"s.observe(update_text, names='value')",
"update_text()",
"display(VBox([s, t]))"
"s.observe(update, names='value')",
"update()"
]
27 changes: 19 additions & 8 deletions packages/html-manager/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,35 @@ export class OutputModel extends outputBase.OutputModel {
defaults() {
return {
...super.defaults(),
msg_id: ''
msg_id: '',
outputs: [],
};
}

initialize(attributes: any, options: any) {
initialize(attributes: any, options: any): void {
super.initialize(attributes, options);
this._outputs = new OutputAreaModel({
values: attributes.outputs,
// Widgets (including this output widget) are only rendered in
// trusted contexts
trusted: true,
});
this._outputs = new OutputAreaModel({ trusted: true });
this.listenTo(this, 'change:outputs', this.setOutputs);
this.setOutputs();
}

get outputs() {
return this._outputs;
}

clear_output(wait = false): void {
this._outputs.clear(wait);
}

setOutputs(model?: any, value?: any, options?: any): void {
if (!(options && options.newMessage)) {
// fromJSON does not clear the existing output
this.clear_output();
// fromJSON does not copy the message, so we make a deep copy
this._outputs.fromJSON(JSON.parse(JSON.stringify(this.get('outputs'))));
}
}

private _outputs: OutputAreaModel;
widget_manager: HTMLManager;
}
Expand Down
44 changes: 43 additions & 1 deletion packages/html-manager/test/src/output_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,46 @@ describe('Output widget', function() {
);
expect(widgetTag.innerText).to.equal('something different');
});
});

it('renders text output', async () => {
const manager = new HTMLManager();
const modelId = 'u-u-i-d';
const modelCreate = {
model_name: 'OutputModel',
model_id: modelId,
model_module: '@jupyter-widgets/output',
model_module_version: '*',
};

const startingText = 'starting text';
const endingText = 'ending text';
const modelState = {
_view_module: '@jupyter-widgets/output',
outputs: [
{
output_type: 'stream',
name: 'stdout',
text: startingText,
},
],
};

const widgetTag = document.createElement('div');
widgetTag.className = 'widget-subarea';
document.body.appendChild(widgetTag);
const model = await manager.new_model(modelCreate, modelState);
await manager.display_model(
undefined, model, { el: widgetTag }
);
expect(widgetTag.innerText).to.equal(startingText);

model.set('outputs', [
{
output_type: 'stream',
name: 'stdout',
text: endingText,
},
]);
expect(widgetTag.innerText).to.equal(endingText);
});
});