Skip to content

Commit ff9bd14

Browse files
committed
allows plotly plots to be placed in another div by specifying the div id as "div_id" in the MIME json package
implements jupyterlab#173
1 parent 6990c5f commit ff9bd14

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

packages/plotly-extension/src/index.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,44 @@ interface IPlotlySpec {
3434
}
3535

3636
export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
37+
render_target: HTMLElement;
3738
/**
3839
* Create a new widget for rendering Plotly.
3940
*/
4041
constructor(options: IRenderMime.IRendererOptions) {
4142
super();
42-
this.addClass(CSS_CLASS);
4343
this._mimeType = options.mimeType;
4444
}
4545

4646
/**
4747
* Render Plotly into this widget's node.
4848
*/
4949
renderModel(model: IRenderMime.IMimeModel): Promise<void> {
50-
const { data, layout, frames, config } = model.data[this._mimeType] as
51-
| any
52-
| IPlotlySpec;
50+
const { data, layout, frames, config, div_id } = model.data[
51+
this._mimeType
52+
] as any | IPlotlySpec;
5353
// const metadata = model.metadata[this._mimeType] as any || {};
54-
return Plotly.react(this.node, data, layout, config).then(plot => {
54+
if (div_id == null) {
55+
this.render_target = this.node;
56+
this.addClass(CSS_CLASS);
57+
} else {
58+
this.render_target = document.getElementById(div_id);
59+
}
60+
return Plotly.react(this.render_target, data, layout, config).then(plot => {
5561
this.update();
5662
if (frames) {
57-
Plotly.addFrames(this.node, frames).then(() => {
58-
Plotly.animate(this.node);
63+
Plotly.addFrames(this.render_target, frames).then(() => {
64+
Plotly.animate(this.render_target);
5965
});
6066
}
61-
if (this.node.offsetWidth > 0 && this.node.offsetHeight > 0) {
67+
if (
68+
this.render_target.offsetWidth > 0 &&
69+
this.render_target.offsetHeight > 0
70+
) {
6271
Plotly.toImage(plot, {
6372
format: 'png',
64-
width: this.node.offsetWidth,
65-
height: this.node.offsetHeight
73+
width: this.render_target.offsetWidth,
74+
height: this.render_target.offsetHeight
6675
}).then((url: string) => {
6776
const imageData = url.split(',')[1];
6877
if (model.data['image/png'] !== imageData) {
@@ -97,8 +106,8 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
97106
*/
98107
protected onUpdateRequest(msg: Message): void {
99108
if (this.isVisible) {
100-
Plotly.redraw(this.node).then(() => {
101-
Plotly.Plots.resize(this.node);
109+
Plotly.redraw(this.render_target).then(() => {
110+
Plotly.Plots.resize(this.render_target);
102111
});
103112
}
104113
}

0 commit comments

Comments
 (0)