Skip to content

Commit 042b9a9

Browse files
committed
Fix repeated trace name case.
1 parent 7d7ab69 commit 042b9a9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

plotly/graph_objs/graph_objs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,18 +895,20 @@ def get_data(self, flatten=False):
895895
self.to_graph_objs()
896896
data = [v.get_data(flatten=flatten) for v in self]
897897
d = {}
898+
taken_names = []
898899
for i, trace in enumerate(data):
899900

900901
# we want to give the traces helpful names
901902
# however, we need to be sure they're unique too...
902903
trace_name = trace.pop('name', 'trace_{}'.format(i))
903-
if trace_name in d:
904+
if trace_name in taken_names:
904905
j = 1
905906
new_trace_name = "{}_{}".format(trace_name, j)
906-
while new_trace_name in d:
907+
while new_trace_name in taken_names:
907908
new_trace_name = "{}_{}".format(trace_name, j)
908909
j += 1
909910
trace_name = new_trace_name
911+
taken_names.append(trace_name)
910912

911913
# finish up the dot-concatenation
912914
for k, v in trace.items():

plotly/tests/test_core/test_graph_objs/test_get_data.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,13 @@ def test_get_data_flatten(self):
162162
self.assertEqual(flat_data, comp_data)
163163

164164
# TODO test for Data, Scatter, etc..
165+
166+
def test_flatten_repeated_trace_names(self):
167+
dl = Data([Scatter(name='thesame', x=[1, 2, 3]) for _ in range(3)])
168+
data = dl.get_data(flatten=True)
169+
comp_data = {
170+
'thesame.x': [1, 2, 3],
171+
'thesame_1.x': [1, 2, 3],
172+
'thesame_2.x': [1, 2, 3]
173+
}
174+
self.assertEqual(data, comp_data)

0 commit comments

Comments
 (0)