Skip to content

functional

Class info

🛈 DocStrings

Functional API for looking up font icons.

get_icon async

get_icon(icon_name: str) -> Icon

Get a font icon, downloading the font on first use.

Parameters:

Name Type Description Default
icon_name str

Name in format "prefix.name" (e.g. "fa.heart")

required

Returns:

Type Description
Icon

Icon with the Unicode character and the path to the font file

Raises:

Type Description
ValueError

If the prefix is unknown or the icon does not exist

Source code in src/pycons/functional.py
async def get_icon(icon_name: str) -> Icon:
    """Get a font icon, downloading the font on first use.

    Args:
        icon_name: Name in format "prefix.name" (e.g. "fa.heart")

    Returns:
        Icon with the Unicode character and the path to the font file

    Raises:
        ValueError: If the prefix is unknown or the icon does not exist
    """
    from pycons.font_providers.registry import FontRegistry

    registry = FontRegistry()
    return await registry.get_icon(icon_name)

get_icon_from_iconify_id async

get_icon_from_iconify_id(iconify_id: str) -> Icon

Get an icon using an Iconify identifier format.

Parameters:

Name Type Description Default
iconify_id str

Icon identifier in Iconify format (e.g., "mdi:home" or "material-symbols:home-outline")

required

Returns:

Type Description
Icon

Icon object

Raises:

Type Description
ValueError

If the icon identifier is invalid or the icon is not found

Source code in src/pycons/functional.py
async def get_icon_from_iconify_id(iconify_id: str) -> Icon:
    """Get an icon using an Iconify identifier format.

    Args:
        iconify_id: Icon identifier in Iconify format (e.g., "mdi:home" or
                   "material-symbols:home-outline")

    Returns:
        Icon object

    Raises:
        ValueError: If the icon identifier is invalid or the icon is not found
    """
    from pycons.font_providers.registry import FontRegistry

    # Parse the Iconify identifier
    if ":" not in iconify_id:
        msg = f"Invalid Iconify identifier: {iconify_id}. Expected format: 'prefix:name'"
        raise ValueError(msg)

    namespace, icon_name = iconify_id.split(":", 1)

    # Map Iconify namespaces to our provider prefixes
    namespace_map = {
        "mdi": "mdi",  # Community Material Design
        "fa6-regular": "fa",  # Font Awesome Regular
        "fa6-solid": "fas",  # Font Awesome Solid
        "fa6-brands": "fab",  # Font Awesome Brands
        "codicon": "msc",  # VS Code Codicons
        "ph": "ph",  # Phosphor
        "ri": "ri",  # Remix
        "el": "el",  # Elusive
    }

    # Special handling for material-symbols
    if namespace == "material-symbols":
        if icon_name.endswith("-outline"):
            prefix = "mso"  # Material Symbols Outlined
            name = icon_name[:-8]  # Remove "-outline" suffix
        elif icon_name.endswith("-rounded"):
            prefix = "msr"  # Material Symbols Rounded
            name = icon_name[:-8]  # Remove "-rounded" suffix
        elif icon_name.endswith("-sharp"):
            prefix = "mss"  # Material Symbols Sharp
            name = icon_name[:-6]  # Remove "-sharp" suffix
        else:
            # Default to outlined if no style suffix
            prefix = "mso"
            name = icon_name
    else:
        # For other namespaces, look up the mapping
        prefix = namespace_map.get(namespace)
        if not prefix:
            msg = f"Unsupported Iconify namespace: {namespace}"
            raise ValueError(msg)
        name = icon_name

    # Use our standard get_icon method with the mapped prefix and name
    registry = FontRegistry()
    return await registry.get_icon(f"{prefix}.{name}")

get_icon_sync

get_icon_sync(icon_name: str) -> Icon

Synchronous version of get_icon.

Source code in src/pycons/functional.py
def get_icon_sync(icon_name: str) -> Icon:
    """Synchronous version of `get_icon`."""
    return asyncio.run(get_icon(icon_name))