sphinx-ux-badgesΒΆ

Alpha GitHub PyPI

Alpha

Rendered output is stable. The Python API, CSS class names, and Sphinx config value names may change without a major version bump. Pin your dependency to a specific version range in production.

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-ux-badges

Live demosΒΆ

Every variant rendered by the real build_badge / build_badge_group / build_toolbar API:

No icon

label build_badge("label")

With icon β€” left (default, gp-sphinx-badge[data-icon])

readonly mutating build_badge("readonly", icon="πŸ”")

With icon β€” right (gp-sphinx-badge–icon-right)

readonly mutating build_badge("readonly", icon="πŸ”", classes=[SAB.ICON_RIGHT])

Icon-only β€” 16x16 coloured box (gp-sphinx-badge–icon-only)

build_badge("", style="icon-only", icon="πŸ”")

Inline-icon β€” bare emoji inside code chip (gp-sphinx-badge–inline-icon)

some_function() other_func() build_badge("", style="inline-icon", icon="✏️")

Outline (gp-sphinx-badge–outline)

outline outline + icon outline + icon right build_badge("outline", fill="outline")

Dense (gp-sphinx-badge–dense) β€” compact bordered, dotted underline

dense dense + icon left dense + icon right SAB.DENSE + icon variants

Dense + outline

dense outline dense outline + icon left dense outline + icon right SAB.DENSE + outline

Underline control modifiers

dense default dense icon right no underline solid dotted (opt-in) solid (opt-in) SAB.NO_UNDERLINE / SAB.UNDERLINE_SOLID / SAB.UNDERLINE_DOTTED

All sizes β€” standard pill

xxs xxs+icon xs xs+icon sm sm+icon md md+icon default default+icon lg lg+icon xl xl+icon xxs / xs / sm / md / default / lg / xl

All sizes β€” dense

xxs xxs+icon xs xs+icon sm sm+icon md md+icon default default+icon lg lg+icon xl xl+icon xxs / xs / sm / md / default / lg / xl (gp-sphinx-badge--dense)

Icon positions β€” standard (no icon / left / right / icon-only)

function function function function: none / left / right / icon-only

fixture fixture fixture fixture: none / left / right / icon-only

config config config config: none / left / right / icon-only

directive directive directive directive: none / left / right / icon-only

Icon positions β€” dense

function function function function (dense): none / left / right

fixture fixture fixture fixture (dense): none / left / right

config config config config (dense): none / left / right

directive directive directive directive (dense): none / left / right

Badge group

alpha beta gamma build_badge_group([...badges...])

Toolbar (push-right in flex heading)

Example heading readonly tool

build_toolbar(build_badge_group([...]))

Python API types (gp-sphinx-badge–type-*)

function class method property attribute data exception type alias module

Python API types β€” dense variant (gp-sphinx-badge–dense)

function class method property attribute data exception type alias module

Python API modifiers (gp-sphinx-badge–mod-*, outlined)

async classmethod staticmethod abstract final

Python API modifiers β€” dense variant

async classmethod staticmethod abstract final

pytest fixture types (gp-sphinx-badge–type-fixture)

fixture SAB.TYPE_FIXTURE β€” standard

fixture SAB.TYPE_FIXTURE β€” dense

pytest fixture scopes (gp-sphinx-badge–scope-*)

session module class

session module class

pytest fixture kinds / states (outlined)

factory override auto deprecated

factory override auto deprecated

Sphinx config (gp-sphinx-badge–type-config / gp-sphinx-badge–mod-rebuild)

config env html standard

config env html dense

docutils (gp-sphinx-badge–type-directive / role / option)

directive role option standard

directive role option dense

Package metadata β€” maturity

Alpha Beta gp-sphinx-badge--meta-alpha / gp-sphinx-badge--meta-beta (filled)

Package metadata β€” link badges (outline)

GitHub PyPI _link_badge(label, url)  β€” gp-sphinx-badge gp-sphinx-badge--outline gp-sphinx-badge--meta-link <a>

Package metadata β€” full header row

Alpha GitHub PyPI

Working usage examplesΒΆ

setup() registers the extension with Sphinx:

  1. add_node() registers BadgeNode with HTML visitors (visit_badge_html / depart_badge_html).

  2. add_css_file() injects the shared sphinx_ux_badges.css stylesheet.

  3. Downstream extensions call setup_extension() to load the badge layer:

def setup(app: Sphinx) -> dict[str, Any]:
    app.setup_extension("sphinx_ux_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.

Build a grouped toolbar in your own directive or transform:

from sphinx_ux_badges import build_badge, build_badge_group, build_toolbar

badge_group = build_badge_group(
    [
        build_badge(
            "readonly",
            tooltip="Read-only operation",
            classes=["gp-sphinx-fastmcp__safety-readonly"],
        ),
        build_badge(
            "tool",
            tooltip="FastMCP tool entry",
            classes=["gp-sphinx-fastmcp__type-tool"],
        ),
    ],
)
toolbar = build_toolbar(badge_group, classes=["my-extension-toolbar"])

Colour paletteΒΆ

All semantic badge colours live in sab_palettes.css (registered by this extension). Every sphinx-autodoc-* package uses the SAB.* constants instead of its own colour classes. The live demo below shows every variant.

Colour class

SAB constant

Used for

gp-sphinx-badge--type-function

SAB.TYPE_FUNCTION

Python functions (blue)

gp-sphinx-badge--type-class

SAB.TYPE_CLASS

Python classes (indigo)

gp-sphinx-badge--type-method

SAB.TYPE_METHOD

Instance / class / static methods (cyan)

gp-sphinx-badge--type-property

SAB.TYPE_PROPERTY

Properties (teal)

gp-sphinx-badge--type-attribute

SAB.TYPE_ATTRIBUTE

Attributes (slate)

gp-sphinx-badge--type-data

SAB.TYPE_DATA

Module-level data (grey)

gp-sphinx-badge--type-exception

SAB.TYPE_EXCEPTION

Exceptions (rose/red)

gp-sphinx-badge--type-typealias

SAB.TYPE_TYPEALIAS

Type aliases (violet)

gp-sphinx-badge--type-module

SAB.TYPE_MODULE

Modules (green)

gp-sphinx-badge--mod-async

SAB.MOD_ASYNC

async modifier (purple outline)

gp-sphinx-badge--mod-classmethod

SAB.MOD_CLASSMETHOD

classmethod modifier (amber outline)

gp-sphinx-badge--mod-staticmethod

SAB.MOD_STATICMETHOD

staticmethod modifier (grey outline)

gp-sphinx-badge--mod-abstract

SAB.MOD_ABSTRACT

abstract modifier (indigo outline)

gp-sphinx-badge--mod-final

SAB.MOD_FINAL

final modifier (emerald outline)

gp-sphinx-badge--state-deprecated

SAB.STATE_DEPRECATED

deprecated (muted red, shared across domains)

gp-sphinx-badge--type-fixture

SAB.TYPE_FIXTURE

pytest fixtures (green)

gp-sphinx-badge--scope-session

SAB.SCOPE_SESSION

session-scope fixtures (amber)

gp-sphinx-badge--scope-module

SAB.SCOPE_MODULE

module-scope fixtures (teal)

gp-sphinx-badge--scope-class

SAB.SCOPE_CLASS

class-scope fixtures (slate)

gp-sphinx-badge--state-factory

SAB.STATE_FACTORY

factory fixtures (amber outline)

gp-sphinx-badge--state-override

SAB.STATE_OVERRIDE

override hooks (violet outline)

gp-sphinx-badge--state-autouse

SAB.STATE_AUTOUSE

autouse fixtures (rose outline)

gp-sphinx-badge--type-config

SAB.TYPE_CONFIG

Sphinx config values (amber)

gp-sphinx-badge--mod-rebuild

SAB.MOD_REBUILD

Sphinx rebuild mode (grey outline)

gp-sphinx-badge--type-directive

SAB.TYPE_DIRECTIVE

docutils directives (violet)

gp-sphinx-badge--type-role

SAB.TYPE_ROLE

docutils roles (violet)

gp-sphinx-badge--type-option

SAB.TYPE_OPTION

docutils directive options (violet)

API referenceΒΆ

class sphinx_ux_badges.BadgeSpec
ΒΆ
class

Bases: object

Typed badge descriptor used by producer extensions.

Parameters:
  • text (str) – Visible badge label.

  • tooltip (str) – Tooltip and aria label for the badge.

  • icon (str) – Optional icon rendered via ::before.

  • classes (tuple[str, ...]) – Additional CSS classes for package-specific color and role styling.

  • style ({"full", "icon-only", "inline-icon"}) – Structural variant.

  • fill ({"filled", "outline"}) – Visual fill variant.

  • size (str) – Optional size token: "xxs", "xs", "sm", "md", "lg", or "xl".

  • tabindex (str) – Focus behavior token forwarded to BadgeNode.

Examples

>>> spec = BadgeSpec("config", tooltip="Sphinx config value")
>>> spec.text
'config'
sphinx_ux_badges.build_badge_from_spec(spec)
ΒΆ
function

Build a BadgeNode from a typed BadgeSpec.

Parameters:

spec (BadgeSpec) – Structured badge description from a producer extension.

Returns:

Renderable badge node.

Return type:

BadgeNode

sphinx_ux_badges.build_badge(text, *, tooltip='', icon='', classes=(), style='full', fill='filled', size='', tabindex='0')
ΒΆ
function

Build a single badge node.

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 ({β€œfull”, β€œicon-only”, β€œinline-icon”}) – Structural variant.

  • fill ({β€œfilled”, β€œoutline”}) – Visual fill variant.

  • size (str) – Optional size tier: "xxs", "xs", "sm", "md", "lg", or "xl". Empty string uses the default (no extra class).

  • tabindex (str) – "0" for focusable, "" to skip.

Return type:

BadgeNode

Examples

>>> b = build_badge("async", tooltip="Asynchronous", classes=[SAB.MOD_ASYNC])
>>> b.astext()
'async'
>>> b = build_badge(
...     "",
...     style="icon-only",
...     classes=["gp-sphinx-fastmcp__safety-readonly"],
... )
>>> SAB.ICON_ONLY in b["classes"]
True
>>> b = build_badge("big", size="lg")
>>> SAB.LG in b["classes"]
True
sphinx_ux_badges.build_badge_group(badges, *, classes=())
ΒΆ
function

Wrap badges in a group container with inter-badge spacing.

Parameters:
  • badges (Sequence[BadgeNode]) – Badge nodes to group.

  • classes (Sequence[str]) – Additional CSS classes on the group container.

Return type:

nodes.inline

Examples

>>> from sphinx_ux_badges._nodes import BadgeNode
>>> g = build_badge_group([BadgeNode("a"), BadgeNode("b")])
>>> SAB.BADGE_GROUP in g["classes"]
True
sphinx_ux_badges.build_toolbar(badge_group, *, classes=())
ΒΆ
function

Wrap a badge group in a toolbar (margin-left: auto for flex titles).

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_ux_badges._nodes import BadgeNode
>>> g = build_badge_group([BadgeNode("x")])
>>> t = build_toolbar(g, classes=["gp-sphinx-fastmcp__toolbar"])
>>> SAB.TOOLBAR in t["classes"]
True
class sphinx_ux_badges.BadgeNode
ΒΆ
class

Bases: inline

Inline badge rendered as <span> with ARIA and icon support.

Subclasses nodes.inline so unregistered builders (text, LaTeX, man) fall back to visit_inline via MRO dispatch in SphinxTranslator.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: "xxs", "xs", "sm", "md", "lg", or "xl". Empty means default.

tabindex

"0"

"0" for keyboard-focusable, "" to skip.

classes

None

Additional CSS classes (plugin prefix + color class).

class sphinx_ux_badges._css.SAB
ΒΆ
class

Bases: object

CSS class constants under the gp-sphinx- namespace.

The gp-sphinx- prefix identifies these classes as part of the gp-sphinx workspace. The badge block (gp-sphinx-badge) and its sibling blocks (gp-sphinx-badge-group, gp-sphinx-toolbar) are tier-A shared concepts β€” any extension may consume them directly, and the theme may restyle them once for every consumer.

Covers both structural variants (size, outline, icon-only) and the unified semantic colour palette from sab_palettes.css (filename is historical).

All consuming sphinx-autodoc-* packages use these constants for shared badge primitives (group, badge, toolbar, type, modifier, state classes). Extension-specific layout and semantic classes live under each package’s own namespace (e.g. gp-sphinx-fastmcp__* for FastMCP tool sections, gp-sphinx-pytest-fixtures__* for fixture-index layout).

Examples

>>> SAB.PREFIX
'gp-sphinx-badge'
>>> SAB.OUTLINE
'gp-sphinx-badge--outline'
>>> SAB.TYPE_METHOD
'gp-sphinx-badge--type-method'
>>> SAB.STATE_AUTOUSE
'gp-sphinx-badge--state-autouse'
sphinx_ux_badges.setup(app)
ΒΆ
function

Register BadgeNode with HTML visitor and shared CSS.

Parameters:

app (Sphinx) – Sphinx application.

Returns:

Extension metadata.

Return type:

dict[str, Any]

Examples

>>> from sphinx_ux_badges import setup
>>> callable(setup)
True

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) ────── */
  --gp-sphinx-badge-bg: transparent;        /* badge background */
  --gp-sphinx-badge-fg: inherit;            /* badge text color */
  --gp-sphinx-badge-border: none;           /* badge border shorthand */

  /* ── Metrics ───────────────────────────────────────── */
  --gp-sphinx-badge-font-size: 0.75em;
  --gp-sphinx-badge-font-weight: 700;
  --gp-sphinx-badge-padding-v: 0.35em;      /* vertical padding */
  --gp-sphinx-badge-padding-h: 0.65em;      /* horizontal padding */
  --gp-sphinx-badge-radius: 0.25rem;        /* border-radius */
  --gp-sphinx-badge-icon-gap: 0.28rem;      /* gap between icon and label */

  /* ── Depth (inset shadow on solid badges) ──────────── */
  --gp-sphinx-badge-buff-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.2),
    inset 0 -1px 2px rgba(0, 0, 0, 0.12);
  --gp-sphinx-badge-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

--gp-sphinx-badge-bg

Badge background color. Extensions set this per badge class (e.g. green for β€œreadonly”).

--gp-sphinx-badge-fg

Badge text color. Falls back to inherit when unset.

--gp-sphinx-badge-border

Border shorthand (1px solid #...). Defaults to none.

--gp-sphinx-badge-font-size

Font size. Context-aware sizing (headings, body, TOC) overrides this.

--gp-sphinx-badge-font-weight

Font weight. Default 700 (bold).

--gp-sphinx-badge-padding-v / --gp-sphinx-badge-padding-h

Vertical and horizontal padding.

--gp-sphinx-badge-radius

Border radius for pill shape.

--gp-sphinx-badge-icon-gap

Gap between the ::before icon and the label text.

--gp-sphinx-badge-buff-shadow

Subtle inset highlight + shadow for depth on light backgrounds.

--gp-sphinx-badge-buff-shadow-dark-ui

Stronger inset shadow variant for dark theme / prefers-color-scheme: dark.

CSS class referenceΒΆ

All classes use the sab- prefix (sphinx autodoc badges).

Class

Applied by

Description

gp-sphinx-badge

BadgeNode

Base class. Always present on every badge.

gp-sphinx-badge--outline

build_badge(fill="outline")

Transparent background, inherits text color.

gp-sphinx-badge--icon-only

build_badge(style="icon-only")

16 Γ— 16 colored box with emoji ::before.

gp-sphinx-badge--inline-icon

build_badge(style="inline-icon")

Bare emoji inside a code chip, no background.

gp-sphinx-badge-group

build_badge_group()

Flex container with gap: 0.3rem between badges.

gp-sphinx-toolbar

build_toolbar()

Flex push-right (margin-left: auto) for title rows.

gp-sphinx-badge--size-xxs

build_badge(size="xxs") / BadgeNode(..., badge_size="xxs")

Minimum size (status dots, very tight layouts).

gp-sphinx-badge--size-xs

build_badge(size="xs") / BadgeNode(..., badge_size="xs")

Extra small (dense tables, tight UI).

gp-sphinx-badge--size-sm

build_badge(size="sm")

Small inline badges.

gp-sphinx-badge--size-md

build_badge(size="md")

Medium β€” larger than the default but smaller than lg.

gp-sphinx-badge--size-lg

build_badge(size="lg")

Large (section titles, callouts).

gp-sphinx-badge--size-xl

build_badge(size="xl")

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 (gp-sphinx-badge--size-xs … gp-sphinx-badge--size-xl) override contextual sizing when present (higher specificity than context rules).

Context

Font size

Selectors

Heading (h2, h3)

0.68rem

.body h2 .gp-sphinx-badge, [role="main"] h3 .gp-sphinx-badge

Body (p, li, td, a)

0.62rem

.body p .gp-sphinx-badge, [role="main"] li .gp-sphinx-badge, etc.

TOC sidebar

0.58rem

.toc-tree .gp-sphinx-badge (compact, with emoji icons)

Downstream extensionsΒΆ

All colour variants are provided by the shared palette above. Downstream extensions reference SAB.* constants instead of maintaining their own sab-* / spf-* / sas-* / sadoc-* colour classes.

Extension

Badge types used

sphinx-autodoc-fastmcp

Safety tiers (readonly / mutating / destructive), MCP tool type (smf-* β€” FastMCP-specific colours not in shared palette)

sphinx-autodoc-api-style

SAB.TYPE_FUNCTION, SAB.TYPE_CLASS, SAB.TYPE_METHOD, modifiers, SAB.STATE_DEPRECATED

sphinx-autodoc-pytest-fixtures

SAB.TYPE_FIXTURE, SAB.SCOPE_*, SAB.STATE_FACTORY, SAB.STATE_OVERRIDE, SAB.STATE_AUTOUSE

sphinx-autodoc-sphinx

SAB.TYPE_CONFIG, SAB.MOD_REBUILD

sphinx-autodoc-docutils

SAB.TYPE_DIRECTIVE, SAB.TYPE_ROLE, SAB.TYPE_OPTION

Package referenceΒΆ

Copyable config snippet

extensions = [
    "sphinx_ux_badges",
]

Package metadata

Registered Surface

sphinx_ux_badges

Source on GitHub Β· PyPI