8000 add validation to `offline.iplot` · ltnetcase/plotly.py@1ac4816 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ac4816

Browse files
committed
add validation to offline.iplot
1 parent b8d11a8 commit 1ac4816

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

plotly/offline/offline.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def init_notebook_mode():
5656
'</script>'))
5757

5858

59-
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
59+
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
60+
validate=True):
6061
"""
6162
Draw plotly graphs inside an IPython notebook without
6263
connecting to an external server.
@@ -74,6 +75,11 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
7475
of the chart that will export the chart to
7576
Plotly Cloud or Plotly Enterprise
7677
link_text (default='Export to plot.ly') -- the text of export link
78+
validate (default=True) -- validate that all of the keys in the figure
79+
are valid? omit if your version of plotly.js
80+
has become outdated with your version of
81+
graph_reference.json or if you need to include
82+
extra, unnecessary keys in your figure.
7783
7884
Example:
7985
```
@@ -96,15 +102,10 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
96102
raise ImportError('`iplot` can only run inside an IPython Notebook.')
97103

98104
from IPython.display import HTML, display
99-
if isinstance(figure_or_data, dict):
100-
data = figure_or_data['data']
101-
layout = figure_or_data.get('layout', {})
102-
else:
103-
data = figure_or_data
104-
layout = {}
105+
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
105106

106-
width = layout.get('width', '100%')
107-
height = layout.get('height', 525)
107+
width = figure.get('layout', {}).get('width', '100%')
108+
height = figure.get('layout', {}).get('height', 525)
108109
try:
109110
float(width)
110111
except (ValueError, TypeError):
@@ -120,8 +121,8 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
120121
width = str(width) + 'px'
121122

122123
plotdivid = uuid.uuid4()
123-
jdata = json.dumps(data, cls=utils.PlotlyJSONEncoder)
124-
jlayout = json.dumps(layout, cls=utils.PlotlyJSONEncoder)
124+
jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
125+
jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder)
125126

126127
if show_link is False:
127128
link_text = ''

0 commit comments

Comments
 (0)
0