from brand_yml import Brand
brand = Brand.from_yaml_str("""
meta:
name: Brand YAML
color:
primary: "#ff0202"
typography:
base: Open Sans
""")Brand
Brand(**data)Brand guidelines in a class.
A brand instance encapsulates the color, typography and logo preferences for a given brand, typically found in brand guidelines created by a company’s marketing department. brand_yml.Brand organizes this information in a common, fully-specified class instance that makes it easy to re-use for theming any artifact from websites to data visualizations.
Unified brand information following the Brand YAML specification. Read brand metadata from a YAML file, typically named _brand.yml, with brand_yml.Brand.from_yaml or from a YAML string with brand_yml.Brand.from_yaml_str. Or create a full brand instance directly via this class.
Attributes
- meta
-
BrandMeta | None
Key identity information, name of the company, links to brand guidelines, etc.
- logo
-
BrandLogo | BrandLogoResource | None
Files or links to the brand’s logo at various sizes.
- color
-
BrandColor | None
Named colors in the brand’s color palette and semantic colors (e.g., primary, secondary, success, warning).
- typography
-
BrandTypography | None
Font definitions, font family, weight, style, color, and line height for key elements (e.g., base, headings, and monospace text).
- defaults
-
Additional context-specific settings beyond the basic brand colors and typography.
- path
-
Path | None
The file path of the brand configuration. This attribute is excluded from serialization and representation.
Methods
| Name | Description |
|---|---|
| from_yaml | Create a Brand instance from a Brand YAML file. |
| from_yaml_str | Create a Brand instance from a string of YAML. |
| model_dump_yaml | Serialize the Brand object to YAML. |
| use_logo | Extract a logo resource from a brand. |
from_yaml
Brand.from_yaml(path=None)Create a Brand instance from a Brand YAML file.
Reads a Brand YAML file or finds and reads a _brand.yml file and returns a validated :class:Brand instance.
To find a project-specific _brand.yml file, pass path the project directory or __file__ (the path of the current Python script). brand_yml.Brand.from_yaml will look in that directory or any parent directory for a _brand.yml, brand/_brand.yml or _brand/_brand.yml file (or the same variants with a .yaml extension). Note that it starts the search in the directory passed in and moves upward to find the _brand.yml file; it does not search into subdirectories of the current directory.
Parameters
path: str | Path | None = None-
The path to the brand.yml file or a directory where
_brand.ymlis expected to be found. Typically, you can pass__file__from the calling script to find_brand.ymlor_brand.yamlin the current directory or any of its parent directories. Alternatively, if no path is specified, theBRAND_YML_PATHenvironment variable is checked for the path to the brand.yml file.
Returns
| Name | Type | Description |
|---|---|---|
A validated Brand object with all fields populated according to the brand.yml file. |
Raises
| Name | Type | Description |
|---|---|---|
| FileNotFoundError | Raises a FileNotFoundError if no brand configuration file is found within the given path. |
|
| ValueError | Raises ValueError or other validation errors from pydantic if the brand.yml file is invalid. |
Examples
from brand_yml import Brand
brand = Brand.from_yaml(__file__)
brand = Brand.from_yaml("path/to/_brand.yml")from_yaml_str
Brand.from_yaml_str(text, path=None)Create a Brand instance from a string of YAML.
Parameters
Returns
| Name | Type | Description |
|---|---|---|
A validated brand_yml.Brand object with all fields populated according to the Brand YAML text. |
Raises
| Name | Type | Description |
|---|---|---|
| ValueError | Raises ValueError or other validation errors from pydantic if the Brand YAML file is invalid. |
Examples
brand.metaBrandMeta(name=BrandMetaName(full='Brand YAML'))
brand.color.primary'#ff0202'
model_dump_yaml
Brand.model_dump_yaml(stream=None, *, transform=None)Serialize the Brand object to YAML.
Write the brand_yml.Brand instance to a string or to a file on disk.
Examples
from brand_yml import Brand
brand = Brand.from_yaml_str("""
meta:
name: Brand YAML
color:
palette:
orange: "#ff9a02"
primary: orange
typography:
headings: Raleway
""")print(brand.model_dump_yaml())meta:
name:
full: Brand YAML
color:
palette:
orange: '#ff9a02'
primary: '#ff9a02'
typography:
fonts:
- family: Raleway
headings:
family: Raleway
Parameters
Returns
| Name | Type | Description |
|---|---|---|
| Any | A string with the YAML representation of the brand if stream is None. Otherwise, the YAML representation is written to stream, typically a file. Note that the output YAML may not be 100% identical to the input _brand.yml. The output will contain the fully validated Brand instance where default or computed values may be included as well as any values resolved during validation, such as colors. |
use_logo
Brand.use_logo(
name,
variant='auto',
*,
required=None,
allow_fallback=True,
**kwargs,
)Extract a logo resource from a brand.
Returns a brand logo resource specified by name and variant from a brand object. The image paths in the returned object are adjusted to be absolute, relative to the location of the brand YAML file, if brand was read from a file, or the local working directory otherwise.
Parameters
name: str-
The name of the logo to use. Either a size (
"small","medium","large") or an image name frombrand.logo.images. Alternatively, you can also use"smallest"or"largest"to select the smallest or largest available logo size, respectively. variant: Literal['auto', 'light', 'dark', 'light-dark'] = ‘auto’-
Which variant to use, only used when
nameis one of the brand.yml fixed logo sizes ("small","medium", or"large"). Can be one of:"auto": Auto-detect, returns a light/dark logo resource if both variants are present, otherwise it returns a single logo resource, either the value forbrand.logo.{name}or the single light or dark variant if only one is present."light": Returns only the light variant. If no light variant is present, butbrand.logo.{name}is a single logo resource andallow_fallbackisTrue,use_logo()falls back to the single logo resource."dark": Returns only the dark variant, or, as above, falls back to the single logo resource if no dark variant is present andallow_fallbackisTrue."light-dark": Returns a light/dark object with both variants. If a single logo resource is present forbrand.logo.{name}andallow_fallbackisTrue, the single logo resource is promoted to a light/dark logo resource with identical light and dark variants.
required: bool | str | None = None-
Logical or string. If
True, an error is thrown if the requested logo is not found. If a string, it is used to describe why the logo is required in the error message and completes the phrase"is required ____". Defaults toFalsewhennameis one of the fixed sizes –"small","medium","large"or"smallest"or"largest". Otherwise, an error is thrown by default if the requested logo is not found. allow_fallback: bool = True-
If
True(the default), allows falling back to a non-variant-specific logo when a specific variant is requested. Only used whennameis one of the fixed logo sizes ("small","medium", or"large"). **kwargs:TagAttrValue= {}-
Additional named attributes to be added to the image HTML or markdown when created via formatting methods.
Returns
| Name | Type | Description |
|---|---|---|
BrandLogoResource | BrandLogoResourceLightDark | None |
A BrandLogoResource object, a BrandLogoResourceLightDark object, or None if the requested logo doesn’t exist and required is False. |
Raises
| Name | Type | Description |
|---|---|---|
BrandLogoMissingError |
If the requested logo is not found and required is True or a string. |
|
| ValueError | If variant is not one of "auto", "light", "dark", or "light-dark". |
Examples
from brand_yml import Brand
brand = Brand.from_yaml_str('''
logo:
small:
light:
path: https://pandas.pydata.org/static/img/pandas_mark.svg
alt: Pandas logo
dark:
path: https://pandas.pydata.org/static/img/pandas_mark_white.svg
alt: Pandas logo
medium:
path: https://pandas.pydata.org/static/img/pandas_secondary.svg
alt: Pandas logo and name
''')
brand.use_logo("small", width="100px")str(brand.use_logo("small", variant="light").to_html())'<img class="brand-logo" src="https://pandas.pydata.org/static/img/pandas_mark.svg" alt="Pandas logo"/>'
brand.use_logo("small", variant="dark").to_markdown()'{alt="Pandas logo" .brand-logo}'
brand.use_logo("medium").to_markdown()'{alt="Pandas logo and name" .brand-logo}'