insomnihack

When sleep eludes, the keyboard beckons

IPython Color Console Output

December 16, 2009 Dale 1 Comment

IPython does a decent job of providing console output in color on all the platforms I use it on. If you’re like me and you’ve wanted to use its coloring functionality for your own terminal output, here’s a snippet that should get you started.

[python]
from IPython import ColorANSI
from IPython.genutils import Term
tc = ColorANSI.TermColors()
red = tc.Red + "Red" + tc.Normal
print >> Term.cout, red
blue = tc.Blue + "Blue" + tc.Normal
print >> Term.cout, blue
[/python]

Assuming you have color output enabled (if you’re on Windows, you’ll need PyReadline), you should be able to %paste the plain text from the snippet above into IPython and see the output below.

Red
Blue

Programming IPython, Python

Comments

  1. the1within0 says

    April 3, 2015 at 9:50 AM

    In newer versions of IPython (my version is 2.3.0) the same concept works, its just the imports moved around

    from IPython.utils.coloransi import TermColors as tc
    from IPython.utils import io
    red = tc.Red + “Red” + tc.Normal
    print >>io.stdout, red
    blue = tc.Blue + “Blue” + tc.Normal
    print >> io.stdout, blue

    I hope this helps all who are still trying to do this on Windows.

Leave a Reply

Your email address will not be published. Required fields are marked *

About Me

Hi, my name's Dale, and this is my tech blog. Read More…

Search

Tags

blogging c# database Django documentation Email furniture git hacks htpc IPython Linux mac nas packaging PyCharm Python Roleplaying tools twitter unit tests virtualenv Visual Studio vmware Windows

Copyright © 2025 · Equilibre Theme on Genesis Framework · WordPress · Log in