printing bold, colored, etc., text in ipython qtconsole

In Jupyter Notebooks, one clean way of solving this problem is using markdown:

from IPython.display import Markdown, display
def printmd(string):
    display(Markdown(string))

And then do something like:

printmd("**bold text**")

Of course, this is great for bold, italics, etc., but markdown itself does not implement color. However, you can place html in your markdown, and get something like this:

printmd("<span style="color:red">Red text</span>")

You could also wrap this in the printmd function :

def printmd(string, color=None):
    colorstr = "<span style="color:{}">{}</span>".format(color, string)
    display(Markdown(colorstr))

And then do cool things like

printmd("**bold and blue**", color="blue")

For the colors, you can use the hexadecimal notation too (eg. color = "#00FF00" for green)

To clarify, although we use markdown, this is a code cell: you can do things like:

for c in ('green', 'blue', 'red', 'yellow'):
    printmd("Writing in {}".format(c), color=c)

Of course, a drawback of this method is the reliance on being within a Jupyter notebook.

Leave a Comment