4

When using R in the console, one can use the package colorout to highlight R outputs.

Does there exist such a thing for Python output? I am mostly interested in:

  • easily distinguishing commands lines from output lines
  • easily distinguishing between list, dict, int, str, objects, bare print outputs etc.
  • easily spotting Python error output
  • making the console feel more comfortable to the eye
2
  • Far away from optimum, you can use xterm escape sequences for coloring on linux, but you would have to turn it off on windows. the first link which I found about it: wiki.archlinux.org/index.php/Color_Bash_Prompt Commented Oct 13, 2015 at 10:41
  • @danielalder The Python package colorama colors console outputs on Linux and Windows. On Linux escape sequences are used, on Windows it should be WinAPI calls.
    – Paebbels
    Commented Oct 15, 2015 at 18:56

2 Answers 2

6

Yes, it could. There are two python script that could do so: bpython and ipython. You may start them either directly or from python interpreter.

bpython

bpython shot

ipython qtconsole

ipython shot

1
  • Wow, these are great! Funny though how their philosophy differs from colorout in that they color input not output (I guess this is because one quite doesn't use output for the same things in R and Python). Just before I mark the answer as accepted, do you know whether there are options in IPython or BPython to parse and color output? Like distinguishing list from dictionnaries with colors, or strings from ints?
    – iago-lito
    Commented Oct 14, 2015 at 8:10
2

Simply install colorama with pip install colorama then you can use it with:

import colorama
colorama.init()
print (colorama.Fore.RED + 'some red text')
print (colorama.Back.GREEN + 'and with a green background')

Screenshot

If you would like a simpler way of coloring output, among other things, you should take a look at click and the secho() command which detects if colorama is installed and uses it automatically, it also handles stripping out the color when output is being rediriected, etc.

import click
click.clear()  # Clear the terminal window
click.secho('Hello World!', fg='green')
click.secho('Some more text', bg='blue', fg='white')
click.secho('ATTENTION', blink=True, bold=True)

enter image description here

2
  • This does not parse your output, does it? I guess I would then have to hightlight myself strings, numerics, exceptions etc.
    – iago-lito
    Commented Oct 13, 2015 at 13:42
  • 1
    @lago-lito If you want to have syntax highlighting, you should call it by its name, the way how you describe it is easy to overlook (i did and steve also did) Commented Oct 15, 2015 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.