Skip to content

Commit 1cd5097

Browse files
authored
Remove stream write validation to avoid incorrect validation failures (#1145)
* Fix for stream write validation errors. Removed validation since stream writes don't actually have the same validation rules as traces. * Remove stream trace/layout validation tests
1 parent 752d2a2 commit 1cd5097

File tree

2 files changed

+29
-69
lines changed

2 files changed

+29
-69
lines changed

Diff for: plotly/plotly/plotly.py

+29-51
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ class Stream:
509509
"""
510510
Interface to Plotly's real-time graphing API.
511511
512+
NOTE: Streaming is no longer supported in Plotly Cloud.
513+
Streaming is still available as part of Plotly On-Premises.
514+
512515
Initialize a Stream object with a stream_id
513516
found in {plotly_domain}/settings.
514517
Real-time graphs are initialized with a call to `plot` that embeds
@@ -624,74 +627,49 @@ def write(self, trace, layout=None,
624627
you can 'write' to it in real time.
625628
626629
positional arguments:
627-
trace - A valid plotly trace object (e.g., Scatter, Heatmap, etc.).
628-
Not all keys in these are `stremable` run help(Obj) on the type
629-
of trace your trying to stream, for each valid key, if the key
630-
is streamable, it will say 'streamable = True'. Trace objects
631-
must be dictionary-like.
630+
trace - A dict of properties to stream
631+
Some valid keys for trace dictionaries:
632+
'x', 'y', 'text', 'z', 'marker', 'line'
632633
633634
keyword arguments:
634-
layout (default=None) - A valid Layout object
635+
layout (default=None) - A valid Layout object or dict with
636+
compatible properties
635637
Run help(plotly.graph_objs.Layout)
636638
637-
Some valid keys for trace dictionaries:
638-
'x', 'y', 'text', 'z', 'marker', 'line'
639-
640639
Examples:
641-
>>> write(dict(x=1, y=2)) # assumes 'scatter' type
642-
>>> write(Bar(x=[1, 2, 3], y=[10, 20, 30]))
643-
>>> write(Scatter(x=1, y=2, text='scatter text'))
644-
>>> write(Scatter(x=1, y=3, marker=Marker(color='blue')))
645-
>>> write(Heatmap(z=[[1, 2, 3], [4, 5, 6]]))
640+
641+
Append a point to a scatter trace
642+
>>> write(dict(x=1, y=2))
643+
644+
Overwrite the x and y properties of a scatter trace
645+
>>> write(dict(x=[1, 2, 3], y=[10, 20, 30]))
646+
647+
Append a point to a scatter trace and set the points text value
648+
>>> write(dict(x=1, y=2, text='scatter text'))
649+
650+
Append a point to a scatter trace and set the points color
651+
>>> write(dict(x=1, y=3, marker=go.Marker(color='blue')))
652+
653+
Set a new z value array for a Heatmap trace
654+
>>> write(dict(z=[[1, 2, 3], [4, 5, 6]]))
646655
647656
The connection to plotly's servers is checked before writing
648657
and reconnected if disconnected and if the response status code
649658
is in `reconnect_on`.
650659
651660
For more help, see: `help(plotly.plotly.Stream)`
652661
or see examples and tutorials here:
653-
http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s7_streaming/s7_streaming.ipynb
654662
655663
"""
656-
# always bypass validation in here as
657-
# now automatically done
658-
validate = False
659664

660665
# Convert trace objects to dictionaries
661666
if isinstance(trace, BaseTraceType):
662-
trace = trace.to_plotly_json()
663-
664-
stream_object = dict()
665-
stream_object.update(trace)
666-
if 'type' not in stream_object:
667-
# tests if Scatter contains invalid kwargs
668-
dummy_obj = copy.deepcopy(Scatter(**stream_object))
669-
stream_object = Scatter(**stream_object)
670-
stream_object['type'] = 'scatter'
671-
672-
# TODO: remove this validation as now it's
673-
# done automatically
674-
if validate:
675-
try:
676-
tools.validate(stream_object, stream_object['type'])
677-
except exceptions.PlotlyError as err:
678-
raise exceptions.PlotlyError(
679-
"Part of the data object with type, '{0}', is invalid. "
680-
"This will default to 'scatter' if you do not supply a "
681-
"'type'. If you do not want to validate your data objects "
682-
"when streaming, you can set 'validate=False' in the call "
683-
"to 'your_stream.write()'. Here's why the object is "
684-
"invalid:\n\n{1}".format(stream_object['type'], err)
685-
)
686-
if layout is not None:
687-
try:
688-
tools.validate(layout, 'Layout')
689-
except exceptions.PlotlyError as err:
690-
raise exceptions.PlotlyError(
691-
"Your layout kwarg was invalid. "
692-
"Here's why:\n\n{0}".format(err)
693-
)
694-
del stream_object['type']
667+
stream_object = trace.to_plotly_json()
668+
else:
669+
stream_object = copy.deepcopy(trace)
670+
671+
# Remove 'type' if present since this trace type cannot be changed
672+
stream_object.pop('type', None)
695673

696674
if layout is not None:
697675
stream_object.update(dict(layout=layout))

Diff for: plotly/tests/test_plot_ly/test_stream/test_stream.py

-18
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,6 @@ def test_stream_layout(self):
8888
my_stream.write(Scatter(x=[1], y=[10]), layout=Layout(title=title_1))
8989
my_stream.close()
9090

91-
@attr('slow')
92-
def test_stream_validate_data(self):
93-
with self.assertRaises(ValueError):
94-
py.sign_in(un, ak)
95-
my_stream = py.Stream(tk)
96-
my_stream.open()
97-
my_stream.write(dict(x=[1], y=[10], z=[1])) # assumes scatter...
98-
my_stream.close()
99-
100-
@attr('slow')
101-
def test_stream_validate_layout(self):
102-
with self.assertRaises(ValueError):
103-
py.sign_in(un, ak)
104-
my_stream = py.Stream(tk)
105-
my_stream.open()
106-
my_stream.write(Scatter(x=[1], y=[10]), layout=Layout(legend=True))
107-
my_stream.close()
108-
10991
@attr('slow')
11092
def test_stream_unstreamable(self):
11193

0 commit comments

Comments
 (0)