Skip to content

Commit

Permalink
Update config options and disable show_link by default (#1377)
Browse files Browse the repository at this point in the history
* Pass through all user-specified config options to Plotly.js and
raise a warning for options that are not known by plotly.py to be valid

Previously we would silently drop unrecognized options.  This change
was done so that if plotly.py falls behind plotly.js in the future,
the user still has a way to specify all config options supported by
plotly.js

* Add missing config options for 1.43.0

* Change show_link default to False.

Along with the change to set `showSendToCloud` to False by default in
plotly.js 1.43.0, now the default behavior of plotly.py/plotly.js is
that there are no options available by default that will send data
off of the local network.
  • Loading branch information
jonmmease committed Dec 27, 2018
1 parent 802609d commit 2482efd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
40 changes: 27 additions & 13 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,14 @@ def _build_mathjax_script(url):


def _get_jconfig(config):
# TODO: The get_config 'source of truth' should
# really be somewhere other than plotly.plotly
config = config if config else plotly.plotly.get_config()

configkeys = (
'staticPlot',
'plotlyServerURL',
'editable',
'edits',
'autosizable',
'responsive',
'queueLength',
'fillFrame',
'frameMargins',
Expand All @@ -134,6 +132,7 @@ def _get_jconfig(config):
'showAxisRangeEntryBoxes',
'showLink',
'sendData',
'showSendToCloud',
'linkText',
'showSources',
'displayModeBar',
Expand All @@ -142,6 +141,7 @@ def _get_jconfig(config):
'modeBarButtons',
'toImageButtonOptions',
'displaylogo',
'watermark',
'plotGlPixelRatio',
'setBackground',
'topojsonURL',
Expand All @@ -152,7 +152,21 @@ def _get_jconfig(config):
'locales',
)

clean_config = dict((k, config[k]) for k in configkeys if k in config)
if config and isinstance(config, dict):
# Warn user on unrecognized config options. We make this a warning
# rather than an error since we don't have code generation logic in
# place yet to guarantee that the config options in plotly.py are up
# to date
bad_config = [k for k in config if k not in configkeys]
if bad_config:
warnings.warn("""
Unrecognized config options supplied: {bad_config}"""
.format(bad_config=bad_config))

clean_config = config
else:
config = plotly.plotly.get_config()
clean_config = dict((k, config[k]) for k in configkeys if k in config)

# TODO: The get_config 'source of truth' should
# really be somewhere other than plotly.plotly
Expand Down Expand Up @@ -381,7 +395,7 @@ def _plot_html(figure_or_data, config, validate, default_width,
return plotly_html_div, plotdivid, width, height


def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
def iplot(figure_or_data, show_link=False, link_text='Export to plot.ly',
validate=True, image=None, filename='plot_image', image_width=800,
image_height=600, config=None):
"""
Expand All @@ -397,7 +411,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
graph descriptions.
Keyword arguments:
show_link (default=True) -- display a link in the bottom-right corner of
show_link (default=False) -- display a link in the bottom-right corner of
of the chart that will export the chart to
Plotly Cloud or Plotly Enterprise
link_text (default='Export to plot.ly') -- the text of export link
Expand Down Expand Up @@ -497,7 +511,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
ipython_display.display(ipython_display.HTML(script))


def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
def plot(figure_or_data, show_link=False, link_text='Export to plot.ly',
validate=True, output_type='file', include_plotlyjs=True,
filename='temp-plot.html', auto_open=True, image=None,
image_filename='plot_image', image_width=800, image_height=600,
Expand All @@ -523,7 +537,7 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
graph descriptions.
Keyword arguments:
show_link (default=True) -- display a link in the bottom-right corner of
show_link (default=False) -- display a link in the bottom-right corner of
of the chart that will export the chart to Plotly Cloud or
Plotly Enterprise
link_text (default='Export to plot.ly') -- the text of export link
Expand Down Expand Up @@ -742,7 +756,7 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',


def plot_mpl(mpl_fig, resize=False, strip_style=False,
verbose=False, show_link=True, link_text='Export to plot.ly',
verbose=False, show_link=False, link_text='Export to plot.ly',
validate=True, output_type='file', include_plotlyjs=True,
filename='temp-plot.html', auto_open=True,
image=None, image_filename='plot_image',
Expand All @@ -762,7 +776,7 @@ def plot_mpl(mpl_fig, resize=False, strip_style=False,
resize (default=False) -- allow plotly to choose the figure size.
strip_style (default=False) -- allow plotly to choose style options.
verbose (default=False) -- print message.
show_link (default=True) -- display a link in the bottom-right corner of
show_link (default=False) -- display a link in the bottom-right corner of
of the chart that will export the chart to Plotly Cloud or
Plotly Enterprise
link_text (default='Export to plot.ly') -- the text of export link
Expand Down Expand Up @@ -824,7 +838,7 @@ def plot_mpl(mpl_fig, resize=False, strip_style=False,


def iplot_mpl(mpl_fig, resize=False, strip_style=False,
verbose=False, show_link=True,
verbose=False, show_link=False,
link_text='Export to plot.ly', validate=True,
image=None, image_filename='plot_image',
image_height=600, image_width=800):
Expand All @@ -847,7 +861,7 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
resize (default=False) -- allow plotly to choose the figure size.
strip_style (default=False) -- allow plotly to choose style options.
verbose (default=False) -- print message.
show_link (default=True) -- display a link in the bottom-right corner of
show_link (default=False) -- display a link in the bottom-right corner of
of the chart that will export the chart to
Plotly Cloud or Plotly Enterprise
link_text (default='Export to plot.ly') -- the text of export link
Expand Down Expand Up @@ -888,7 +902,7 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,


def enable_mpl_offline(resize=False, strip_style=False,
verbose=False, show_link=True,
verbose=False, show_link=False,
link_text='Export to plot.ly', validate=True):
"""
Convert mpl plots to locally hosted HTML documents.
Expand Down
15 changes: 15 additions & 0 deletions plotly/tests/test_core/test_offline/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,28 @@ def test_autoresizing_div(self):

def test_config(self):
config = dict(linkText='Plotly rocks!',
showLink=True,
editable=True)
html = self._read_html(plotly.offline.plot(fig, config=config,
auto_open=False))
self.assertIn('"linkText": "Plotly rocks!"', html)
self.assertIn('"showLink": true', html)
self.assertIn('"editable": true', html)

def test_config_bad_options(self):
config = dict(bogus=42)

def get_html():
return self._read_html(plotly.offline.plot(
fig, config=config, auto_open=False))

# Attempts to validate warning ran into
# https://bugs.python.org/issue29620, don't check warning for now.
# Revisit when we move to pytest
html = get_html()

self.assertIn('"bogus": 42', html)

def test_plotlyjs_version(self):
with open('js/package.json', 'rt') as f:
package_json = json.load(f)
Expand Down

0 comments on commit 2482efd

Please sign in to comment.