Skip to content

Commit 10be9df

Browse files
committed
Merge branch 'master' into colorscale-sampling
# Conflicts: # packages/python/plotly/_plotly_utils/colors/__init__.py # packages/python/plotly/plotly/tests/test_core/test_colors/test_colors.py
2 parents fb2c139 + db920c2 commit 10be9df

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

Diff for: packages/python/plotly/_plotly_utils/colors/__init__.py

+27
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,33 @@ def named_colorscales():
808808
return [c for c in ColorscaleValidator("", "").named_colorscales]
809809

810810

811+
def get_colorscale(name):
812+
"""
813+
Returns the colorscale for a given name. See `named_colorscales` for the
814+
built-in colorscales.
815+
"""
816+
from _plotly_utils.basevalidators import ColorscaleValidator
817+
818+
if not isinstance(name, str):
819+
raise exceptions.PlotlyError("Name argument have to be a string.")
820+
821+
name = name.lower()
822+
if name[-2:] == "_r":
823+
should_reverse = True
824+
name = name[:-2]
825+
else:
826+
should_reverse = False
827+
828+
if name in ColorscaleValidator("", "").named_colorscales:
829+
colorscale = ColorscaleValidator("", "").named_colorscales[name]
830+
else:
831+
raise exceptions.PlotlyError(f"Colorscale {name} is not a built-in scale.")
832+
833+
if should_reverse:
834+
return colorscale[::-1]
835+
return colorscale
836+
837+
811838
def sample_colorscale(colorscale, samplepoints, low=0.0, high=1.0, colortype="rgb"):
812839
"""
813840
Samples a colorscale at specific points.

Diff for: packages/python/plotly/plotly/colors/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@
4646
"plotlyjs",
4747
"DEFAULT_PLOTLY_COLORS",
4848
"PLOTLY_SCALES",
49+
"get_colorscale",
4950
]

Diff for: packages/python/plotly/plotly/io/_html.py

-5
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,6 @@ def write_html(
423423
require an active internet connection in order to load the plotly.js
424424
library.
425425
426-
If 'directory', a script tag is included that references an external
427-
plotly.min.js bundle that is assumed to reside in the same
428-
directory as the HTML file. If `file` is a string to a local file path
429-
and `full_html` is True then
430-
431426
If 'directory', a script tag is included that references an external
432427
plotly.min.js bundle that is assumed to reside in the same
433428
directory as the HTML file. If `file` is a string to a local file

Diff for: packages/python/plotly/plotly/tests/test_core/test_colors/test_colors.py

+26
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,32 @@ def test_make_colorscale(self):
138138
PlotlyError, pattern2, colors.make_colorscale, color_list2, scale
139139
)
140140

141+
def test_get_colorscale(self):
142+
143+
# test for incorrect input type
144+
pattern = "Name argument have to be a string."
145+
name = colors.sequential.haline
146+
147+
self.assertRaisesRegexp(PlotlyError, pattern, colors.get_colorscale, name)
148+
149+
# test for non-existing colorscale
150+
pattern = r"Colorscale \S+ is not a built-in scale."
151+
name = "foo"
152+
self.assertRaisesRegex(PlotlyError, pattern, colors.get_colorscale, name)
153+
154+
# test non-capitalised access
155+
self.assertEqual(colors.sequential.haline, colors.get_colorscale("haline"))
156+
# test capitalised access
157+
self.assertEqual(colors.diverging.Earth, colors.get_colorscale("Earth"))
158+
# test accessing non-capitalised scale with capitalised name
159+
self.assertEqual(colors.cyclical.mrybm, colors.get_colorscale("Mrybm"))
160+
# test accessing capitalised scale with non-capitalised name
161+
self.assertEqual(colors.sequential.Viridis, colors.get_colorscale("viridis"))
162+
# test accessing reversed scale
163+
self.assertEqual(
164+
colors.diverging.Portland_r, colors.get_colorscale("portland_r")
165+
)
166+
141167
def test_sample_colorscale(self):
142168

143169
# test that sampling a colorscale at the defined points returns the same

0 commit comments

Comments
 (0)