Skip to main content

A different way to style python output

Project description

stylish-python

An intuitive way to style python terminal output

Import

First import the libaray with:

from stylish_py import *

Usage

All output must use sprint() (stylish print) as below:

sprint("This will be styled")

sprint() accepts both strings and integer values and will print whatever is passed into it onto the console. It will print styled text if styles are applied as intstructed below.

The style() method

For basic styling use the style() method. All styles must be declared in a dictionary where the key is the property and the value is the style you want to apply, sort of like CSS. Below is some basic usage of the style() method:

sprint("blue and bold").style({"color": "blue", "font": "italic"})
sprint("hex and rgb is accepted too!").style({"color": "#FFFFFF", "background": (0,0,0)})

At the moment the following styles can be applied:

  • "color": sets the foreground color
  • "background": sets the background color
  • "font": can change basic font properties such as weight and slant, dependent on terminal used
  • "style": only accepts "none" and is used only for nested styles explained later on

Both "color" and "background" accept rgb values (in 3 element tuple containing only integers), hex values (in a string beginning with "#") or one of the pre-assigned values explained at end of the README.

Init()

The init() method should be called at the start of the file. At the moment the init method accepts the following:

  • stylesheet: Can either accept a string that points to the path of the .ass file or to a variable containing a list of styles (stylesheets are explained below)
  • palette: Can either accept a string that points to the path of the .acp file or to a variable containing a dictionary of custom colors (palettes are explained below)
  • end: Can accept a boolean, either True or False. If set to False the style applied to the line above will continue on through all lines. Recommend to set to True

file.py

from stylish_py import *

sprint().init(
    end=True,
    stylesheet="./path/to/stylesheet.ass",
    palette="./path/to/palette.acp"
)

Stylesheets

stylish allows you to use stylesheets to easily apply styles to multiple elements through the use of the id() and stylesheet() methods. A stylesheet can either be imported from a .ass (awesome style sheet) file or defined inline as shown below. At the moment only one style sheet can be used at once. Below are two examples of stylesheet usage:

style.ass

{"id": "title", "color": (255, 255, 255), "font": "bold"}
{"id": "subtitle", "color": (200, 200, 200), "font": "italic"}

file.py

from stylish_py import *

sprint().init(
    stylesheet="./style.ass"
)

sprint("This is a white, bold title").id("title")
sprint("This is a grey, italic subtitle").id("subtitle")

As you can see in the above example a stylesheet was imported using the init() method. Each dictionary in a stylesheet should have an "id" tag, this tells stylish what styles should be used where. The id() method accepts a string which should match that of the "id" value in the dictionary containing the styles you want to apply.

Stylsheets can also be created within the .py that they are going to be used as below:

file.py

from stylish_py import *

stylesheet = [
    {"id": "title", "color": (255, 255, 255), "font": "bold"},
    {"id": "subtitle", "color": (200, 200, 200), "font": "italic"}
]

sprint().init(
    stylesheet=stylesheet
)

sprint("This is a white, bold title").id("title")
sprint("This is a grey, italic subtitle").id("subtitle")

This will produce the same output but instead of passing in a string to the init() method a list of dictionaries is instead used.

Palettes

stylish also allows you to create palettes of custom colors which you can use to set your "color" and "background". Palettes function in a similar way to stylesheets in that they can be defined either inline or in a .acp (awesome color palette file) as below:

palette.acp

{
    "turquoise": "#41EAD4",
    "sage": (186, 191, 149)
}

file.py

from stylish_py import *

sprint().init(
    palette="./palette.acp"
)

sprint("This is a custom color!").style({"color": "turquoise"})
sprint("So is this!").style({"color": "sage"})

The below will output the exact same as the above:

from stylish_py import *

palette = {
    "turquoise": "#41EAD4",
    "sage": (186, 191, 149)
}

sprint().init(
    palette=palette
)

sprint("This is a custom color!").style({"color": "turquoise"})
sprint("So is this!").style({"color": "sage"})

Default values

Colors

Color RGB
red (255, 0, 0)
green (0, 255, 0)
blue (0, 0, 255)
yellow (255, 255, 0)
aqua (0, 255, 255)
magenta (255, 0, 255)
pink (255, 105, 180)
chocolate (255, 105, 30)
white (255, 255, 255)
black (0, 0,0 )

FONTS

  • bold
  • dim
  • italic
  • underline
  • invisible

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stylish_py-0.0.2.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stylish_py-0.0.2-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file stylish_py-0.0.2.tar.gz.

File metadata

  • Download URL: stylish_py-0.0.2.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.5

File hashes

Hashes for stylish_py-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4cf236ff55bfec46780a7b161ded0420b51fe23f96b630b02b7db64ef4892e6e
MD5 a72307df63e9df506da19069cb911d00
BLAKE2b-256 dcee7af1ca22104bb7c8665a63c06f5166e5884c437ce5c41bf30422326295f0

See more details on using hashes here.

File details

Details for the file stylish_py-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: stylish_py-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.5

File hashes

Hashes for stylish_py-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0c8628af9ed61b9f4c4c4771a7f1a34fbc987d251a6f9e0143df2c376b124ff4
MD5 609c65149d0b7f172c9932749abc9bfd
BLAKE2b-256 220da1ae08be3c497d542c483c4999d77eececb666298722602975c4126a4620

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page