Skip to content

Commit d486d8e

Browse files
committed
Support auto-resize in offline.plot with output_type='div' and include_plotlyjs=False
1 parent f9e94c9 commit d486d8e

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

Diff for: plotly/offline/offline.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,12 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
541541
'</div>',
542542
])
543543
else:
544-
return plot_html
544+
return ''.join([
545+
'<div>',
546+
plot_html,
547+
resize_script,
548+
'</div>',
549+
])
545550

546551

547552
def plot_mpl(mpl_fig, resize=False, strip_style=False,

Diff for: plotly/tests/test_core/test_offline/test_offline.py

+30-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
)
2222
}
2323

24+
25+
resize_code_strings = [
26+
'window.addEventListener("resize", ',
27+
'Plotly.Plots.resize('
28+
]
29+
30+
2431
PLOTLYJS = plotly.offline.offline.get_plotlyjs()
2532

2633

@@ -77,10 +84,7 @@ def test_div_output(self):
7784
self.assertTrue(html.startswith('<div>') and html.endswith('</div>'))
7885

7986
def test_autoresizing(self):
80-
resize_code_strings = [
81-
'window.addEventListener("resize", ',
82-
'Plotly.Plots.resize('
83-
]
87+
8488
# If width or height wasn't specified, then we add a window resizer
8589
html = self._read_html(plotly.offline.plot(fig, auto_open=False))
8690
for resize_code_string in resize_code_strings:
@@ -96,6 +100,28 @@ def test_autoresizing(self):
96100
for resize_code_string in resize_code_strings:
97101
self.assertNotIn(resize_code_string, html)
98102

103+
def test_autoresizing_div(self):
104+
105+
# If width or height wasn't specified, then we add a window resizer
106+
for include_plotlyjs in [True, False]:
107+
html = plotly.offline.plot(fig,
108+
output_type='div',
109+
include_plotlyjs=include_plotlyjs)
110+
111+
for resize_code_string in resize_code_strings:
112+
self.assertIn(resize_code_string, html)
113+
114+
# If width or height was specified, then we don't resize
115+
html = plotly.offline.plot({
116+
'data': fig['data'],
117+
'layout': {
118+
'width': 500, 'height': 500
119+
}
120+
}, output_type='div')
121+
122+
for resize_code_string in resize_code_strings:
123+
self.assertNotIn(resize_code_string, html)
124+
99125
def test_config(self):
100126
config = dict(linkText='Plotly rocks!',
101127
editable=True)

0 commit comments

Comments
 (0)