Skip to content

Icon Provider

This is a helper weather_client class that can be used to resolve weather icon into references that a client web application can use.

The sc_utility library currently supports the following weather icon libraries:

key Library Name GitHub source
meteocons Meteocons basmilius weather-icons
weather-icons Erik Flowers “Weather Icons” erikflowers weather-icons

The key is used in the library argument when initialising the WeatherIconProvider class:

from pathlib import Path

import weather_client
from weather_client.icon_provider import WeatherIconProvider


ICON_LIBRARY = "meteocons"
ICON_THEME = "fill-animated"
STATIC_ICON_PREFIX = "/static/weather-icons"


def build_icon_url(icon_provider: WeatherIconProvider, icon_name: str) -> str:
    prefix_path = Path(weather_client.__file__).resolve().parent / "weather_icons"
    icon_rel_path = icon_provider.get_icon_relative_path(icon_name)
    return f"{prefix_path}/{icon_rel_path}"


icon_provider = WeatherIconProvider(library=ICON_LIBRARY, theme=ICON_THEME, is_cropped=True)
icon_name = "raindrop"
print(f"Example icon URL: {build_icon_url(icon_provider, icon_name)}")

The icon library's support different themes. Within each theme, an uncropped and cropped version of the SVG image is provided. The uncropped version is an original copy from the developer's Github repo. The cropped version removes most of the whitespace around the active elements of the image.

Note that this library includes the /scripts/crop_svg_viewbox.py whcih can be used to crop the excess whitespace from the default SVG files. For example, we used this command to crop the meteocons files:

uv run scripts/crop_svg_viewbox.py src/weather_client/weather_icons/meteocons/default src/weather_client/weather_icons/meteocons/cropped --target-fill 0.9

The following themes are supported

key Themes Cropping style
meteocons fill-static
fill-animated
line-static
line-animated
monochrome-black-static
monochrome-white-static
cropped
uncropped
weather-icons monochrome-black-static
monochrome-white-static
cropped
uncropped

Resolve normalized weather conditions to icon metadata and assets.

__init__(library='meteocons', theme=None, is_cropped=False)

Initialize the provider with a specific icon library and theme.

Parameters:

Name Type Description Default
library str

The icon library to use. One of ("meteocons", "weather-icons").

'meteocons'
theme str | None

The theme variant to use within the library (e.g., "fill-static"). If None, the library's default theme will be used.

None
is_cropped bool | None

Whether to use cropped icons. Defaults to False.

False

Raises:

Type Description
ValueError

If the specified library or theme is not supported.

export_icons(destination)

Export the icon SVGs for the current library and theme to a local directory.

Parameters:

Name Type Description Default
destination str | Path

The base directory to which the icons should be exported. The icons will be placed in a subdirectory structure based on the library and theme.

required

Returns:

Type Description
Path

The path to the directory containing the exported icons.

get_icon_relative_path(icon_name)

Get the relative path to an icon SVG within the library's resource structure.

Parameters:

Name Type Description Default
icon_name str

The name of the icon (without file extension) to retrieve.

required

Returns:

Type Description
str

The relative path to the icon SVG.

get_icon_resource(icon_name)

Get a reference to the icon SVG resource.

Parameters:

Name Type Description Default
icon_name str

The name of the icon (without file extension) to retrieve.

required

Returns:

Type Description
Traversable

A reference to the icon SVG resource.

get_icon_svg(icon_name)

Get the SVG content of an icon as a string.

Parameters:

Name Type Description Default
icon_name str

The name of the icon (without file extension) to retrieve.

required

Returns:

Type Description
str

The SVG content of the icon as a string.