sphinx-autodoc-badges¶
Shared badge node, HTML visitors, and CSS infrastructure for Sphinx autodoc
extensions. Provides a single BadgeNode and builder API that
sphinx-autodoc-api-style, sphinx-autodoc-pytest-fixtures, and
sphinx-autodoc-fastmcp share instead of reimplementing badges
independently.
$ pip install sphinx-autodoc-badges
How it works¶
setup() registers the extension with Sphinx:
add_node()registersBadgeNodewith HTML visitors (visit_badge_html/depart_badge_html).add_css_file()injects the sharedsphinx_autodoc_badges.cssstylesheet.Downstream extensions call
setup_extension()to load the badge layer:
def setup(app: Sphinx) -> dict[str, Any]:
app.setup_extension("sphinx_autodoc_badges")
BadgeNode subclasses docutils.nodes.inline, so unregistered
builders (text, LaTeX, man) fall back to visit_inline via Sphinx’s
MRO-based dispatch — no special handling needed.
Live badge demos¶
Every variant rendered by the real build_badge / build_badge_group /
build_toolbar API:
Size variants (xs / sm / default / lg / xl)
xs sm md lg xl build_badge("lg", size="lg")
Filled (default)
label build_badge("label")
with icon build_badge("with icon", icon="\U0001f50d")
Outline
outline build_badge("outline", fill="outline")
Icon-only
build_badge("", style="icon-only", icon="\U0001f50d")
Inline-icon (inside code chips)
some_function() build_badge("", style="inline-icon", icon="\u270f\ufe0f")
Badge group
alpha beta gamma build_badge_group([...badges...])
Toolbar (push-right in flex heading)
build_toolbar(build_badge_group([...]))
API reference¶
- sphinx_autodoc_badges.build_badge(text, *, tooltip='', icon='', classes=(), style='full', fill='filled', size='', tabindex='0')¶
Build a single badge node.
- Return type:
- Parameters:
text (str) – Visible label. Empty string for icon-only badges.
tooltip (str) – Hover text and
aria-label.icon (str) – Emoji character for CSS
::before.classes (Sequence[str]) – Additional CSS classes (plugin prefix + color class).
style (str) – Structural variant:
"full","icon-only","inline-icon".fill (str) – Visual fill:
"filled"(default) or"outline".size (str) – Optional size tier:
"xs","sm","lg", or"xl". Empty string uses the default (no extra class).tabindex (str) –
"0"for focusable,""to skip.
- Return type:
Examples
>>> b = build_badge("async", tooltip="Asynchronous", classes=["gas-mod-async"]) >>> b.astext() 'async'
>>> b = build_badge("", style="icon-only", classes=["smf-safety-readonly"]) >>> "sab-icon-only" in b["classes"] True
>>> b = build_badge("big", size="lg") >>> "sab-lg" in b["classes"] True
- sphinx_autodoc_badges.build_badge_group(badges, *, classes=())¶
Wrap badges in a group container with inter-badge spacing.
- Return type:
inline- Parameters:
- Return type:
nodes.inline
Examples
>>> from sphinx_autodoc_badges._nodes import BadgeNode >>> g = build_badge_group([BadgeNode("a"), BadgeNode("b")]) >>> "sab-badge-group" in g["classes"] True
- sphinx_autodoc_badges.build_toolbar(badge_group, *, classes=())¶
Wrap a badge group in a toolbar (
margin-left: autofor flex titles).- Return type:
inline- Parameters:
badge_group (nodes.inline) – Badge group from
build_badge_group().classes (Sequence[str]) – Additional CSS classes on the toolbar.
- Return type:
nodes.inline
Examples
>>> from sphinx_autodoc_badges._nodes import BadgeNode >>> g = build_badge_group([BadgeNode("x")]) >>> t = build_toolbar(g, classes=["smf-toolbar"]) >>> "sab-toolbar" in t["classes"] True
- class sphinx_autodoc_badges.BadgeNode¶
Bases:
inlineInline badge rendered as
<span>with ARIA and icon support.Subclasses
nodes.inlineso unregistered builders (text, LaTeX, man) fall back tovisit_inlinevia MRO dispatch inSphinxTranslator.dispatch_visit.Examples
>>> b = BadgeNode("hello", badge_tooltip="greeting") >>> b["badge_tooltip"] 'greeting'
>>> b.astext() 'hello'
Constructor parameters
Parameter
Default
Description
text""Visible label. Empty string for icon-only badges.
badge_tooltip""Hover text and
aria-label.badge_icon""Emoji character rendered via CSS
::before.badge_style"full"Structural variant:
"full","icon-only","inline-icon".badge_size""Optional size:
"xs","sm","lg", or"xl". Empty means default.tabindex"0""0"for keyboard-focusable,""to skip.classesNoneAdditional CSS classes (plugin prefix + color class).
- __init__(text='', *, badge_tooltip='', badge_icon='', badge_style='full', badge_size='', tabindex='0', classes=None, **attributes)¶
- classmethod __new__(*args, **kwargs)¶
- class sphinx_autodoc_badges._css.SAB¶
Bases:
objectCSS class constants (
sab-= sphinx autodoc badges).Examples
>>> SAB.PREFIX 'sab'
>>> SAB.OUTLINE 'sab-outline'
- PREFIX = 'sab'¶
- BADGE = 'sab-badge'¶
- BADGE_GROUP = 'sab-badge-group'¶
- TOOLBAR = 'sab-toolbar'¶
- ICON_ONLY = 'sab-icon-only'¶
- INLINE_ICON = 'sab-inline-icon'¶
- OUTLINE = 'sab-outline'¶
- FILLED = 'sab-filled'¶
- XS = 'sab-xs'¶
- SM = 'sab-sm'¶
- LG = 'sab-lg'¶
- XL = 'sab-xl'¶
CSS custom properties¶
All colors and metrics are exposed as CSS custom properties on :root.
Override them in your project’s custom.css or via
add_css_file().
Defaults¶
:root {
/* ── Color hooks (set by downstream extensions) ────── */
--sab-bg: transparent; /* badge background */
--sab-fg: inherit; /* badge text color */
--sab-border: none; /* badge border shorthand */
/* ── Metrics ───────────────────────────────────────── */
--sab-font-size: 0.75em;
--sab-font-weight: 700;
--sab-padding-v: 0.35em; /* vertical padding */
--sab-padding-h: 0.65em; /* horizontal padding */
--sab-radius: 0.25rem; /* border-radius */
--sab-icon-gap: 0.28rem; /* gap between icon and label */
/* ── Depth (inset shadow on solid badges) ──────────── */
--sab-buff-shadow:
inset 0 1px 0 rgba(255, 255, 255, 0.2),
inset 0 -1px 2px rgba(0, 0, 0, 0.12);
--sab-buff-shadow-dark-ui:
inset 0 1px 0 rgba(255, 255, 255, 0.1),
inset 0 -1px 2px rgba(0, 0, 0, 0.28);
}
Property reference¶
Property |
Purpose |
|---|---|
|
Badge background color. Extensions set this per badge class (e.g. green for “readonly”). |
|
Badge text color. Falls back to |
|
Border shorthand ( |
|
Font size. Context-aware sizing (headings, body, TOC) overrides this. |
|
Font weight. Default |
|
Vertical and horizontal padding. |
|
Border radius for pill shape. |
|
Gap between the |
|
Subtle inset highlight + shadow for depth on light backgrounds. |
|
Stronger inset shadow variant for dark theme / |
CSS class reference¶
All classes use the sab- prefix (sphinx autodoc badges).
Class |
Applied by |
Description |
|---|---|---|
|
|
Base class. Always present on every badge. |
|
|
Transparent background, inherits text color. |
|
|
16 × 16 colored box with emoji |
|
|
Bare emoji inside a code chip, no background. |
|
|
Flex container with |
|
|
Flex push-right ( |
|
|
Extra small (dense tables, tight UI). |
|
|
Small inline badges. |
|
|
Large (section titles, callouts). |
|
|
Extra large (hero / landing emphasis). |
Context-aware sizing¶
Badge size adapts automatically based on where it appears in the document.
CSS selectors handle it. Explicit size classes (sab-xs … sab-xl) override
contextual sizing when present (higher specificity than context rules).
Context |
Font size |
Selectors |
|---|---|---|
Heading ( |
|
|
Body ( |
|
|
TOC sidebar |
|
|
Downstream extensions¶
Each extension adds its own CSS color layer on top of the shared base:
Extension |
Prefix |
Badge types |
|---|---|---|
|
Safety tiers (readonly / mutating / destructive), MCP tool type |
|
|
Python object types (function, class, method, …), modifiers (async, deprecated, …) |
|
|
Fixture scopes (session, module, function), kind badges (autouse, yield) |
Package reference¶
Copyable config snippet
extensions = [
"sphinx_autodoc_badges",
]
Package metadata
Source on GitHub: sphinx-autodoc-badges
PyPI: sphinx-autodoc-badges
Maturity:
Alpha
Registered Surface
sphinx_autodoc_badges