Anybadge

raw JSON →
1.16.0 verified Sat Apr 25 auth: no python

Anybadge is a Python library and command-line tool designed for simple and flexible generation of SVG badges for your projects. It allows users to define custom labels, values, and configurable thresholds to color badges dynamically. The library is actively maintained, with its current version being 1.16.0, and receives regular updates.

pip install anybadge
error ImportError: cannot import name 'get_template' from 'anybadge.templates'
cause This error occurs due to a known issue in certain versions of the anybadge package where the 'get_template' function is not properly accessible.
fix
Upgrade to the latest version of anybadge where this issue has been resolved.
error ModuleNotFoundError: No module named 'anybadge'
cause This error occurs when the anybadge package is not installed in the Python environment.
fix
Install anybadge using pip: 'pip install anybadge'.
error TypeError: __init__() got an unexpected keyword argument 'thresholds'
cause This error occurs when an incorrect keyword argument is passed to the Badge class constructor in anybadge.
fix
Ensure that the 'thresholds' argument is correctly defined as a dictionary mapping numeric thresholds to color names.
error ImportError: cannot import name 'Badge' from 'anybadge'
cause This error typically occurs when a local Python file or directory is named `anybadge.py` (or `anybadge`) and shadows the installed `anybadge` package, causing Python to import the local file instead of the library.
fix
Rename your local Python file or directory from anybadge.py (or anybadge) to something else to avoid name collision with the installed library.
error cannot import name 'get_template' from 'anybadge.templates'
cause This error usually arises from an incompatibility after an `anybadge` update, where internal module structures or function names (like `get_template`) might have changed, or due to a corrupted or mixed installation.
fix
Ensure you have the latest version of anybadge installed by running pip install --upgrade anybadge. If the issue persists, consider recreating your Python environment and reinstalling the package.
breaking Python 2.7 support was dropped in version 1.10.0. Projects using older Python versions must upgrade to Python 3.4 or higher.
fix Ensure your project runs on Python >= 3.4. Upgrade your Python environment if necessary.
gotcha Earlier versions (prior to v1.16.0) could mishandle malformed thresholds, leading to unexpected badge colors or errors.
fix Upgrade to anybadge v1.16.0 or newer. Ensure threshold definitions are correctly formatted (e.g., `{'value': 'color'}`).
gotcha Issues with escaping special characters in badge labels and values were present in versions prior to v1.16.0, potentially causing malformed SVG output.
fix Upgrade to anybadge v1.16.0 or newer to benefit from fixes for label and value escaping.
gotcha A package version bug was identified and fixed in v1.16.0, which might have affected how the library reported or used its own version information.
fix Upgrade to anybadge v1.16.0 or newer to resolve any internal package versioning inconsistencies.
runtime status import time mem disk
3.10-alpine 0.11s 2.1MB 18.8M
3.10-slim 0.13s 2.1MB 19M
3.11-alpine 0.25s 2.4MB 20.8M
3.11-slim 0.18s 2.4MB 21M
3.12-alpine 0.20s 1.9MB 12.6M
3.12-slim 0.16s 1.9MB 13M
3.13-alpine 0.19s 2.7MB 12.3M
3.13-slim 0.18s 2.5MB 13M
3.9-alpine 0.09s 2.2MB 18.3M
3.9-slim 0.09s 2.2MB 19M

This example demonstrates how to create a PyLint score badge using thresholds to determine its color and then save it as an SVG file. Values can be integers, floats, or strings, and custom colors (hex codes or named colors) are supported.

import anybadge

# Define thresholds for coloring the badge based on value
# <2=red, <4=orange, <6=yellow, <10=green
thresholds = {2: 'red', 4: 'orange', 6: 'yellow', 10: 'green'}

# Create a badge instance
badge = anybadge.Badge(
    label='pylint',
    value=2.22,
    thresholds=thresholds
)

# Write the badge to an SVG file
badge.write_badge('pylint.svg')

print('pylint.svg generated.')