Skip to content

Commit

Permalink
add _get_cmap_gallery_html function
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishavlin committed Jun 18, 2024
1 parent 7cf2e6c commit 0982a25
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
51 changes: 51 additions & 0 deletions cmweather/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
'<div style="vertical-align: middle;">'
f'<strong>{cmap.name}</strong> '
'</div>'
'<div class="cmap"><img '
f'alt="{cmap.name} colormap" '
f'title="{cmap.name}" '
'style="border: 1px solid #555;" '
f'src="data:image/png;base64,{png_base64}"></div>'
)

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
16 changes: 6 additions & 10 deletions docs/source/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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))
```
13 changes: 13 additions & 0 deletions tests/test_cm_html_repr.py
Original file line number Diff line number Diff line change
@@ -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('<div')
assert html_str.endswith('</div>')

0 comments on commit 0982a25

Please sign in to comment.