使用纯html/css生成徽章/盾牌。
abadge的Python项目详细描述
生成纯html+css的状态徽章/屏蔽。
概述
模块中的Badge类用于生成状态徽章。它 支持多种配置选项,如字体、背景等 包括阈值支持,用于显示作业状态 例子。
用法
Badge可以实例化以生成许多相同格式的徽章:
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'))
或一次生成:
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
所有方法的参数都相同。关于 构造函数将作为默认值存储在实例中,然后 被to_html方法的参数重写。make_badge总是 使用类默认配置(它是一个类方法)。
参数
这三种方法都支持以下参数:
可选参数
label: | text for the label (left) part. Can also be given as keyword argument ^{tt5}$ |
---|---|
value: | text for the value (right) part. Can also be given as keyword argument ^{tt6}$ |
关键字参数
^{tt7}$: | how rounded the corners of the badge should be (CSS “^{tt8}$”) |
---|---|
^{tt9}$: | font to use in the badge (CSS “^{tt10}$”) |
^{tt11}$: | font size to use in the badge (CSS “^{tt12}$”) |
^{tt13}$: | the text in label part of the badge |
^{tt14}$: | |
background color for the label (left) part (CSS “^{tt15}$”) | |
^{tt16}$: | |
text color for the label (left) part (CSS “^{tt17}$”) | |
^{tt18}$: | |
configuration for the text shadow (CSS “^{tt19}$”) | |
^{tt20}$: | |
decoration for the link (CSS “^{tt21}$”) | |
^{tt8}$: | amount of space between the border and the text (CSS “^{tt8}$”) |
^{tt24}$: | dict with label-specific configuration options, so that multiple labels can be handled by the same class instance. See Thresholds below |
^{tt25}$: | makes the badge link to the given URL |
^{tt26}$: | the text in the value part of the badge |
^{tt27}$: | |
background color for the value part (CSS “^{tt15}$”). This is also the final fallback if the value is neither found in ^{tt24}$ nor in ^{tt30}$ | |
^{tt30}$: | |
dict with value to ^{tt27}$ mappings. See Thresholds below | |
^{tt33}$: | |
text color for the value part (CSS “^{tt17}$”) | |
^{tt35}$: | |
configuration for the text shadow (CSS “^{tt19}$”) |
阈值
参数thresholds是一个dict,label作为键和配置 dict作为值。dict支持以下键:
^{tt38}$: | May be: ^{tt39}$, ^{tt40}$, ^{tt41}$, ^{tt42}$, or ^{tt43}$, with ^{tt39}$ being the default if ^{tt38}$ does not exist. ^{tt40}$, ^{tt41}$ and ^{tt42}$ forces level of that type (see below). ^{tt39}$ uses ordering of type float or int if all values in ^{tt50}$ are numbers type, with ^{tt40}$ taking precedence. If ^{tt39}$ is set and at least one value is a string, or if ^{tt43}$ is set, then an exact match is used for determining color, ie. no ordering |
---|---|
^{tt50}$: | dict with value to color mapping |
^{tt55}$: | Value is a color. if an ordering is requested, and the given value is above the highest value (key) in ^{tt50}$, then this color is used |
^{tt57}$: | Whether to shade the color depending on distance between the thresholds. Each R, G, and B color is calculated based on the fraction of the distance of the value between the thresholds |
通过对colorsdict中的键进行排序并比较 每个键的传入值,从具有最低值的键开始 值,直到该值小于或等于键:
for k in sorted(thresholds['colors'].keys, key=<sort by type>): if value <= k: return thresholds['colors'][k] return thresholds['above']
示例
一个实例可以配置为生产不同的标签类型:
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.to_html('build', 'UNSTABLE')) # Using a non-existing value will use the value_background color print(build_badge.to_html('build', 'SKIP')) print(build_badge.to_html('build', 'HOP', value_background='#ccc')) print(build_badge.to_html('passrate', 0.5))
如果在thresholds中找不到该颜色,则将查找该值 在value_backgroundsdict中作为回退:
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.to_html('test', 'ABORTED'))
着色不产生颜色步骤,而是在 门槛。着色仅适用于“float”和“int”类型:
build_badge = Badge(thresholds={ 'speed': { 'shade': True, 'colors': {0: '#0f0', 120: '#f00'}, # speed limit 'above': '#f08'}} # too fast! ) print(build_badge.to_html('speed', 97)) # Here is the rainbow build_badge = Badge(thresholds={ 'rainbow': { 'shade': True, 'colors': {0.0: '#ff0000', 1.0: '#ffff00', 2.0: '#00ff00', 3.0: '#00ffff', 4.0: '#0000ff', 5.0: '#8000ff'}}}) for c in range(0, 11): print(build_badge.to_html('rainbow', c / 2.0))