Skip to main content

Colorain v0.0.52

Ever wondered how some people make those console programmes print in pretty colours? Well, the answer is simple: there are ANSI colour codes with which you have to tag up the text. The problem is, these codes are pretty cumbersome to remember and are not built into Python either. And so typically people write bland, all-whites console programmes. Colorain is a package that makes printing coloured text to the console a piece of cake. Using a simple markup designed just for styling text on the terminal, Colorain allows you to quickly add styles to any Python console project you might have. There's a few other packages that help you to print styled text with Python, but none with the ease of use as Colorain (well, at least to me :p).

Installation

pip install colorain

Usage

Using a very simple markup that kinda looks like HTML, you can easily modify the colour of different parts of some text. The general syntax is as follows: <f=C>Text</> colours the foreground with colour C, i.e. the text, <b=C>Text</> colours the background with colour C, and <f=C1;b=C2>Text</> or <b=C2;f=C1> colours the foreground with C1 and the background with C2.

A Hello World Example

from colorain import *
# parses the colour tags and colour-codes the text as needed
txt = StyledText("<f=y;b=r;B;I;U>Hello world</>") 
print(txt)

screenshot-1

Getting Help

You can use the styles_guide() helper function to view all the available styles and the tags for them.

styles_guide()

screenshot-4

The Syntax

The core class in colorain is the StyledText class. The constructor takes a string marked up with the colorain markup and parses that to generate styled strings. The markup is fairly similar in spirit to HTML, and so is pretty simple to remember. For styling some part of a text, tag up the beginning and end of that part by the start tag/token and the end tag/token. The end tag is always the same: </>. The start tag specifies styling properties, called props. In the above code, f=y tells colorain to colour the foreground, i.e., the text itself, with 'y' i.e. yellow. Similarly, b=r tells colorain to paint the background with red. The following props, B, I, and U, tell colorain to make the text bold, italic, and underlined. You don't have to specify the props in any specific order. The start tag must contain at least one styling prop. If you're using more than one prop, make sure you separate them with semicolons.

In case you want several differently styled texts at a stretch, you don't strictly have to put an end tag after each; you can just place the end tag at the end of all of them. However, do note the slight inconsistency that arises from this. This is due to the nature of the ANSI codes, and so it's recommended that you close each tag separately. (I might add some more error checking to the library to detect if all tags have been closed. At present, it only checks if there's at least one end tag, if there is(are) start tag(s).)

title1 = StyledText("<f=r>c <f=o>o <f=y>l <f=g>o <f=b>r <f=gr>a <f=lr>i <f=c>n  </>")
print(title1)


title2 = StyledText("<f=r>c</> <f=o>o</> <f=y>l</> <f=g>o</> <f=b>r</> <f=gr>a</> <f=lr>i</> <f=c>n</>")
print(title2)

screenshot-2

Useful Features

parse_color() and raw_text(): Any StyledText object has a couple of methods you might find useful. StyledText.parse_color() gives you the parsed output string, i.e., ANSI colour-coded string. This is the string you might directly print using the print() function, and the output will be coloured as required. This is useful, for example, if you want to copy the ANSI coded string. You could send that to any friend of yours who hasn't installed colorain and they could copy the string to directly print the styled text.

In case you want to strip the text of all styles, the StyledText.raw_text() method is what you're looking for. (By the way, in the following snippet I'm using a third-party module called clipboard. You should check that out if you haven't already - really great for automating certain tasks!)

import clipboard
print(title1.parse_color())
clipboard.copy(title1.parse_color()) # copies the ANSI coded string to your clipboard
print(title1.raw_text())
clipboard.copy(title1.raw_text()) # copies an unstyled version of the string to your clipboard

screenshot-3

Wrapper Classes: In case you have to style short stretches of text, using the markup is very easy. However, what if you had a long stretch of text styled the same way? It might become kinda tedious to remember to put in the end tag. If that's what you're worried about, then you can use the wrapper classes built into colorain, all of which inherit from StyledText.

print(FGYellow("""\
    In case you have to style short stretches of text, using the markup is very easy. However, what if you had a long
    stretch of text in the same way? It might become kinda tedious to remember to put in the end tag. If that's what 
    you're worried about, then you can use the wrapper classes built into colorain.\
"""))
print(FGLtRed("""\
    In case you have to style short stretches of text, using the markup is very easy. However, what if you had a long
    stretch of text in the same way? It might become kinda tedious to remember to put in the end tag. If that's what 
    you're worried about, then you can use the wrapper classes built into colorain.\
"""))
print(Bold("""\
    In case you have to style short stretches of text, using the markup is very easy. However, what if you had a long
    stretch of text in the same way? It might become kinda tedious to remember to put in the end tag. If that's what 
    you're worried about, then you can use the wrapper classes built into colorain.\
"""))
print(Italic("""\
    In case you have to style short stretches of text, using the markup is very easy. However, what if you had a long
    stretch of text in the same way? It might become kinda tedious to remember to put in the end tag. If that's what 
    you're worried about, then you can use the wrapper classes built into colorain.\
"""))

screenshot-5

Adding together styled strings: colorain overloads the "+" operator for StyledText objects so that you can manipulate StyledText objects as you manipulate regular strings.

print(FGYellow("""\
    In case you have to style short stretches of text, using the markup is very easy. However, what if you had a long
    stretch of text in the same way? It might become kinda tedious to remember to put in the end tag. If that's what 
    you're worried about, then you can use the wrapper classes built into colorain.
""") + FGLtRed("""\
    In case you have to style short stretches of text, using the markup is very easy. However, what if you had a long
    stretch of text in the same way? It might become kinda tedious to remember to put in the end tag. If that's what 
    you're worried about, then you can use the wrapper classes built into colorain.
""")+ Bold("""\
    In case you have to style short stretches of text, using the markup is very easy. However, what if you had a long
    stretch of text in the same way? It might become kinda tedious to remember to put in the end tag. If that's what 
    you're worried about, then you can use the wrapper classes built into colorain.
""")+ Italic("""\
    In case you have to style short stretches of text, using the markup is very easy. However, what if you had a long
    stretch of text in the same way? It might become kinda tedious to remember to put in the end tag. If that's what 
    you're worried about, then you can use the wrapper classes built into colorain.\
"""))

screenshot-6

Errors

If you mess something up in the markup, there's some (slightly) informative errors that might help you with debugging.

print(StyledText("""\
    <f=>In case you have to style short stretches of text, using the markup is very easy. However, what if you had a long
    stretch of text in the same way? It might become kinda tedious to remember to put in the end tag. If that's what 
    you're worried about, then you can use the wrapper classes built into colorain.</>\
"""))

screenshot-7 screenshot-8 screenshot-9 screenshot-10

Lastly, the entire thing is a work in progress by an amateur coder. Please let me know if you find any errors or have any suggestions! Thank you!

Metadata

Release files for colorain 0.0.52

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for colorain 0.0.52
File Size Uploaded Provenance Actions
colorain-0.0.52.tar.gz 12.3 kB Without provenance Details for colorain-0.0.52.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for colorain 0.0.52
File Interpreter ABI Platform Actions
colorain-0.0.52-py3-none-any.whl Python 3 none any Details for colorain-0.0.52-py3-none-any.whl

Total release size: 25.1 kB

Release files / colorain-0.0.52.tar.gz

Download URL colorain-0.0.52.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
819c2af965e6aac7af7050d665e72442b197d70e516a754abcd004aeb8f5f11c
BLAKE2b-256 checksum
How to use checksums
279044b4bbd0687a064e90c1742f380674b1c5b71d03a69ba4fab7844a0694cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.8

Release files / colorain-0.0.52-py3-none-any.whl

Download URL colorain-0.0.52-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f3fbd5564560d1a50e8587c7f61d7a6f31be0c9733ed9c65f43207c85332c56d
BLAKE2b-256 checksum
How to use checksums
929ada58969ce87131c0ece6aa9b8a5c76d644ffb9059f6e6d355d6565ae2d60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.8

Security for colorain 0.0.52

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.52 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page