Forums

ANSI colour codes

I've got a little Python3 program I wrote which does some basic ANSI colour code stuff. But it doesn't seem to work when I run it from a bash console - everything that I print is in the default pale blue text colour, until it gets to a reset code, whereupon it makes everything white from that point on.

I admit that the way I'm doing the colours is probably not very good (just chucked the escape code \033[91m and \033[93m straight into a string), could that be causing the problem? Or is there some kind of terminal variable I need to set? I'm a linux novice - trying to remember things from my first year compsci electives years ago.

Basically I just want to be able to demonstrate this program to people on the web without them having to install Python or SSH into something themselves.

Hi richardlait2012 -- that certainly should work, but it's hard to say what's wrong without seeing the code. Could you share it with us? Either here on the forums, or if you prefer via email at support@pythonanywhere.com

I came across a nice package for working with terminal colours the other day. It's called blessings.

You can install it with

pip-3.2 install --user blessings

In a bash console. It works under 3.2. Just remember to adapt their examples to use the Python 3 print call.

The whole program is too big to stick in a post, but I replicated the behaviour in a much smaller test program (hope I've got the syntax right):

a = '\033[91mR\033[0m'
b = '\033[93mY\033[0m'
print('W', a, b)

When I run this on my own machine (a Raspberry Pi, SSHed through PuTTy) it displays a grey letter W, a red letter R and a yellow letter Y, but when I run it in a PAW bash console, the W and R display in the same pale blue as the prompt and normal shell text, and the Y displays in white. Then everything else in the shell is white from then on, until something else has a specific colour, which resets it all.

Those ANSI codes are only valid if the terminal has 16-colour support, I believe. Try changing the "91m" and "93m" to "31m" and "33m" respectively, which should display the coloured versions without being bold. That seems to work on the PA console.

EDIT: Looks like the bolding works as well, but you can't use the 16-colour variants - instead, explicitly apply the bold attribute with something like \033[01;31m. Of course, the results of bolding can be visually different to using bright variants of colours (though some terminals use the same effect in both cases) but it's rather more portable.

I'd definitely concur with hansel - if you want to have any hope of making your code portable to different terminal types, you want to use a library instead of hard-coding (and on cursory inspection it looks like blessings is certainly rather friendlier than curses, which can only be a good thing).

Still, if you want to see the escape codes available, I've always found this page very useful. Many of the codes only apply to xterms, but it's a very handy reference.

Okay that appears to work on the PAW thing, which is cool, but now I'm damned if I can work out how to install blessings for Python3.2 on my own machine (I am not very Linux-savvy). How do I get pip-3.2? I managed to install pip, but it only installed blessings for Python 2.7. :(

It's a bit convoluted but here you are:

curl -O http://python-distribute.org/distribute_setup.py
python3.2 distribute_setup.py
easy_install-3.2 pip

That should give you a pip-3.2 that you can use on the command line. See? This is why people just use PythonAnywhere :-)

Also you will need to run those as root or with sudo if you use that instead.

Hi Hansel,

The method for installing pip-3.2 no longer works. (Switching for python3.3 in the commandline still ends with a permission denied error)

Thanks Robert

richardlait2012 was asking about installing it on his own machine, so he left out the --user that you would use on PAW. Also, a sudo may be necessary on the last 2 lines depending on your Linux distribution.