@@ -31,6 +31,8 @@ def to_html(fig,
3131 post_script = None ,
3232 full_html = True ,
3333 animation_opts = None ,
34+ default_width = '100%' ,
35+ default_height = '100%' ,
3436 validate = True ):
3537 """
3638 Convert a figure to an HTML string representation.
@@ -93,10 +95,10 @@ def to_html(fig,
9395 If a string that ends in '.js', a script tag is included that
9496 references the specified path. This approach can be used to point the
9597 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
100102 plotly.js figure is associated with. One application for this script
101103 is to install custom plotly.js event handlers.
102104 full_html: bool (default True)
@@ -109,6 +111,11 @@ def to_html(fig,
109111 https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js
110112 for available options. Has no effect if the figure does not contain
111113 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%').
112119 validate: bool (default True)
113120 True if the figure should be validated before being converted to
114121 JSON, False otherwise.
@@ -143,25 +150,47 @@ def to_html(fig,
143150 # ## Serialize figure config ##
144151 config = _get_jconfig (config )
145152
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 )
150155
151156 jconfig = json .dumps (config )
152157
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+
153178 # ## Get platform URL ##
154179 plotly_platform_url = config .get ('plotlyServerURL' , 'https://plot.ly' )
155180
156181 # ## Build script body ##
157182 # This is the part that actually calls Plotly.js
183+
184+ # build post script snippet(s)
185+ then_post_script = ''
158186 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(){{
160191 {post_script}
161192 }})""" .format (
162- post_script = post_script .replace ('{plot_id}' , plotdivid ))
163- else :
164- then_post_script = ''
193+ post_script = ps .replace ('{plot_id}' , plotdivid ))
165194
166195 then_addframes = ''
167196 then_animate = ''
@@ -274,7 +303,8 @@ def to_html(fig,
274303 <div>
275304 {mathjax_script}
276305 {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>
278308 <script type="text/javascript">
279309 {require_start}
280310 window.PLOTLYENV=window.PLOTLYENV || {{}};
@@ -286,6 +316,8 @@ def to_html(fig,
286316 mathjax_script = mathjax_script ,
287317 load_plotlyjs = load_plotlyjs ,
288318 id = plotdivid ,
319+ width = div_width ,
320+ height = div_height ,
289321 plotly_platform_url = plotly_platform_url ,
290322 require_start = require_start ,
291323 script = script ,
@@ -313,6 +345,8 @@ def write_html(fig,
313345 full_html = True ,
314346 animation_opts = None ,
315347 validate = True ,
348+ default_width = '100%' ,
349+ default_height = '100%' ,
316350 auto_open = False ):
317351 """
318352 Write a figure to an HTML file representation
@@ -393,10 +427,10 @@ def write_html(fig,
393427 If a string that ends in '.js', a script tag is included that
394428 references the specified path. This approach can be used to point the
395429 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
400434 plotly.js figure is associated with. One application for this script
401435 is to install custom plotly.js event handlers.
402436 full_html: bool (default True)
@@ -409,6 +443,11 @@ def write_html(fig,
409443 https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js
410444 for available options. Has no effect if the figure does not contain
411445 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%').
412451 validate: bool (default True)
413452 True if the figure should be validated before being converted to
414453 JSON, False otherwise.
@@ -430,8 +469,11 @@ def write_html(fig,
430469 include_mathjax = include_mathjax ,
431470 post_script = post_script ,
432471 full_html = full_html ,
472+ animation_opts = animation_opts ,
473+ default_width = default_width ,
474+ default_height = default_height ,
433475 validate = validate ,
434- animation_opts = animation_opts )
476+ )
435477
436478 # Check if file is a string
437479 file_is_str = isinstance (file , six .string_types )
0 commit comments