Skip to content

Commit a123026

Browse files
committed
Add support for specifying fig.data as a scalar trace.
1 parent 6a44329 commit a123026

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

packages/python/plotly/_plotly_utils/basevalidators.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,6 +2268,8 @@ def description(self):
22682268
that may be specified as:
22692269
- A list or tuple of trace instances
22702270
(e.g. [Scatter(...), Bar(...)])
2271+
- A single trace instance
2272+
(e.g. Scatter(...), Bar(...), etc.)
22712273
- A list or tuple of dicts of string/value properties where:
22722274
- The 'type' property specifies the trace type
22732275
{trace_types}
@@ -2302,7 +2304,10 @@ def validate_coerce(self, v, skip_invalid=False):
23022304

23032305
if v is None:
23042306
v = []
2305-
elif isinstance(v, (list, tuple)):
2307+
else:
2308+
if not isinstance(v, (list, tuple)):
2309+
v = [v]
2310+
23062311
trace_classes = tuple(self.class_map.values())
23072312

23082313
res = []
@@ -2355,12 +2360,6 @@ def validate_coerce(self, v, skip_invalid=False):
23552360
for trace in v:
23562361
trace.uid = str(uuid.uuid4())
23572362

2358-
else:
2359-
if skip_invalid:
2360-
v = []
2361-
else:
2362-
self.raise_invalid_val(v)
2363-
23642363
return v
23652364

23662365

packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_figure.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,10 @@ def test_add_trace_underscore_kwarg(self):
139139
fig.add_scatter(y=[2, 1, 3], marker_line_color='green')
140140

141141
self.assertEqual(fig.data[0].marker.line.color, 'green')
142+
143+
def test_scalar_trace_as_data(self):
144+
fig = go.Figure(data=go.Waterfall(y=[2, 1, 3]))
145+
self.assertEqual(fig.data, (go.Waterfall(y=[2, 1, 3]),))
146+
147+
fig = go.Figure(data=dict(type='waterfall', y=[2, 1, 3]))
148+
self.assertEqual(fig.data, (go.Waterfall(y=[2, 1, 3]),))

0 commit comments

Comments
 (0)