Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2eef301

Browse files
committed
move figure_or_data argument logic to tools for use in widgets.py
1 parent a283d83 commit 2eef301

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

plotly/plotly/plotly.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -163,33 +163,8 @@ def plot(figure_or_data, validate=True, **plot_options):
163163
False: do not open plot in the browser, but do return the unique url
164164
165165
"""
166-
if isinstance(figure_or_data, dict):
167-
figure = figure_or_data
168-
elif isinstance(figure_or_data, list):
169-
figure = {'data': figure_or_data}
170-
else:
171-
raise exceptions.PlotlyError("The `figure_or_data` positional argument "
172-
"must be either `dict`-like or "
173-
"`list`-like.")
174-
if validate:
175-
try:
176-
tools.validate(figure, obj_type='Figure')
177-
except exceptions.PlotlyError as err:
178-
raise exceptions.PlotlyError("Invalid 'figure_or_data' argument. "
179-
"Plotly will not be able to properly "
180-
"parse the resulting JSON. If you "
181-
"want to send this 'figure_or_data' "
182-
"to Plotly anyway (not recommended), "
183-
"you can set 'validate=False' as a "
184-
"plot option.\nHere's why you're "
185-
"seeing this error:\n\n{0}"
186-
"".format(err))
187-
if not figure['data']:
188-
raise exceptions.PlotlyEmptyDataError(
189-
"Empty data list found. Make sure that you populated the "
190-
"list of data objects you're sending and try again.\n"
191-
"Questions? [email protected]"
192-
)
166+
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
167+
193168
for entry in figure['data']:
194169
for key, val in list(entry.items()):
195170
try:

plotly/tools.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,3 +1230,35 @@ def __init__(self, url, width, height):
12301230

12311231
def _repr_html_(self):
12321232
return self.embed_code
1233+
1234+
1235+
def return_figure_from_figure_or_data(figure_or_data, validate_figure):
1236+
if isinstance(figure_or_data, dict):
1237+
figure = figure_or_data
1238+
elif isinstance(figure_or_data, list):
1239+
figure = {'data': figure_or_data}
1240+
else:
1241+
raise exceptions.PlotlyError("The `figure_or_data` positional "
1242+
"argument must be either "
1243+
"`dict`-like or `list`-like.")
1244+
if validate_figure:
1245+
try:
1246+
validate(figure, obj_type='Figure')
1247+
except exceptions.PlotlyError as err:
1248+
raise exceptions.PlotlyError("Invalid 'figure_or_data' argument. "
1249+
"Plotly will not be able to properly "
1250+
"parse the resulting JSON. If you "
1251+
"want to send this 'figure_or_data' "
1252+
"to Plotly anyway (not recommended), "
1253+
"you can set 'validate=False' as a "
1254+
"plot option.\nHere's why you're "
1255+
"seeing this error:\n\n{0}"
1256+
"".format(err))
1257+
if not figure['data']:
1258+
raise exceptions.PlotlyEmptyDataError(
1259+
"Empty data list found. Make sure that you populated the "
1260+
"list of data objects you're sending and try again.\n"
1261+
"Questions? [email protected]"
1262+
)
1263+
1264+
return figure

0 commit comments

Comments
 (0)