From 0e36c5deb1d3a38a948ffb8136c67891bc767280 Mon Sep 17 00:00:00 2001 From: chavlin Date: Thu, 13 Jun 2024 10:20:29 -0500 Subject: [PATCH 1/3] display the colormaps in docs --- docs/source/api.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/source/api.md b/docs/source/api.md index 46ec641..08288ac 100644 --- a/docs/source/api.md +++ b/docs/source/api.md @@ -1,3 +1,12 @@ +--- +jupytext: + text_representation: + format_name: myst +kernelspec: + display_name: Python 3 + name: python3 +--- + # Reference ## Color Vision Deficiency Friendly Colormaps (`cmweather.cm_colorblind`) @@ -9,6 +18,18 @@ :show-inheritance: ``` +```{code-cell} ipython3 +:tags: [remove-input] + +from IPython.display import HTML +from cmweather.cm_colorblind import cmap_d + +cm_names = [cnm for cnm in cmap_d.keys() if not cnm.endswith('_r')] + +for cm_name in cm_names: + display(HTML(cmap_d[cm_name]._repr_html_())) +``` + ## More Weather Colormaps (`cmweather.cm`) ```{eval-rst} @@ -17,3 +38,16 @@ :undoc-members: :show-inheritance: ``` + +```{code-cell} ipython3 +:tags: [remove-input] + +from IPython.display import HTML +from cmweather.cm import cmap_d + +cm_names = [cnm for cnm in cmap_d.keys() if not cnm.endswith('_r')] +cm_names.sort() + +for cm_name in cm_names: + display(HTML(cmap_d[cm_name]._repr_html_())) +``` From 7cf2e6cc92bfe8e385c285a01a679242defd22a3 Mon Sep 17 00:00:00 2001 From: mgrover1 Date: Fri, 14 Jun 2024 16:13:59 -0600 Subject: [PATCH 2/3] FIX: Fix the linting error and change order of pages --- docs/source/api.md | 10 +++++----- docs/source/index.md | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/source/api.md b/docs/source/api.md index 08288ac..15a95de 100644 --- a/docs/source/api.md +++ b/docs/source/api.md @@ -26,9 +26,9 @@ from cmweather.cm_colorblind import cmap_d cm_names = [cnm for cnm in cmap_d.keys() if not cnm.endswith('_r')] -for cm_name in cm_names: - display(HTML(cmap_d[cm_name]._repr_html_())) -``` +for cm_name in cm_names: + display(HTML(cmap_d[cm_name]._repr_html_())) +``` ## More Weather Colormaps (`cmweather.cm`) @@ -48,6 +48,6 @@ from cmweather.cm import cmap_d cm_names = [cnm for cnm in cmap_d.keys() if not cnm.endswith('_r')] cm_names.sort() -for cm_name in cm_names: - display(HTML(cmap_d[cm_name]._repr_html_())) +for cm_name in cm_names: + display(HTML(cmap_d[cm_name]._repr_html_())) ``` diff --git a/docs/source/index.md b/docs/source/index.md index 1826edc..2ca491a 100644 --- a/docs/source/index.md +++ b/docs/source/index.md @@ -9,8 +9,8 @@ caption: Getting Started hidden: --- usage -contributing api +contributing ``` ## Feedback From 0982a257c045cf26e71534bcd7fc7b151aba10f4 Mon Sep 17 00:00:00 2001 From: chavlin Date: Tue, 18 Jun 2024 15:35:56 -0500 Subject: [PATCH 3/3] add _get_cmap_gallery_html function --- cmweather/cm.py | 51 ++++++++++++++++++++++++++++++++++++++ docs/source/api.md | 16 +++++------- tests/test_cm_html_repr.py | 13 ++++++++++ 3 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 tests/test_cm_html_repr.py diff --git a/cmweather/cm.py b/cmweather/cm.py index 08e3ef5..8027781 100644 --- a/cmweather/cm.py +++ b/cmweather/cm.py @@ -143,3 +143,54 @@ def _generate_cmap(name, lutsize): mpl.colormaps.register(name=name, cmap=cmap, force=True) except AttributeError: mpl.cm.register_cmap(name=name, cmap=cmap) + + +def _get_cmap_gallery_html(cmaps, sort_d=False): + """ + return a html str representation of a colormap dictionary + + use with either cmap_d from either .cm or .cm_colorblind, + reversed colormaps are excluded. The html repr of + individual colormaps is based on what the base + matplotlib.colors.Colormap._repr_html_() returns but + without the "over", "under" and "bad" labels. + + Parameters + ---------- + cmaps: dict + a dictionary of colormaps + sort_d: bool + if True (default False), will sort the cmaps by name + + Returns + ------- + str + the concatenated html str for the input colormap dict + + """ + import base64 + + def _get_cmap_div(cmap): + png_bytes = cmap._repr_png_() + png_base64 = base64.b64encode(png_bytes).decode('ascii') + + return ( + '
' + f'{cmap.name} ' + '
' + '
' + ) + + cm_names = [cnm for cnm in cmaps.keys() if not cnm.endswith('_r')] + if sort_d: + cm_names.sort() + + html_str = '' + for cm_name in cm_names: + html_str += _get_cmap_div(cmaps[cm_name]) + + return html_str diff --git a/docs/source/api.md b/docs/source/api.md index 15a95de..8787418 100644 --- a/docs/source/api.md +++ b/docs/source/api.md @@ -23,11 +23,10 @@ kernelspec: from IPython.display import HTML from cmweather.cm_colorblind import cmap_d +from cmweather.cm import _get_cmap_gallery_html -cm_names = [cnm for cnm in cmap_d.keys() if not cnm.endswith('_r')] - -for cm_name in cm_names: - display(HTML(cmap_d[cm_name]._repr_html_())) +html_str = _get_cmap_gallery_html(cmap_d, sort_d=False) +display(HTML(html_str)) ``` ## More Weather Colormaps (`cmweather.cm`) @@ -43,11 +42,8 @@ for cm_name in cm_names: :tags: [remove-input] from IPython.display import HTML -from cmweather.cm import cmap_d - -cm_names = [cnm for cnm in cmap_d.keys() if not cnm.endswith('_r')] -cm_names.sort() +from cmweather.cm import cmap_d, _get_cmap_gallery_html -for cm_name in cm_names: - display(HTML(cmap_d[cm_name]._repr_html_())) +html_str = _get_cmap_gallery_html(cmap_d, sort_d=True) +display(HTML(html_str)) ``` diff --git a/tests/test_cm_html_repr.py b/tests/test_cm_html_repr.py new file mode 100644 index 0000000..37e25d8 --- /dev/null +++ b/tests/test_cm_html_repr.py @@ -0,0 +1,13 @@ +import pytest + +from cmweather import cm, cm_colorblind + + +@pytest.mark.parametrize('cmap_d,sort_d', [(cm.cmap_d, True), (cm_colorblind.cmap_d, False)]) +def test_get_cmap_gallery_html(cmap_d, sort_d): + html_str = cm._get_cmap_gallery_html(cmap_d, sort_d=sort_d) + + assert isinstance(html_str, str) + assert len(html_str) > 0 + assert html_str.startswith('')