Skip to content

Instantly share code, notes, and snippets.

@wmayner
Last active July 17, 2023 01:00
Show Gist options
  • Save wmayner/9b099a0e4a5f8e94f0c6ab2f570187a5 to your computer and use it in GitHub Desktop.
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
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
)))
@arnicas
Copy link

arnicas commented Jul 7, 2022

Thank you for this!

@ryan-williams
Copy link

ryan-williams commented Dec 10, 2022

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
    )))    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment