Catalog class

class Catalog

Manager of the components and their global settings.

Catalog(
    folder: str | pathlib._local.Path | None = None,
    *,
    jinja_env: jinja2.environment.Environment | None = None,
    extensions: list | None = None,
    filters: dict[str, typing.Any] | None = None,
    tests: dict[str, typing.Any] | None = None,
    auto_reload: bool = True,
    **globals: Any
) -> None
Argument Description
folder Optional folder path to scan for components. It's a shortcut to
calling add_folder when only one is used.
jinja_env Optional Jinja2 environment to use for rendering.
extensions Optional extra Jinja2 extensions to add to the environment.
filters Optional extra Jinja2 filters to add to the environment.
tests Optional extra Jinja2 tests to add to the environment.
auto_reload Whether to check the last-modified time of the components files and
automatically re-process them if they change. The performance impact of
leaving it on is minimal, but might be noticeable when rendering a
component that uses a large number of child components.
**globals Variables to make available to all components by default.

function add_folder

Add a folder path from which to search for components, optionally under a prefix.

add_folder(
    path: str | pathlib._local.Path,
    *,
    prefix: str = '',
    preload: bool = True
) -> None
Argument Description
path Absolute path of the folder with component files.
prefix Optional path prefix that all the components in the folder
will have. The default is empty.
preload Whether to preload the data of components in the folder.
If set to True (the default), the component data will be loaded into
memory when the folder is added, instead of just before rendering it.
This makes the first render faster at the expense of a few
microseconds upfront.

Components without a prefix can be imported as a path relative to the folder, e.g.: sub/folder/component.jinja or with a path relative to the component where it is used: ./folder/component.jinja.

Relative imports cannot go outside the folder.

Components added with a prefix must be imported using the @prefix/ syntax: @prefix/sub/folder/component.jinja. If the importing is done from within a component with the prefix itself, a relative import can also be used, e.g.: ./component.jinja.

All the folders added under the same prefix will be treated as if they were a single folder. This means if you add two folders, under the same prefix, with a component with the same subpath/filename, the one in the folder added first will be used and the other ignored.

WARNING: You cannot move or delete components files from the folder after calling this method, but you can call it again to add new components added to the folder. This is unrelated to the value of preload.

function get_component

Instantiate and return a component object by its relative path.

get_component(
    relpath: str
) -> jx.component.Component
Argument Description
relpath The path of the component to render, including the extension,relative to its view folder.
e.g.: "sub/component.jinja". Always use the forward slash (/) as the path separator.

function get_component_data

Get the component data from the cache. If the file has been updated, the component is re-processed.

get_component_data(
    relpath: str
) -> jx.catalog.CData
Argument Description
relpath The path of the component to render, including the extension,relative to its view folder.
e.g.: "sub/component.jinja". Always use the forward slash (/) as the path separator.

function get_signature

Return a component's signature including its arguments and metadata.

get_signature(
    relpath: str
) -> dict[str, typing.Any]
Argument Description
relpath The path of the component, including the extension, relative to its view folder.
e.g.: "sub/component.jinja". Always use the forward slash (/) as the path separator.

Returns: A dictionary containing:

  • required: dict of required argument names mapped to their type (or None)
  • optional: dict of optional arguments mapped to (default_value, type or None)
  • slots: tuple of slot names
  • css: tuple of CSS file URLs
  • js: tuple of JS file URLs

function list_components

Return all registered component paths.

list_components() -> list[str]

Returns: A list of component relative paths (e.g., ["button.jinja", "card.jinja"]).

function render

Render a component with the given relative path and context.

render(
    relpath: str,
    globals: dict[str, typing.Any] | None = None,
    **kwargs
) -> str
Argument Description
relpath The path of the component to render, including the extension,relative to its view folder.
e.g.: "sub/component.jinja". Always use the forward slash (/) as the path separator.
globals Optional global variables to make available to the component and all its
imported components.
**kwargs Keyword arguments to pass to the component.
They will be available in the component's context but not to its imported components.

Returns: The rendered component as a string.

function render_string

Render a component from a string source. Works like render, but the component is not cached and cannot do relative imports.

render_string(
    source: str,
    globals: dict[str, typing.Any] | None = None,
    **kwargs
) -> str
Argument Description
source The Jinja2 source code of the component to render.
globals Optional global variables to make available to the component and all its
imported components.
**kwargs Keyword arguments to pass to the component.
They will be available in the component's context but not to its imported components.

Returns: The rendered component as a string.