Skip to main content

constyle

A Python library to add style to your console.

The name of the library comes from merging the words CONSoLE and STYLE. Also "con" means "with" in Spanish.

Installation

You can install this package with pip or conda.

$ pip install constyle
$ conda install -c conda-forge constyle
$ conda install -c abrahammurciano constyle

Links

Documentation

Source Code - GitHub

PyPI - constyle

Anaconda - constyle

Discord - Community

Usage

There are a couple of ways to use this library.

The style function

The simplest way is with the style function.

from constyle import style, Attributes

print(style('Hello World', Attributes.GREEN, Attributes.BOLD, Attributes.ON_BLUE))

Style objects

You can also use Style objects to create a reusable style with any number of attributes.

Calling a Style object

Style objects are callable and take a string as input (or any other object, which will be converted to a string) and return a styled string.

warning = Style(Attributes.YELLOW, Attributes.BOLD)
print(warning('You shall not pass!'))

Adding Style objects

Adding together Style objects will also create Style objects.

whisper = Attributes.GREY + Attributes.DIM + Attributes.SUPERSCRIPT
print(whisper('Fly you fools'))

Converting Style objects to strings

Style objects can be converted to strings to obtain the ANSI escape sequence for that style.

warning = Style(Attributes.YELLOW, Attributes.BOLD)
print(f"{warning}You shall not pass!{Attributes.RESET}")

Attributes

The Attributes enum contains all the available ANSI attributes. You can read more about them here.

Attributes are also Style objects, and as such, as demonstrated above, they too can be called to style a string, added together and to other Style objects, and converted to strings to obtain their ANSI sequence.

You'll find there is limited support for all the ANSI attributes among some consoles.

If you find more attributes that aren't provided in this enum, you can create your own by constructing a Style with an integer.

Nesting

In order to nest styles, you can use the end= keyword argument of the style function or the Style class. Usually when applying a style, the RESET attribute is appended to the end. This can be undesirable when nesting (see the example below).

bold = Attributes.BOLD
yellow = Attributes.YELLOW
green = Attributes.GREEN

print(yellow(bold('This is bold and yellow')))
print(green(f"This is green. {yellow('This is yellow.')} This is no longer green"))

In order to achieve the desired result in the above example, you would have to use the end= keyword argument of the style function. You can pass any Style to end.

print(green(f"This is green. {bold('This is green and bold.', end=Attributes.NO_BOLD)} This is still green but not bold anymore"))
print(green(f"This is green. {yellow('This is yellow.', end=green)} This is now green again"))

Custom colours

The constyle.custom_colours module contains a few classes that can be used to create custom colours.

RGB colours

You can create a Style for a custom RGB colour by using the RGB class. This is not well supported by all consoles.

from constyle.custom_colours import RGB

print(style('This is pink', RGB(255, 192, 203)))

8-bit colours

Some consoles support 8-bit colours. You can create a Style for an 8-bit colour by using the EightBit class, passing a single integer to it, or you can use the EightBitRGB class to create an 8-bit colour style as close to the RGB values as possible.

The command line interface

This package also provides a very basic command line interface to print styled strings.

You can pass it any number of strings and it will print them all together (like echo). You can pass --attribute (or -a) with the name of an attribute to apply to the other strings being printed. You can pass --attribute as many times as you like.

You can use constyle --help to see more specific details, as well as all available attributes.

For example you can use constyle from your shell to print some styled text.

$ constyle Hello World! -a green -a bold -a on_white

Or if you're writing a shell script you can make an alias or a function to reuse a certain style.

#!/bin/bash
alias error="constyle --attribute bold --attribute red" # With an alias
warn() { constyle $@ -a bold -a yellow } # With a function
error You shall not pass!
warn Fly you fools!

Metadata

Release files for constyle 2.0.5

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

Source distribution (sdist)

Source distribution for constyle 2.0.5
File Size Uploaded
constyle-2.0.5.tar.gz 21.5 kB Details

Built distribution (wheel)

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

Total release size: 42.9 kB

Release files / constyle-2.0.5.tar.gz

Download URL constyle-2.0.5.tar.gz
Size 21.5 kB
Tags Source
SHA-256 checksum
How to use checksums
842009155b6629eb7b40f32c6fff7b79853109f79f2a9d1821b8808b9c7e188c
BLAKE2b-256 checksum
How to use checksums
87b2217fea582000cb8e6637b1ccdba104e19dec2ff6fbdaac9c1317cac97b5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Linux/6.5.0-1021-azure

Release files / constyle-2.0.5-py3-none-any.whl

Download URL constyle-2.0.5-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89cc0fc352c7935f3b0a44e0c354b9514aa2ed30b7f94346410149c626e44e7a
BLAKE2b-256 checksum
How to use checksums
c13675eed01d752823ac053702e5e8ded49d12631b1b269d21c14987fd26d978
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Linux/6.5.0-1021-azure

Release history Release notifications | RSS feed

This release

2.0.5 This release

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

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