anybadge

repository·master·Indexed 19 days ago

https://github.com/jongracecox/anybadge

A Python utility and library for generating customizable .svg badges. It features dynamic coloring based on configurable value thresholds, support for semantic versioning (SemVer), and a command-line interface for creating badges for CI/CD metrics like linting scores or test coverage.

Tokens
2.8K
Snippets
10
Records
15
Agent score
62%

What's inside anybadge

  1. Use emojis in badge labels and values

    master

    You can include emoji characters directly in the label or value strings.

    Important Considerations:

    • Rendering: The appearance of the emoji depends on the client rendering the SVG. Emojis are embedded as-is.
    • Layout: Emojis can have varying widths which may affect the badge layout. Use num_label_padding_chars or num_value_padding_chars to adjust spacing if necessary.
    • Compatibility: Some IDEs (like PyCharm) may not render emojis in their built-in SVG viewers.

    Python Example:

    badge = anybadge.Badge(label="Pipeline status", value="😄", num_value_padding_chars=1)
  2. How thresholds work in anybadge

    master

    Unlike many badge tools that require a static color, anybadge uses thresholds to select a color based on the badge value.

    When you provide thresholds (e.g., 2=red 4=orange), the utility selects the color associated with the highest threshold that the current value has not yet exceeded. This allows for dynamic coloring of badges (like linting scores or coverage percentages) based on predefined ranges.

  3. Use anybadge via Command Line

    master

    You can generate SVG badges from the terminal. By default, the SVG content is written to stdout, which can be redirected to a file. To write directly to a file, use the --file option.

    To use built-in thresholds, specify the template name instead of manual threshold/color pairs.

    Example: Using a template

    anybadge --value=2.22 --file=pylint.svg pylint

    Example: Manual thresholds

    anybadge -l pylint -v 2.22 -f pylint.svg 2=red 4=orange 8=yellow 10=green
  4. Use the anybadge CLI to generate SVG badges

    master

    The anybadge CLI utility generates .svg badge images with configurable colors based on thresholds.

    Output Behavior:

    • If you use the --file option, the .svg image is written to the specified file.
    • If you omit the --file option, the .svg content is written to stdout, allowing you to redirect it to a file (e.g., anybadge ... > badge.svg).

    Threshold Logic: Thresholds are passed as positional arguments in the format <value>=<color>. These are interpreted as: "Less than <value> = <color>". For example, 2=red 4=orange means values less than 2 are red, and values between 2 and 4 are orange.

    Built-in Styles: Instead of defining manual thresholds, you can pass a built-in style name as a positional argument (e.g., gitlab-scoped). This automatically applies predefined labels, suffixes, and threshold colors.

    # Example: Pylint badge with manual thresholds
    anybadge.py --value=2.22 --file=pylint.svg pylint 2=red 4=orange 8=yellow 10=green
    
    # Example: Coverage badge with suffix
    anybadge.py --label=coverage --value=65 --suffix='%%' --file=coverage.svg 50=red 60=orange 80=yellow 100=green
    
    # Example: CI Pipeline badge using string values
    anybadge.py --label=pipeline --value=passing --file=pipeline.svg passing=green failing=red
  5. Generate badges using the anybadge Python API

    master

    To generate badges programmatically, import anybadge and use the anybadge.Badge class.

    1. Define a thresholds dictionary where keys are numeric values and values are color strings.
    2. Instantiate anybadge.Badge(label, value, thresholds=thresholds).
    3. Call .write_badge(filename) to save the .svg file.
    import anybadge
    
    # Define thresholds: <2=red, <4=orange, <8=yellow, <10=green
    thresholds = {
        2: 'red',
        4: 'orange',
        6: 'yellow',
        10: 'green'
    }
    
    badge = anybadge.Badge('pylint', 2.22, thresholds=thresholds)
    badge.write_badge('pylint.svg')
  6. Configure colors in anybadge

    master

    Anybadge supports two types of colors for default_color, text_color, and thresholds:

    1. Named colors: Uses names from the Mozilla color keywords list (e.g., teal, aliceblue, crimson).
    2. Hex codes: Custom colors using hex representation (e.g., #008080).

    Python Usage Example:

    import anybadge
    
    # Using a named color
    badge = anybadge.Badge(label='custom color', value='teal', default_color='teal', num_padding_chars=1)
    
    # Using a hex color
    badge = anybadge.Badge(label='custom color', value='teal', default_color='#008080', num_padding_chars=1)
    import anybadge
    
    badge = anybadge.Badge(label='custom color', value='teal', default_color='teal', num_padding_chars=1)
    badge = anybadge.Badge(label='custom color', value='teal', default_color='#008080', num_padding_chars=1)
  7. Create badges using the anybadge Python API

    master

    The anybadge.Badge class is the primary interface for generating badges programmatically. You can customize the label, value, font, size, padding, and thresholds.

    Core Parameters:

    • label: The text displayed as the label.
    • value: The numeric or string value.
    • font_name: Font family string.
    • font_size: Integer font size.
    • num_padding_chars: Padding for the label.
    • num_value_padding_chars: Padding for the value.
    • thresholds: A dictionary mapping values/versions to colors.
    • default_color: The color used if no threshold is met.
    • template: A custom SVG template string using Jinja2-style placeholders (e.g., {{ label }}).

    Example:

    from anybadge import Badge
    
    badge = Badge(
        label='Status',
        value='100%',
        default_color='#4c1',
        text_color='#fff'
    )
    badge.write_badge('status.svg')
  8. Implement Semantic Versioning (SemVer) thresholds

    master

    Anybadge supports semantic versioning for value and threshold keys when semver=True is enabled. This allows color-coded badges based on version numbers.

    Thresholds define the upper bounds of a version range. If a value is less than the threshold, it falls into that color category.

    Python Example:

    from anybadge import Badge
    
    badge = Badge(
        label='Version',
        value='3.0.0',
        thresholds={'3.0.0': 'red', '3.2.0': 'orange', '999.0.0': 'green'},
        semver=True
    )

    In this example:

    • Value < 3.0.0 $\rightarrow$ red
    • Value < 3.2.0 $\rightarrow$ orange
    • Value < 999.0.0 $\rightarrow$ green

    Note: Always provide an extreme upper bound (e.g., 999.0.0) if you do not know the maximum possible version.

  9. Disable XML escaping for labels and values

    master

    Since badges are generated as SVG (XML) files, HTML characters in labels or values are escaped by default. To prevent this, use the following options:

    Python API:

    • escape_label=False
    • escape_value=False

    CLI:

    • --no-escape-label
    • --no-escape-value
  10. Generate badges using the anybadge CLI

    master

    You can generate .svg badges directly from the command line. You must specify a label (-l), a value (-v), and an output filename (-f). You can also provide thresholds to automatically determine the badge color based on the value.

    Thresholds are provided as pairs of <value>=color. Values can be integers or floats, and colors are specified as strings.

    anybadge -l pylint -v 2.22 -f pylint.svg 2=red 4=orange 8=yellow 10=green
  11. Configure the anybadge server via environment variables

    master

    The server respects several environment variables for configuration, which can be used instead of or in addition to CLI flags:

    Environment VariableDescription
    ANYBADGE_PORTSets the server port number (must be an integer).
    ANYBADGE_LISTEN_ADDRESSSets the server listen address.
    ANYBADGE_LOG_LEVELSets the logging level (e.g., INFO, DEBUG, WARNING).