Skip to content

Commit 313129b

Browse files
ivirshupjonmmease
authored andcommitted
Fix ColorScale presentation when specified as string (plotly#1089)
* 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 plotly#1087. * Fix presentation of string colorscales Fixes plotly#1087. * Added support for Cividis to ColorScaleValidator
1 parent 1c75712 commit 313129b

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

Diff for: _plotly_utils/basevalidators.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ class ColorscaleValidator(BaseValidator):
12111211
named_colorscales = [
12121212
'Greys', 'YlGnBu', 'Greens', 'YlOrRd', 'Bluered', 'RdBu', 'Reds',
12131213
'Blues', 'Picnic', 'Rainbow', 'Portland', 'Jet', 'Hot', 'Blackbody',
1214-
'Earth', 'Electric', 'Viridis'
1214+
'Earth', 'Electric', 'Viridis', 'Cividis'
12151215
]
12161216

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

12351235
return desc
@@ -1274,9 +1274,11 @@ def validate_coerce(self, v):
12741274
return v
12751275

12761276
def present(self, v):
1277-
# Return tuple of tuples so that colorscale is immutable
1277+
# Return-type must be immutable
12781278
if v is None:
12791279
return None
1280+
elif isinstance(v, string_types):
1281+
return v
12801282
else:
12811283
return tuple([tuple(e) for e in v])
12821284

Diff for: _plotly_utils/tests/validators/test_colorscale_validator.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ def validator():
1111

1212

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

@@ -26,6 +27,8 @@ def test_acceptance_named(named_colorscale, validator: ColorscaleValidator):
2627
# Uppercase
2728
assert (validator.validate_coerce(named_colorscale.upper()) ==
2829
named_colorscale.upper())
30+
31+
assert validator.present(named_colorscale) == named_colorscale
2932

3033
# ### Acceptance as array ###
3134
@pytest.mark.parametrize('val', [

Diff for: plotly/tests/test_core/test_graph_objs/test_properties_validated.py

+10
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ def test_present_colorscale(self):
102102
# Presented as tuple of tuples
103103
self.assertEqual(self.scatter.marker.colorscale,
104104
((0, 'red'), (1, 'green')))
105+
106+
def test_present_colorscale_str(self):
107+
self.assertIsNone(self.scatter.marker.colorscale)
108+
109+
# Assign string
110+
self.scatter.marker.colorscale = "Viridis"
111+
112+
# Presented as a string
113+
self.assertEqual(self.scatter.marker.colorscale,
114+
"Viridis")
105115

106116

107117
class TestPropertyIterContains(TestCase):

0 commit comments

Comments
 (0)