Skip to content

Commit 579d823

Browse files
gnestorjonmmease
authored andcommitted
Pass config object to iplot when rendering plotly JSON (#1281)
* Pass config object to iplot when rendering plotly JSON * Pass config file contents by default * Pass plotly_domain as plotlyServerURL to front-end
1 parent a8ae062 commit 579d823

File tree

1 file changed

+63
-49
lines changed

1 file changed

+63
-49
lines changed

plotly/offline/offline.py

+63-49
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,65 @@ def _build_mathjax_script(url):
113113
.format(url=url))
114114

115115

116+
def _get_jconfig(config):
117+
# TODO: The get_config 'source of truth' should
118+
# really be somewhere other than plotly.plotly
119+
config = config if config else plotly.plotly.get_config()
120+
121+
configkeys = (
122+
'staticPlot',
123+
'plotlyServerURL',
124+
'editable',
125+
'edits',
126+
'autosizable',
127+
'queueLength',
128+
'fillFrame',
129+
'frameMargins',
130+
'scrollZoom',
131+
'doubleClick',
132+
'showTips',
133+
'showAxisDragHandles',
134+
'showAxisRangeEntryBoxes',
135+
'showLink',
136+
'sendData',
137+
'linkText',
138+
'showSources',
139+
'displayModeBar',
140+
'modeBarButtonsToRemove',
141+
'modeBarButtonsToAdd',
142+
'modeBarButtons',
143+
'toImageButtonOptions',
144+
'displaylogo',
145+
'plotGlPixelRatio',
146+
'setBackground',
147+
'topojsonURL',
148+
'mapboxAccessToken',
149+
'logging',
150+
'globalTransforms',
151+
'locale',
152+
'locales',
153+
)
154+
155+
clean_config = dict((k, config[k]) for k in configkeys if k in config)
156+
157+
# TODO: The get_config 'source of truth' should
158+
# really be somewhere other than plotly.plotly
159+
plotly_platform_url = plotly.plotly.get_config().get('plotly_domain',
160+
'https://plot.ly')
161+
162+
if (plotly_platform_url != 'https://plot.ly' and
163+
clean_config['linkText'] == 'Export to plot.ly'):
164+
165+
link_domain = plotly_platform_url\
166+
.replace('https://', '')\
167+
.replace('http://', '')
168+
link_text = clean_config['linkText'].replace('plot.ly', link_domain)
169+
clean_config['linkText'] = link_text
170+
clean_config['plotlyServerURL'] = plotly_platform_url
171+
172+
return clean_config
173+
174+
116175
# Build script to set global PlotlyConfig object. This must execute before
117176
# plotly.js is loaded.
118177
_window_plotly_config = """\
@@ -271,56 +330,9 @@ def _plot_html(figure_or_data, config, validate, default_width,
271330
else:
272331
jframes = None
273332

274-
configkeys = (
275-
'staticPlot',
276-
'plotlyServerURL',
277-
'editable',
278-
'edits',
279-
'autosizable',
280-
'queueLength',
281-
'fillFrame',
282-
'frameMargins',
283-
'scrollZoom',
284-
'doubleClick',
285-
'showTips',
286-
'showAxisDragHandles',
287-
'showAxisRangeEntryBoxes',
288-
'showLink',
289-
'sendData',
290-
'linkText',
291-
'showSources',
292-
'displayModeBar',
293-
'modeBarButtonsToRemove',
294-
'modeBarButtonsToAdd',
295-
'modeBarButtons',
296-
'toImageButtonOptions',
297-
'displaylogo',
298-
'plotGlPixelRatio',
299-
'setBackground',
300-
'topojsonURL',
301-
'mapboxAccessToken',
302-
'logging',
303-
'globalTransforms',
304-
'locale',
305-
'locales',
306-
)
307-
308-
config_clean = dict((k, config[k]) for k in configkeys if k in config)
309-
jconfig = _json.dumps(config_clean)
310-
311-
# TODO: The get_config 'source of truth' should
312-
# really be somewhere other than plotly.plotly
333+
jconfig = _json.dumps(_get_jconfig(config))
313334
plotly_platform_url = plotly.plotly.get_config().get('plotly_domain',
314335
'https://plot.ly')
315-
if (plotly_platform_url != 'https://plot.ly' and
316-
config['linkText'] == 'Export to plot.ly'):
317-
318-
link_domain = plotly_platform_url\
319-
.replace('https://', '')\
320-
.replace('http://', '')
321-
link_text = config['linkText'].replace('plot.ly', link_domain)
322-
config['linkText'] = link_text
323-
jconfig = jconfig.replace('Export to plot.ly', link_text)
324336

325337
if jframes:
326338
script = '''
@@ -426,6 +438,8 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
426438
config.setdefault('showLink', show_link)
427439
config.setdefault('linkText', link_text)
428440

441+
jconfig = _get_jconfig(config)
442+
429443
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
430444

431445
# Though it can add quite a bit to the display-bundle size, we include
@@ -438,7 +452,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
438452
frames = _json.loads(_json.dumps(figure.get('frames', None),
439453
cls=plotly.utils.PlotlyJSONEncoder))
440454

441-
fig = {'data': data, 'layout': layout}
455+
fig = {'data': data, 'layout': layout, 'config': jconfig}
442456
if frames:
443457
fig['frames'] = frames
444458

0 commit comments

Comments
 (0)