Skip to content

traces selection/update methods and "magic underscore" support #1534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 3, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added initial update_traces tests
jonmmease committed Apr 23, 2019
commit 36f017fc0cee827502eb13f0865cf14b31a4bd37
101 changes: 95 additions & 6 deletions plotly/tests/test_core/test_update_traces/test_update_traces.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from __future__ import absolute_import
from unittest import TestCase
import inspect
import copy

import plotly.graph_objs as go
from plotly.subplots import make_subplots
from _plotly_future_ import _future_flags


class TestSelectTraces(TestCase):
class TestSelectForEachUpdateTraces(TestCase):

def setUp(self):
_future_flags.add('v4_subplots')
@@ -109,8 +110,8 @@ def setUp(self):
def tearDown(self):
_future_flags.remove('v4_subplots')

# select_traces
# -------------
# select_traces and for_each_trace
# --------------------------------
def assert_select_traces(self, expected_inds, selector=None, row=None, col=None, test_no_grid=False):
# Select traces on figure initialized with make_subplots
trace_generator = self.fig.select_traces(
@@ -129,12 +130,14 @@ def assert_select_traces(self, expected_inds, selector=None, row=None, col=None,

# Test for each trace
trace_list = []
self.fig.for_each_trace(
for_each_res = self.fig.for_each_trace(
lambda t: trace_list.append(t),
selector=selector,
row=row,
col=col,
)
self.assertIs(for_each_res, self.fig)

self.assertEqual(
trace_list, [self.fig.data[i] for i in expected_inds])

@@ -199,5 +202,91 @@ def test_select_property_and_grid(self):
self.assert_select_traces(
[], selector={'type': 'markers'}, row=3, col=1)

# for_each_trace
# --------------
def test_for_each_trace_lowercase_names(self):
# Names are all uppercase to start
original_names = [t.name for t in self.fig.data]
self.assertTrue([str.isupper(n) for n in original_names])

# Lower case names
result_fig = self.fig.for_each_trace(
lambda t: t.update(name=t.name.lower())
)

# Check chaning
self.assertIs(result_fig, self.fig)

# Check that names were altered
self.assertTrue(
all([t.name == n.lower()
for t, n in zip(result_fig.data, original_names)]))

# test update_traces
# ------------------
def assert_update_traces(
self, patch, expected_inds, selector=None, row=None, col=None
):
# Save off original figure
fig_orig = copy.deepcopy(self.fig)
for trace1, trace2 in zip(fig_orig.data, self.fig.data):
trace1.uid = trace2.uid

# Perform update
update_res = self.fig.update_traces(
patch, selector=selector, row=row, col=col
)

# Check chaining support
self.assertIs(update_res, self.fig)

# Check resulting traces
for i, (t_orig, t) in enumerate(zip(fig_orig.data, self.fig.data)):
if i in expected_inds:
# Check that traces are initially equal
self.assertNotEqual(t_orig, t)

# Check that traces are equal after update
t_orig.update(patch)

# Check that traces are equal
self.assertEqual(t_orig, t)

def test_update_traces_by_type(self):
self.assert_update_traces(
{'visible': 'legendonly'},
[0, 2],
selector={'type': 'scatter'}
)

self.assert_update_traces(
{'visible': 'legendonly'},
[1],
selector={'type': 'bar'},
)

self.assert_update_traces(
{'colorscale': 'Viridis'},
[3],
selector={'type': 'heatmap'}
)

self.assert_update_traces(
{'marker': {'line': {'color': 'yellow'}}},
[4, 5],
selector={'type': 'scatter3d'}
)

self.assert_update_traces(
{'line': {'dash': 'dot'}},
[6, 7],
selector={'type': 'scatterpolar'}
)

self.assert_update_traces(
{'dimensions': {1: {'label': 'Dimension 1'}}},
[8],
selector={'type': 'parcoords'}
)

self.assert_update_traces(
{'hoverinfo': 'label+percent'},
[], selector={'type': 'pie'})