Last active
January 6, 2025 14:04
-
-
Save wmayner/9b099a0e4a5f8e94f0c6ab2f570187a5 to your computer and use it in GitHub Desktop.
Display a color swatch from hex color strings with IPython in a Jupyter Notebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.display import Markdown, display | |
colors = ['#018700', '#00acc6', '#e6a500'] | |
display(Markdown('<br>'.join( | |
f'<span style="font-family: monospace">{color} <span style="color: {color}">████████</span></span>' | |
for color in colors | |
))) |
Thank you for this!
Thanks for this. It took me some investigating to see that the color bar is chr(9608) * 8
, a.k.a. 0x259F / "█" / "Full Block".
I made myself this helper, which puts them on the same line, and 6 "spaces" wide, by default:
from IPython.display import Markdown, display
def swatches(colors, sep=' ', width=6):
display(Markdown(sep.join(
f'<span style="font-family: monospace">{color} <span style="color: {color}">{chr(9608)*width}</span></span>'
for color in colors
)))
Note that Google Collaboratory sanitizes HTML in Markdown. I'd recommend using Html instead of Markdown, which I think makes the intent more clear anyway.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!