Skip to content

Issue399 #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ To install the optional dependencies:
pip install -r optional-requirements.txt
```

##Testing
##Testing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd set up your editor to trim trailing white spaces on save


Our API uses Nose to run tests. (https://nose.readthedocs.org/en/latest/)

Expand Down
26 changes: 13 additions & 13 deletions plotly/graph_reference/default-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4405,7 +4405,7 @@
}
},
"colorscale": {
"description": "Has only an effect if `marker.color` is set to a numerical array. Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in color space, use cmin and cmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -5594,7 +5594,7 @@
}
},
"colorscale": {
"description": "Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in z space, use zmin and zmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -6134,7 +6134,7 @@
}
},
"colorscale": {
"description": "Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in z space, use zmin and zmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -6781,7 +6781,7 @@
}
},
"colorscale": {
"description": "Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in z space, use zmin and zmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -7660,7 +7660,7 @@
}
},
"colorscale": {
"description": "Has only an effect if `marker.color` is set to a numerical array. Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in color space, use cmin and cmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -8307,7 +8307,7 @@
}
},
"colorscale": {
"description": "Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in z space, use zmin and zmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -8933,7 +8933,7 @@
}
},
"colorscale": {
"description": "Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in z space, use zmin and zmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -9617,7 +9617,7 @@
}
},
"colorscale": {
"description": "Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in z space, use zmin and zmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -10933,7 +10933,7 @@
}
},
"colorscale": {
"description": "Has only an effect if `marker.color` is set to a numerical array. Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in color space, use cmin and cmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -12308,7 +12308,7 @@
}
},
"colorscale": {
"description": "Has only an effect if `marker.color` is set to a numerical array. Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in color space, use cmin and cmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -13197,7 +13197,7 @@
}
},
"colorscale": {
"description": "Has only an effect if `marker.color` is set to a numerical array. Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in color space, use cmin and cmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -14428,7 +14428,7 @@
}
},
"colorscale": {
"description": "Has only an effect if `marker.color` is set to a numerical array. Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in color space, use cmin and cmax",
"role": "style",
"valType": "colorscale"
},
Expand Down Expand Up @@ -15053,7 +15053,7 @@
}
},
"colorscale": {
"description": "Sets the colorscale.",
"description": "Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example, `[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']]`. To control the bounds of the colorscale in z space, use zmin and zmax",
"role": "style",
"valType": "colorscale"
},
Expand Down
116 changes: 73 additions & 43 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import os
import uuid
import warnings
from pkg_resources import resource_string
import webbrowser

from pkg_resources import resource_string


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like this got accidentally committed - GitHub Desktop makes it easy to not commit and discard these types of changes

import plotly
from plotly import tools, utils
from plotly.exceptions import PlotlyError
Expand Down Expand Up @@ -71,7 +73,7 @@ def init_notebook_mode():
'</script>'))


def _plot_html(figure_or_data, show_link, link_text,
def _plot_html(figure_or_data, config,
validate, default_width, default_height):

figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
Expand All @@ -97,22 +99,44 @@ def _plot_html(figure_or_data, show_link, link_text,
jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder)

config = {}
config['showLink'] = show_link
config['linkText'] = link_text
jconfig = json.dumps(config)
configkeys = (
'editable',
'autosizable',
'fillFrame',
'frameMargins',
'scrollZoom',
'doubleClick',
'showTips',
'showLink',
'sendData',
'linkText',
'showSources',
'displayModeBar',
'modeBarButtonsToRemove',
'modeBarButtonsToAdd',
'modeBarButtons',
'displaylogo',
'plotGlPixelRatio',
'setBackground',
'topojsonURL')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we add another config option in plotly.js? This list can't stay in-sync.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpsievert got this right, by not validating the config options: https://github.com/ropensci/plotly/pull/446/files

At some point, I want to make the config option part of the plot schema, where they could be use to validate user input from the python api. Before that happens though, I would much prefer not validating the config options.


config_clean = dict((k,config[k]) for k in configkeys if k in config)

jconfig = json.dumps(config_clean)

# TODO: The get_config 'source of truth' should
# really be somewhere other than plotly.plotly
plotly_platform_url = plotly.plotly.get_config().get('plotly_domain',
'https://plot.ly')

if (plotly_platform_url != 'https://plot.ly' and
link_text == 'Export to plot.ly'):
'linkText' in config):

link_domain = plotly_platform_url\
.replace('https://', '')\
.replace('http://', '')
link_text = link_text.replace('plot.ly', link_domain)

config['linkText'] = config['linkText'].replace('plot.ly', link_domain)

script = 'Plotly.newPlot("{id}", {data}, {layout}, {config})'.format(
id=plotdivid,
Expand All @@ -137,7 +161,8 @@ def _plot_html(figure_or_data, show_link, link_text,
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,
config={'showLink': True, 'linkText': 'Export to plot.ly'},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we try really hard not to break backwards incompatibility. if we remove show_link and link_text then our user's code could break when they upgrade plotly. in this case, we might want to do something like:

iplot(figure_or_data, show_link=True, link_text='Export to plot.ly', **config_options)

validate=True):
"""
Draw plotly graphs inside an IPython notebook without
Expand All @@ -152,10 +177,12 @@ 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
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
config -- a dictionary with configuration attributes for the plot,
default contains the following two elements:
showLink (default=True) -- display a link in the bottom-right corner of
of the chart that will export the chart to
Plotly Cloud or Plotly Enterprise
linkText (default='Export to plot.ly') -- the text of export link
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll want to describe that these are plotly.js configuration options and provide a link to our documentation: https://plot.ly/javascript/configuration-options/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Something to note though is that the keywords for show_link ad link_text aren't in sync with the ones on the plotly.js config options, which could lead to some confusion.

validate (default=True) -- validate that all of the keys in the figure
are valid? omit if your version of plotly.js
has become outdated with your version of
Expand Down Expand Up @@ -185,14 +212,14 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
from IPython.display import HTML, display

plot_html, plotdivid, width, height = _plot_html(
figure_or_data, show_link, link_text, validate,
figure_or_data, config, validate,
'100%', 525)

display(HTML(plot_html))


def plot(figure_or_data,
show_link=True, link_text='Export to plot.ly',
config={'showLink': True, 'linkText': 'Export to plot.ly'},
validate=True, output_type='file',
include_plotlyjs=True,
filename='temp-plot.html',
Expand All @@ -214,10 +241,12 @@ def plot(figure_or_data,
graph descriptions.

Keyword arguments:
show_link (default=True) -- 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
config -- a dictionary with configuration attributes for the plot,
default contains the following two elements:
showLink (default=True) -- display a link in the bottom-right corner of
of the chart that will export the chart to Plotly Cloud or
Plotly Enterprise
linkText (default='Export to plot.ly') -- the text of export link
validate (default=True) -- validate that all of the keys in the figure
are valid? omit if your version of plotly.js has become outdated
with your version of graph_reference.json or if you need to include
Expand Down Expand Up @@ -254,7 +283,7 @@ def plot(figure_or_data,
filename += '.html'

plot_html, plotdivid, width, height = _plot_html(
figure_or_data, show_link, link_text, validate,
figure_or_data, config, validate,
'100%', '100%')

figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
Expand Down Expand Up @@ -311,8 +340,8 @@ def plot(figure_or_data,
return plot_html


def plot_mpl(mpl_fig, resize=False, strip_style=False,
verbose=False, show_link=True, link_text='Export to plot.ly',
def plot_mpl(mpl_fig, resize=False, strip_style=False, verbose=False,
config={'showLink': True, 'linkText': 'Export to plot.ly'},
validate=True, output_type='file', include_plotlyjs=True,
filename='temp-plot.html', auto_open=True):
"""
Expand All @@ -330,10 +359,12 @@ 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
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
config -- a dictionary with configuration attributes for the plot,
default contains the following two elements:
showLink (default=True) -- display a link in the bottom-right corner of
of the chart that will export the chart to Plotly Cloud or
Plotly Enterprise
linkText (default='Export to plot.ly') -- the text of export link
validate (default=True) -- validate that all of the keys in the figure
are valid? omit if your version of plotly.js has become outdated
with your version of graph_reference.json or if you need to include
Expand Down Expand Up @@ -375,13 +406,13 @@ def plot_mpl(mpl_fig, resize=False, strip_style=False,
```
"""
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
return plot(plotly_plot, show_link, link_text, validate, output_type,
return plot(plotly_plot, config, validate, output_type,
include_plotlyjs, filename, auto_open)


def iplot_mpl(mpl_fig, resize=False, strip_style=False,
verbose=False, show_link=True,
link_text='Export to plot.ly', validate=True):
def iplot_mpl(mpl_fig, resize=False, strip_style=False, verbose=False,
config={'showLink': True, 'linkText': 'Export to plot.ly'},
validate=True):
"""
Convert a matplotlib figure to a plotly graph and plot inside an IPython
notebook without connecting to an external server.
Expand All @@ -401,13 +432,12 @@ 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
of the chart that will export the chart to Plotly Cloud or
Plotly Enterprise
show_link (default=True) -- 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
config -- a dictionary with configuration attributes for the plot,
default contains the following two elements:
showLink (default=True) -- display a link in the bottom-right corner of
of the chart that will export the chart to
Plotly Cloud or Plotly Enterprise
linkText (default='Export to plot.ly') -- the text of export link
validate (default=True) -- validate that all of the keys in the figure
are valid? omit if your version of plotly.js
has become outdated with your version of
Expand All @@ -430,12 +460,13 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
```
"""
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
return iplot(plotly_plot, show_link, link_text, validate)
return iplot(plotly_plot, config, validate)


def enable_mpl_offline(resize=False, strip_style=False,
verbose=False, show_link=True,
link_text='Export to plot.ly', validate=True):
def enable_mpl_offline(resize=False, strip_style=False, verbose=False,
config={'showLink': True,
'linkText': 'Export to plot.ly'},
validate=True):
"""
Convert mpl plots to locally hosted HTML documents.

Expand Down Expand Up @@ -468,5 +499,4 @@ def enable_mpl_offline(resize=False, strip_style=False,
formatter = ip.display_formatter.formatters['text/html']
formatter.for_type(matplotlib.figure.Figure,
lambda fig: iplot_mpl(fig, resize, strip_style, verbose,
show_link, link_text, validate))

config, validate))
92 changes: 92 additions & 0 deletions temp-plot.html

Large diffs are not rendered by default.