Skip to content

Commit

Permalink
Fix ColorScale presentation when specified as string (#1089)
Browse files Browse the repository at this point in the history
* Added tests for colorscales specified as a string

Added tests checking that a colorscale specified as a string is returned correctly. Previously it had been returned as a tuple of 1-tuples. e.g. "Viridis" -> (('V',), ('i',), ('r',), ('i',), ('d',), ('i',), ('s',)). Catches #1087.

* Fix presentation of string colorscales

Fixes #1087.

* Added support for Cividis to ColorScaleValidator
  • Loading branch information
ivirshup authored and jonmmease committed Aug 6, 2018
1 parent 1c75712 commit 313129b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
8 changes: 5 additions & 3 deletions _plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ class ColorscaleValidator(BaseValidator):
named_colorscales = [
'Greys', 'YlGnBu', 'Greens', 'YlOrRd', 'Bluered', 'RdBu', 'Reds',
'Blues', 'Picnic', 'Rainbow', 'Portland', 'Jet', 'Hot', 'Blackbody',
'Earth', 'Electric', 'Viridis'
'Earth', 'Electric', 'Viridis', 'Cividis'
]

def __init__(self, plotly_name, parent_name, **kwargs):
Expand All @@ -1229,7 +1229,7 @@ def description(self):
- One of the following named colorscales:
['Greys', 'YlGnBu', 'Greens', 'YlOrRd', 'Bluered', 'RdBu',
'Reds', 'Blues', 'Picnic', 'Rainbow', 'Portland', 'Jet',
'Hot', 'Blackbody', 'Earth', 'Electric', 'Viridis']
'Hot', 'Blackbody', 'Earth', 'Electric', 'Viridis', 'Cividis]
""".format(plotly_name=self.plotly_name)

return desc
Expand Down Expand Up @@ -1274,9 +1274,11 @@ def validate_coerce(self, v):
return v

def present(self, v):
# Return tuple of tuples so that colorscale is immutable
# Return-type must be immutable
if v is None:
return None
elif isinstance(v, string_types):
return v
else:
return tuple([tuple(e) for e in v])

Expand Down
5 changes: 4 additions & 1 deletion _plotly_utils/tests/validators/test_colorscale_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def validator():


@pytest.fixture(params=['Greys', 'YlGnBu', 'Greens', 'YlOrRd', 'Bluered', 'RdBu', 'Reds', 'Blues',
'Picnic', 'Rainbow', 'Portland', 'Jet', 'Hot', 'Blackbody', 'Earth', 'Electric', 'Viridis'])
'Picnic', 'Rainbow', 'Portland', 'Jet', 'Hot', 'Blackbody', 'Earth', 'Electric',
'Viridis', 'Cividis'])
def named_colorscale(request):
return request.param

Expand All @@ -26,6 +27,8 @@ def test_acceptance_named(named_colorscale, validator: ColorscaleValidator):
# Uppercase
assert (validator.validate_coerce(named_colorscale.upper()) ==
named_colorscale.upper())

assert validator.present(named_colorscale) == named_colorscale

# ### Acceptance as array ###
@pytest.mark.parametrize('val', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ def test_present_colorscale(self):
# Presented as tuple of tuples
self.assertEqual(self.scatter.marker.colorscale,
((0, 'red'), (1, 'green')))

def test_present_colorscale_str(self):
self.assertIsNone(self.scatter.marker.colorscale)

# Assign string
self.scatter.marker.colorscale = "Viridis"

# Presented as a string
self.assertEqual(self.scatter.marker.colorscale,
"Viridis")


class TestPropertyIterContains(TestCase):
Expand Down

0 comments on commit 313129b

Please sign in to comment.