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

Skip to content

Commit c240ffa

Browse files
committed
Added error messages for stream_validate.
1 parent 1b6a2ec commit c240ffa

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

plotly/plotly/plotly.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ def open(self):
367367
'plotly-streamtoken': self.stream_id})
368368

369369

370-
def write(self, data, reconnect_on=(200, '', 408)):
370+
def write(self, data, layout=None, validate=True,
371+
reconnect_on=(200, '', 408)):
371372
""" Write `data` to your stream. This will plot the
372373
`data` in your graph in real-time.
373374
@@ -392,6 +393,15 @@ def write(self, data, reconnect_on=(200, '', 408)):
392393
"""
393394

394395
# TODO: Verify the data
396+
if validate:
397+
if 'type' not in data:
398+
data['type'] = 'scatter'
399+
tools.validate(data, data['type']) # todo: add error message
400+
tools.validate_stream(data, data['type']) # todo: add error message
401+
if layout is not None:
402+
tools.validate(layout, 'Layout') # todo: add error message
403+
404+
# TODO: allow string version of this?
395405
jdata = json.dumps(data, cls=utils._plotlyJSONEncoder)
396406
jdata += "\n"
397407

plotly/tools.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,28 @@ def validate(obj, obj_type):
359359
raise exceptions.PlotlyError(
360360
"'{}' is not a recognizable graph_obj.".
361361
format(obj_type))
362+
363+
364+
def validate_stream(obj, obj_type):
365+
info = graph_objs.INFO[graph_objs.NAME_TO_KEY[obj_type]]
366+
for key, val in obj.items():
367+
if key == 'type':
368+
continue
369+
if 'streamable' in info[key]:
370+
if not info[key]['streamable']:
371+
raise exceptions.PlotlyError(
372+
"The '{}' key is not streamable in the '{}' object".format(
373+
key, obj_type
374+
)
375+
)
376+
else:
377+
raise exceptions.PlotlyError(
378+
"The '{}' key is not streamable in the '{}' object".format(
379+
key, obj_type
380+
)
381+
)
382+
try:
383+
sub_obj_type = graph_objs.KEY_TO_NAME[key]
384+
validate_stream(val, sub_obj_type)
385+
except KeyError:
386+
pass

0 commit comments

Comments
 (0)