Skip to content

Commit dbfa5a4

Browse files
committed
Make offline.plot/iplot accept a plot config dict
1 parent 574e461 commit dbfa5a4

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

plotly/offline/offline.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def _plot_html(figure_or_data, config, validate, default_width,
275275

276276
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
277277
validate=True, image=None, filename='plot_image', image_width=800,
278-
image_height=600):
278+
image_height=600, config=None):
279279
"""
280280
Draw plotly graphs inside an IPython or Jupyter notebook without
281281
connecting to an external server.
@@ -308,6 +308,9 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
308308
will be saved to. The extension should not be included.
309309
image_height (default=600) -- Specifies the height of the image in `px`.
310310
image_width (default=800) -- Specifies the width of the image in `px`.
311+
config (default=None) -- Plot view options dictionary. Keyword arguments
312+
`show_link` and `link_text` set the associated options in this
313+
dictionary if it doesn't contain them already.
311314
312315
Example:
313316
```
@@ -331,9 +334,9 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
331334
if not ipython:
332335
raise ImportError('`iplot` can only run inside an IPython Notebook.')
333336

334-
config = {}
335-
config['showLink'] = show_link
336-
config['linkText'] = link_text
337+
config = dict(config) if config else {}
338+
config.setdefault('showLink', show_link)
339+
config.setdefault('linkText', link_text)
337340

338341
plot_html, plotdivid, width, height = _plot_html(
339342
figure_or_data, config, validate, '100%', 525, True
@@ -375,7 +378,8 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
375378
def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
376379
validate=True, output_type='file', include_plotlyjs=True,
377380
filename='temp-plot.html', auto_open=True, image=None,
378-
image_filename='plot_image', image_width=800, image_height=600):
381+
image_filename='plot_image', image_width=800, image_height=600,
382+
config=None):
379383
""" Create a plotly graph locally as an HTML document or string.
380384
381385
Example:
@@ -435,6 +439,9 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
435439
image will be saved to. The extension should not be included.
436440
image_height (default=600) -- Specifies the height of the image in `px`.
437441
image_width (default=800) -- Specifies the width of the image in `px`.
442+
config (default=None) -- Plot view options dictionary. Keyword arguments
443+
`show_link` and `link_text` set the associated options in this
444+
dictionary if it doesn't contain them already.
438445
"""
439446
if output_type not in ['div', 'file']:
440447
raise ValueError(
@@ -446,9 +453,9 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
446453
"Adding .html to the end of your file.")
447454
filename += '.html'
448455

449-
config = {}
450-
config['showLink'] = show_link
451-
config['linkText'] = link_text
456+
config = dict(config) if config else {}
457+
config.setdefault('showLink', show_link)
458+
config.setdefault('linkText', link_text)
452459

453460
plot_html, plotdivid, width, height = _plot_html(
454461
figure_or_data, config, validate,

plotly/tests/test_core/test_offline/test_offline.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ def test_autoresizing(self):
9494
for resize_code_string in resize_code_strings:
9595
self.assertTrue(resize_code_string not in html)
9696

97+
def test_config(self):
98+
config = dict(linkText='Plotly rocks!',
99+
editable=True)
100+
html = self._read_html(plotly.offline.plot(fig, config=config,
101+
auto_open=False))
102+
self.assertIn('"linkText": "Plotly rocks!"', html)
103+
self.assertIn('"showLink": true', html)
104+
self.assertIn('"editable": true', html)
105+
97106

98107
class PlotlyOfflineOtherDomainTestCase(PlotlyOfflineBaseTestCase):
99108
def setUp(self):

0 commit comments

Comments
 (0)