Skip to main content

Generate badges/shields with pure HTML/CSS.

Project description

Generate status badges/shields of pure HTML+CSS.

abadge-discovered.png

Overview

The Badge class in the module is used to generate status badges. It supports various configuration options like font, background etc., and also includes threshold support, which is useful for presenting job status, for example.

Usage

Badge can be instantiated to generate many badges with the same format:

from abadge import Badge

success_badge = Badge(value_text_color='#11a')
print(success_badge.to_html('build', 'passed'))
print(success_badge.to_html('tests', 'ok'))

or for one-shot generation:

print(Badge(label='tests', value='4/8').to_html())
print(Badge().to_html(label='tests', value='4/8'))  # Same thing
print(Badge.make_badge(tests, '4/8'))               # This too

The arguments to all of the methods are identical. The arguments to the constructor will be stored in the instance as default values which can then be overridden by the arguments to the to_html method. make_badge always use the class default configuration (it is a class method).

Arguments

All three methods support the following arguments:

Optional arguments

label:

text for the label (left) part. Can also be given as keyword argument label=<text>

value:

text for the value (right) part. Can also be given as keyword argument value=<text>

Keyword arguments

border_radius:

how rounded the corners of the badge should be (CSS “padding”)

font_family:

font to use in the badge (CSS “font-family”)

font_size:

font size to use in the badge (CSS “font-size”)

label:

the text in label part of the badge

label_background:

background color for the label (left) part (CSS “background”)

label_text_color:

text color for the label (left) part (CSS “color”)

label_text_shadow:

configuration for the text shadow (CSS “text-shadow”)

link_decoration:

decoration for the link (CSS “text-decoration”)

padding:

amount of space between the border and the text (CSS “padding”)

thresholds:

dict with label-specific configuration options, so that multiple labels can be handled by the same class instance. See Thresholds below

url:

makes the badge link to the given URL

value:

the text in the value part of the badge

value_background:

background color for the value part (CSS “background”). This is also the final fallback if the value is neither found in thresholds nor in value_backgrounds

value_backgrounds:

dict with value to value_background mappings. See Thresholds below

value_text_color:

text color for the value part (CSS “color”)

value_text_shadow:

configuration for the text shadow (CSS “text-shadow”)

Thresholds

The thresholds argument is a dict with label as key and a configuration dict as value. The dict supports the following keys:

order:

May be: auto, float, int, str, or strict, with auto being the default if order does not exist. float, int and str forces level of that type (see below). auto uses ordering of type float or int if all values in colors are numbers type, with float taking precedence. If auto is set and at least one value is a string, or if strict is set, then an exact match is used for determining color, ie. no ordering

colors:

dict with value to color mapping

above:

Value is a color. if an ordering is requested, and the given value is above the highest value (key) in colors, then this color is used

Levels are handled by sorting the keys in the colors dict and comparing the incoming value to each of the keys, starting with the key with the lowest value, until the value is lower than or equal to the key:

for k in sorted(thresholds['colors'].keys, key=<sort by type>):
    if value <= k:
        return thresholds['colors'][k]
return thresholds['above']

Examples:

build_badge = Badge(thresholds={'build': {
                                    'colors': {'SUCCESS': '#0f0',
                                               'FAILURE': '#f00',
                                               'UNSTABLE': '#ff0',
                                               'ABORTED': '#f80',}},
                                'KPI': {
                                    'order': 'str',
                                    'colors': {'A': '#0f4',
                                               'B': '#f04',
                                               'C': '#f84',
                                               'D': '#ff4',}},
                                'passrate': {
                                    'colors': {0.3: '#f00',
                                               0.6: '#c40',
                                               0.8: '#4c0',},
                                    'above': '#0f0',}})

print(build_badge('build', job.get_status()).to_html())
# Using a non-existing value will use the value_background color
print(build_badge('build', 'SKIP').to_html())
print(build_badge('build', 'HOP', value_background='#888').to_html())
print(build_badge('passrate', test_passrate).to_html())

If the color is not found in thresholds then the value will be looked up in the value_backgrounds dict as a fallback:

build_badge = Badge(thresholds={'build': {
                                    'colors': {'SUCCESS': '#0f0',
                                               'FAILURE': '#f00',
                                               'UNSTABLE': '#ff0',
                                               'ABORTED': '#f80',}}},
                    value_backgrounds: {'SUCCESS': '#0f4',
                                        'FAILURE': '#f04',
                                        'UNSTABLE': '#f84',
                                        'ABORTED': '#ff4',},)
print(build_badge('test', job.get_status()))

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

abadge-2.0.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

abadge-2.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file abadge-2.0.0.tar.gz.

File metadata

  • Download URL: abadge-2.0.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.4

File hashes

Hashes for abadge-2.0.0.tar.gz
Algorithm Hash digest
SHA256 5172d58c3b641e66dc14c728b7830102822e3ee687b2ed121ea7a4adeff9d30c
MD5 fa8c14c668dcd299ea4a975031d9c0c1
BLAKE2b-256 ff92d7d9ae33f5587171c0b59b868dda93fa3ad052d5dedd1eaabfa4c42f6412

See more details on using hashes here.

File details

Details for the file abadge-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: abadge-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.4

File hashes

Hashes for abadge-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c237d502a3d5e5e42b99970e206e4a439e18a8076f61060e964ec344ad190c3b
MD5 2fa9fc142f5982608aa8f97e5404bad2
BLAKE2b-256 5454b81c71405b02084328fc0da3c046d095c4c76b4b8d12be280dcc831629f0

See more details on using hashes here.

Supported by

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