from brand_yml import Brand
= Brand.from_yaml_str("""
brand meta:
name: Brand YAML
color:
primary: "#ff0202"
typography:
base: Open Sans
""")
Brand
**data) Brand(
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
=None) Brand.from_yaml(path
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.yml
is expected to be found. Typically, you can pass__file__
from the calling script to find_brand.yml
or_brand.yaml
in the current directory or any of its parent directories. Alternatively, if no path is specified, theBRAND_YML_PATH
environment 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.from_yaml(__file__)
brand = Brand.from_yaml("path/to/_brand.yml") brand
from_yaml_str
=None) Brand.from_yaml_str(text, path
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.meta
BrandMeta(name=BrandMetaName(full='Brand YAML'))
brand.color.primary
'#ff0202'
model_dump_yaml
=None, *, transform=None) Brand.model_dump_yaml(stream
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.from_yaml_str("""
brand 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,='auto',
variant*,
=None,
required=True,
allow_fallback**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
name
is 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_fallback
isTrue
,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_fallback
isTrue
."light-dark"
: Returns a light/dark object with both variants. If a single logo resource is present forbrand.logo.{name}
andallow_fallback
isTrue
, 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 toFalse
whenname
is 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 whenname
is 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.from_yaml_str('''
brand 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
''')
"small", width="100px") brand.use_logo(
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"/>'
"small", variant="dark").to_markdown() brand.use_logo(
'{alt="Pandas logo" .brand-logo}'
"medium").to_markdown() brand.use_logo(
'{alt="Pandas logo and name" .brand-logo}'