简单、灵活的项目徽章生成器。

anybadge的Python项目详细描述


用于为项目生成徽章的python项目

pypi packagebuild statusdownloadsGitHub last commitGitHubGitHub stars

概述

anybadge可用于向python项目添加徽章生成, 还提供了一个命令行界面。

此实用程序可用于使用可配置的 根据徽章值为徽章上色的阈值。许多徽章 生成工具只提供指定徽章颜色的能力。 anybadge允许您指定标签、徽章值和颜色,但是 它还允许您指定一组阈值,这些阈值可用于 根据徽章值选择颜色。

anybadge可能对公司内部开发或任何时候都有用 呼叫外部徽章服务是不可能的,或者是不可取的。 在这种情况下,使用anybadge比运行自己的 内部徽章服务。

该包可以导入到您的python代码中,或者直接从 命令行。

演示

你可以找到一个repl.it demo here。 这将允许您查看包可以做什么,并使用它来测试输出。

基本用法

命令行

例如,如果要生成pylint徽章,可以运行anybadge 从这样的命令行:

anybadge -l pylint -v 2.22 -f pylint.svg 2=red 4=orange 8=yellow 10=green

这将产生这样的徽章:

pylint

在本例中,标签被设置为“pylint”,值为“2.22”,并且 名为“pylint.svg”的输出文件。阈值成对提供 值的<value>=color可以是整数,也可以是范围的浮点数,并且 也支持字符串值。

Python

下面是用python代码实现的相同示例:

importanybadge# Define thresholds: <2=red, <4=orange <8=yellow <10=greenthresholds={2:'red',4:'orange',6:'yellow',10:'green'}badge=anybadge.Badge('pylint',2.22,thresholds=thresholds)badge.write_badge('pylint.svg')

安装

anybadge在pypi中的https://pypi.python.org/pypi/anybadge

您可以使用pip

安装最新版本的anybadge
pip install anybadge

这将安装python包,并使anybadge可用 作为命令行实用程序。

获取帮助

要从命令行实用程序获得帮助,只需运行:

anybadge --help

命令行用法

输出

使用--file选项运行实用程序将导致.svg图像 写入文件。如果没有--file选项,.svg文件内容将是 写入stdout,因此可以重定向到文件。

阈值

为了节省时间,已经设置了一些阈值。使用这些阈值 可以简单地指定模板名称,而不是阈值/颜色对。

anybadge --value=<VALUE> --file=<FILE> <TEMPLATE-NAME>

例如:

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

示例

使用模板pylint
anybadge --value=2.22 --file=pylint.svg pylint
pylint

使用参数pylint
anybadge -l pylint -v 2.22 -f pylint.svg 2=red 4=orange 8=yellow 10=green
pylint

使用模板覆盖
anybadge --value=65 --file=coverage.svg coverage
pylint

管道,使用标记的颜色
anybadge --label=pipeline --value=passing --file=pipeline.svg passing=green failing=red
pylint

固定颜色的徽章
anybadge --label=awesomeness --value="110%" --file=awesomeness.svg --color=#97CA00
pylint

选项

以下是命令行选项:

 positional arguments:
   args                  Pairs of <upper>=<color>. For example 2=red 4=orange
                         6=yellow 8=good. Read this as "Less than 2 = red, less
                         than 4 = orange...".

 optional arguments:
   -h, --help            show this help message and exit
   -l LABEL, --label LABEL
                         The badge label.
   -v VALUE, --value VALUE
                         The badge value.
   -m VALUE_FORMAT, --value-format VALUE_FORMAT
                         Formatting string for value (e.g. "%.2f" for 2dp
                         floats)
   -c COLOR, --color COLOR
                         For fixed color badges use --colorto specify the badge
                         color.
   -p PREFIX, --prefix PREFIX
                         Optional prefix for value.
   -s SUFFIX, --suffix SUFFIX
                         Optional suffix for value.
   -d PADDING, --padding PADDING
                         Number of characters to pad on either side of the
                         badge text.
   -n FONT, --font FONT  "DejaVu Sans,Verdana,Geneva,sans-serif"
   -z FONT_SIZE, --font-size FONT_SIZE
                         Font size.
   -t TEMPLATE, --template TEMPLATE
                         Location of alternative template .svg file.
   -u, --use-max         Use the maximum threshold color when the value exceeds
                         the maximum threshold.
   -f FILE, --file FILE  Output file location.
   -o, --overwrite       Overwrite output file if it already exists.
   -r TEXT_COLOR, --text-color TEXT_COLOR
                         Text color. Single value affects both labeland value
                         colors. A comma separated pair affects label and value
                                 text respectively.

Examples
--------

Here are some usage specific command line examples that may save time on defining
thresholds.

Pylint::

anybadge.py --value=2.22 --file=pylint.svg pylint
anybadge.py --label=pylint --value=2.22 --file=pylint.svg 2=red 4=orange 8=yellow 10=green

Coverage::

anybadge.py --value=65 --file=coverage.svg coverage
anybadge.py --label=coverage --value=65 --suffix='%%' --file=coverage.svg 50=red 60=orange 80=yellow 100=green

CI Pipeline::

anybadge.py --label=pipeline --value=passing --file=pipeline.svg passing=green failing=red

Python usage
============
Here is the output of ``help(anybadge)``::

Help on module anybadge:

NAME
    anybadge - anybadge

DESCRIPTION
    A Python module for generating badges for your projects, with a focus on
    simplicity and flexibility.

CLASSES
    builtins.object
        Badge

    class Badge(builtins.object)
     |  Badge(label, value, font_name='DejaVu Sans,Verdana,Geneva,sans-serif', font_size=11, num_padding_chars=0.5, template='<?xml version="1.0" encoding="UTF-8"?>\n<svg xmlns="http://www.w3.org/2000/svg" width="{{ badge width }}" height="20">\n    <linearGradient id="b" x2="0" y2="100%">\n        <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>\n        <stop offset="1" stop-opacity=".1"/>\n    </linearGradient>\n    <mask id="a">\n        <rect width="{{ badge width }}" height="20" rx="3" fill="#fff"/>\n    </mask>\n    <g mask="url(#a)">\n        <path fill="#555" d="M0 0h{{ color split x }}v20H0z"/>\n        <path fill="{{ color }}" d="M{{ color split x }} 0h{{ value width }}v20H{{ color split x }}z"/>\n        <path fill="url(#b)" d="M0 0h{{ badge width }}v20H0z"/>\n    </g>\n    <g fill="{{ label text color }}" text-anchor="middle" font-family="{{ font name }}" font-size="{{ font size }}">\n        <text x="{{ label anchor shadow }}" y="15" fill="#010101" fill-opacity=".3">{{ label }}</text>\n        <text x="{{ label anchor }}" y="14">{{ label }}</text>\n    </g>\n    <g fill="{{ value text color }}" text-anchor="middle" font-family="{{ font name }}" font-size="{{ font size }}">\n        <text x="{{ value anchor shadow }}" y="15" fill="#010101" fill-opacity=".3">{{ value }}</text>\n        <text x="{{ value anchor }}" y="14">{{ value }}</text>\n    </g>\n</svg>', value_prefix='', value_suffix='', thresholds=None, default_color='#4c1', use_max_when_value_exceeds=True, value_format=None, text_color='#fff')
     |
     |  Badge class used to generate badges.
     |
     |  Examples:
     |
     |      Create a simple green badge:
     |
     |      >>> badge = Badge('label', 123, default_color='green')
     |
     |      Write a badge to file, overwriting any existing file:
     |
     |      >>> badge = Badge('label', 123, default_color='green')
     |      >>> badge.write_badge('demo.svg', overwrite=True)
     |
     |      Here are a number of examples showing thresholds, since there
     |      are certain situations that may not be obvious:
     |
     |      >>> badge = Badge('pipeline', 'passing', thresholds={'passing': 'green', 'failing': 'red'})
     |      >>> badge.badge_color
     |      'green'
     |
     |      2.32 is not <2
     |      2.32 is < 4, so 2.32 yields orange
     |      >>> badge = Badge('pylint', 2.32, thresholds={2: 'red',
     |      ...                                           4: 'orange',
     |      ...                                           8: 'yellow',
     |      ...                                           10: 'green'})
     |      >>> badge.badge_color
     |      'orange'
     |
     |      8 is not <8
     |      8 is <4, so 8 yields orange
     |      >>> badge = Badge('pylint', 8, thresholds={2: 'red',
     |      ...                                        4: 'orange',
     |      ...                                        8: 'yellow',
     |      ...                                        10: 'green'})
     |      >>> badge.badge_color
     |      'green'
     |
     |      10 is not <8, but use_max_when_value_exceeds defaults to
     |      True, so 10 yields green
     |      >>> badge = Badge('pylint', 11, thresholds={2: 'red',
     |      ...                                         4: 'orange',
     |      ...                                         8: 'yellow',
     |      ...                                         10: 'green'})
     |      >>> badge.badge_color
     |      'green'
     |
     |      11 is not <10, and use_max_when_value_exceeds is set to
     |      False, so 11 yields the default color '#4c1'
     |      >>> badge = Badge('pylint', 11, use_max_when_value_exceeds=False,
     |      ...               thresholds={2: 'red', 4: 'orange', 8: 'yellow',
     |      ...                           10: 'green'})
     |      >>> badge.badge_color
     |      '#4c1'
     |
     |  Methods defined here:
     |
     |  __init__(self, label, value, font_name='DejaVu Sans,Verdana,Geneva,sans-serif', font_size=11, num_padding_chars=0.5, template='<?xml version="1.0" encoding="UTF-8"?>\n<svg xmlns="http://www.w3.org/2000/svg" width="{{ badge width }}" height="20">\n    <linearGradient id="b" x2="0" y2="100%">\n        <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>\n        <stop offset="1" stop-opacity=".1"/>\n    </linearGradient>\n    <mask id="a">\n        <rect width="{{ badge width }}" height="20" rx="3" fill="#fff"/>\n    </mask>\n    <g mask="url(#a)">\n        <path fill="#555" d="M0 0h{{ color split x }}v20H0z"/>\n        <path fill="{{ color }}" d="M{{ color split x }} 0h{{ value width }}v20H{{ color split x }}z"/>\n        <path fill="url(#b)" d="M0 0h{{ badge width }}v20H0z"/>\n    </g>\n    <g fill="{{ label text color }}" text-anchor="middle" font-family="{{ font name }}" font-size="{{ font size }}">\n        <text x="{{ label anchor shadow }}" y="15" fill="#010101" fill-opacity=".3">{{ label }}</text>\n        <text x="{{ label anchor }}" y="14">{{ label }}</text>\n    </g>\n    <g fill="{{ value text color }}" text-anchor="middle" font-family="{{ font name }}" font-size="{{ font size }}">\n        <text x="{{ value anchor shadow }}" y="15" fill="#010101" fill-opacity=".3">{{ value }}</text>\n        <text x="{{ value anchor }}" y="14">{{ value }}</text>\n    </g>\n</svg>', value_prefix='', value_suffix='', thresholds=None, default_color='#4c1', use_max_when_value_exceeds=True, value_format=None, text_color='#fff')
     |      Constructor for Badge class.
     |
     |  get_text_width(self, text)
     |      Return the width of text.
     |
     |      Args:
     |          text(str): Text to get the pixel width of.
     |
     |      Returns:
     |          int: Pixel width of the given text based on the badges selected font.
     |
     |      This implementation assumes a fixed font of:
     |
     |      font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"
     |      >>> badge = Badge('x', 1, font_name='DejaVu Sans,Verdana,Geneva,sans-serif', font_size=11)
     |      >>> badge.get_text_width('pylint')
     |      34
     |
     |  write_badge(self, file_path, overwrite=False)
     |      Write badge to file.
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  __dict__
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__
     |      list of weak references to the object (if defined)
     |
     |  badge_color
     |      Badge color based on the configured thresholds.
     |
     |      Returns: str
     |
     |  badge_color_code
     |      Return the color code for the badge.
     |
     |      Returns: str
     |
     |  badge_svg_text
     |      The badge SVG text.
     |
     |      Returns: str
     |
     |  badge_width
     |      The total width of badge.
     |
     |      Returns: int
     |
     |      Examples:
     |
     |          >>> badge = Badge('pylint', '5')
     |          >>> badge.badge_width
     |          103
     |
     |  color_split_position
     |      The SVG x position where the color split should occur.
     |
     |      Returns: int
     |
     |  font_width
     |      Return the width multiplier for a font.
     |
     |      Returns:
     |          int: Maximum pixel width of badges selected font.
     |
     |      Example:
     |
     |          >>> Badge(label='x', value='1').font_width
     |          10
     |
     |  label_anchor
     |      The SVG x position of the middle anchor for the label text.
     |
     |      Returns: float
     |
     |  label_anchor_shadow
     |      The SVG x position of the label shadow anchor.
     |
     |      Returns: float
     |
     |  label_width
     |      The SVG width of the label text.
     |
     |      Returns: int
     |
     |  value_anchor
     |      The SVG x position of the middle anchor for the value text.
     |
     |      Returns: float
     |
     |  value_anchor_shadow
     |      The SVG x position of the value shadow anchor.
     |
     |      Returns: float
     |
     |  value_is_float
     |      Identify whether the value text is a float.
     |
     |      Returns: bool
     |
     |  value_is_int
     |      Identify whether the value text is an int.
     |
     |      Returns: bool
     |
     |  value_type
     |      The Python type associated with the value.
     |
     |      Returns: type
     |
     |  value_width
     |      The SVG width of the value text.
     |
     |      Returns: int

FUNCTIONS
    main()
        Generate a badge based on command line arguments.

    parse_args()
        Parse the command line arguments.

DATA
    BADGE_TEMPLATES = {'coverage': {'label': 'coverage', 'suffix': '%', 't...
    COLORS = {'green': '#4c1', 'lightgrey': '#9f9f9f', 'orange': '#fe7d37'...
    DEFAULT_COLOR = '#4c1'
    DEFAULT_FONT = 'DejaVu Sans,Verdana,Geneva,sans-serif'
    DEFAULT_FONT_SIZE = 11
    DEFAULT_TEXT_COLOR = '#fff'
    FONT_WIDTHS = {'DejaVu Sans,Verdana,Geneva,sans-serif': {11: 10}}
    NUM_PADDING_CHARS = 0.5
    TEMPLATE_SVG = '<?xml version="1.0" encoding="UTF-8"?>\n<svg xmln...ho...
    __summary__ = 'A simple, flexible badge generator.'
    __title__ = 'anybadge'
    __uri__ = 'https://github.com/jongracecox/anybadge'
    __version_info__ = ('0', '0', '0')
    digits = '0123456789'
    version = '0.0.0'

VERSION
    0.0.0

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java将信息存储到另一个类的数组中   java将视频帧添加到JTabbed窗格   java Wicket下拉选择只读   java/Servlets/ISO88591代替UTF8   Java AWS ElastiCache Redis连接超时   升级到Tomcat 7.0.54后java面临404错误   java Hashmap、wicket和计时器?   rust如何使用Java GraalVM在Java代码和嵌入式WASM代码之间传递字符串作为参数和返回值   在调整大小后,如何将java中图像的DPI从96更改为300?   远程文件的java下载统计信息   javascript如何在服务器上复制由jsPDF在web客户端生成的pdf?   java在没有读取权限的情况下从可扩展客户端读取数据   java如何从设备id生成IMIE   java为什么布局参数在Android编程中起作用?   java如何向服务器发送json   java中的数组和对象   java JSON未确定字符串错误