@@ -31,6 +31,8 @@ def to_html(fig,
31
31
post_script = None ,
32
32
full_html = True ,
33
33
animation_opts = None ,
34
+ default_width = '100%' ,
35
+ default_height = '100%' ,
34
36
validate = True ):
35
37
"""
36
38
Convert a figure to an HTML string representation.
@@ -93,10 +95,10 @@ def to_html(fig,
93
95
If a string that ends in '.js', a script tag is included that
94
96
references the specified path. This approach can be used to point the
95
97
resulting HTML div string to an alternative CDN.
96
- post_script: str or None (default None)
97
- JavaScript snippet to be included in the resulting div just after
98
- plot creation. The string may include '{plot_id}' placeholders that
99
- will then be replaced by the `id` of the div element that the
98
+ post_script: str or list or None (default None)
99
+ JavaScript snippet(s) to be included in the resulting div just after
100
+ plot creation. The string(s) may include '{plot_id}' placeholders
101
+ that will then be replaced by the `id` of the div element that the
100
102
plotly.js figure is associated with. One application for this script
101
103
is to install custom plotly.js event handlers.
102
104
full_html: bool (default True)
@@ -109,6 +111,11 @@ def to_html(fig,
109
111
https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js
110
112
for available options. Has no effect if the figure does not contain
111
113
frames, or auto_play is False.
114
+ default_width, default_height: number or str (default '100%')
115
+ The default figure width/height to use if the provided figure does not
116
+ specify its own layout.width/layout.height property. May be
117
+ specified in pixels as an integer (e.g. 500), or as a css width style
118
+ string (e.g. '500px', '100%').
112
119
validate: bool (default True)
113
120
True if the figure should be validated before being converted to
114
121
JSON, False otherwise.
@@ -143,25 +150,47 @@ def to_html(fig,
143
150
# ## Serialize figure config ##
144
151
config = _get_jconfig (config )
145
152
146
- # Check whether we should add responsive
147
- layout_dict = fig_dict .get ('layout' , {})
148
- if layout_dict .get ('width' , None ) is None :
149
- config .setdefault ('responsive' , True )
153
+ # Set responsive
154
+ config .setdefault ('responsive' , True )
150
155
151
156
jconfig = json .dumps (config )
152
157
158
+ # Get div width/height
159
+ layout_dict = fig_dict .get ('layout' , {})
160
+ div_width = layout_dict .get ('width' , default_width )
161
+ div_height = layout_dict .get ('height' , default_height )
162
+
163
+ # Add 'px' suffix to numeric widths
164
+ try :
165
+ float (div_width )
166
+ except (ValueError , TypeError ):
167
+ pass
168
+ else :
169
+ div_width = str (div_width ) + 'px'
170
+
171
+ try :
172
+ float (div_height )
173
+ except (ValueError , TypeError ):
174
+ pass
175
+ else :
176
+ div_height = str (div_height ) + 'px'
177
+
153
178
# ## Get platform URL ##
154
179
plotly_platform_url = config .get ('plotlyServerURL' , 'https://plot.ly' )
155
180
156
181
# ## Build script body ##
157
182
# This is the part that actually calls Plotly.js
183
+
184
+ # build post script snippet(s)
185
+ then_post_script = ''
158
186
if post_script :
159
- then_post_script = """.then(function(){{
187
+ if not isinstance (post_script , (list , tuple )):
188
+ post_script = [post_script ]
189
+ for ps in post_script :
190
+ then_post_script += """.then(function(){{
160
191
{post_script}
161
192
}})""" .format (
162
- post_script = post_script .replace ('{plot_id}' , plotdivid ))
163
- else :
164
- then_post_script = ''
193
+ post_script = ps .replace ('{plot_id}' , plotdivid ))
165
194
166
195
then_addframes = ''
167
196
then_animate = ''
@@ -274,7 +303,8 @@ def to_html(fig,
274
303
<div>
275
304
{mathjax_script}
276
305
{load_plotlyjs}
277
- <div id="{id}" class="plotly-graph-div"></div>
306
+ <div id="{id}" class="plotly-graph-div" \
307
+ style="height:{height}; width:{width};"></div>
278
308
<script type="text/javascript">
279
309
{require_start}
280
310
window.PLOTLYENV=window.PLOTLYENV || {{}};
@@ -286,6 +316,8 @@ def to_html(fig,
286
316
mathjax_script = mathjax_script ,
287
317
load_plotlyjs = load_plotlyjs ,
288
318
id = plotdivid ,
319
+ width = div_width ,
320
+ height = div_height ,
289
321
plotly_platform_url = plotly_platform_url ,
290
322
require_start = require_start ,
291
323
script = script ,
@@ -313,6 +345,8 @@ def write_html(fig,
313
345
full_html = True ,
314
346
animation_opts = None ,
315
347
validate = True ,
348
+ default_width = '100%' ,
349
+ default_height = '100%' ,
316
350
auto_open = False ):
317
351
"""
318
352
Write a figure to an HTML file representation
@@ -393,10 +427,10 @@ def write_html(fig,
393
427
If a string that ends in '.js', a script tag is included that
394
428
references the specified path. This approach can be used to point the
395
429
resulting HTML div string to an alternative CDN.
396
- post_script: str or None (default None)
397
- JavaScript snippet to be included in the resulting div just after
398
- plot creation. The string may include '{plot_id}' placeholders that
399
- will then be replaced by the `id` of the div element that the
430
+ post_script: str or list or None (default None)
431
+ JavaScript snippet(s) to be included in the resulting div just after
432
+ plot creation. The string(s) may include '{plot_id}' placeholders
433
+ that will then be replaced by the `id` of the div element that the
400
434
plotly.js figure is associated with. One application for this script
401
435
is to install custom plotly.js event handlers.
402
436
full_html: bool (default True)
@@ -409,6 +443,11 @@ def write_html(fig,
409
443
https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js
410
444
for available options. Has no effect if the figure does not contain
411
445
frames, or auto_play is False.
446
+ default_width, default_height: number or str (default '100%')
447
+ The default figure width/height to use if the provided figure does not
448
+ specify its own layout.width/layout.height property. May be
449
+ specified in pixels as an integer (e.g. 500), or as a css width style
450
+ string (e.g. '500px', '100%').
412
451
validate: bool (default True)
413
452
True if the figure should be validated before being converted to
414
453
JSON, False otherwise.
@@ -430,8 +469,11 @@ def write_html(fig,
430
469
include_mathjax = include_mathjax ,
431
470
post_script = post_script ,
432
471
full_html = full_html ,
472
+ animation_opts = animation_opts ,
473
+ default_width = default_width ,
474
+ default_height = default_height ,
433
475
validate = validate ,
434
- animation_opts = animation_opts )
476
+ )
435
477
436
478
# Check if file is a string
437
479
file_is_str = isinstance (file , six .string_types )
0 commit comments