Theme
Version
GitHub PyPI Discord
On this page

Dependencies

The JS/CSS dependency types collected and placed at serialize time, and the built-in citry.ext.dependencies extension that owns them.

View source

CitryDependencies class

A component's merged secondary assets (from the nested Dependencies classes).

Holds resolved entries:

  • a local file (declared with PathLike or a resolvable string) - resolved to Path
  • URLs (plain strings) - unchanged
  • Script/Style objects - unchanged
  • Pre-rendered tags (__html__) - unchanged

The entry's type is what tells the emission step what to do with it (inline the file content, emit a src/href tag, or output the tag verbatim; see emission.py).

Attributes

  • js tuple[Any, ...] - JS entries, base classes' entries first, then the class's own, de-duplicated.
  • css Mapping[str, tuple[Any, ...]] - CSS entries per media type (``"all"``, ``"print"``, ...), same ordering per list.
View source

Dependency class

Shared base of :class:Script and :class:Style.

Holds either inline content or a url, never both; rendering raises when neither or both are set.

View source

content attribute

content: str | None

Text inside the <script> or <style> tag. None for url-based dependencies.

View source

url attribute

url: str | None

If set, renders as <script src="..."> / <link rel="stylesheet" href="..."> instead of an inline tag.

View source

attrs attribute

attrs: dict[str, str | bool]

Extra HTML attributes (True renders a bare boolean attribute).

View source

kind attribute

kind: DependencyKind

What this dependency is for; see :data:DependencyKind.

View source

origin_class_id attribute

origin_class_id: str | None

class_id of the component class this dependency came from, when known. Used in error messages and for per-component hooks.

View source

render function

render() -> SafeString

Render as an HTML tag string.

View source

render_json function

render_json() -> dict[str, str | dict[str, str | bool]]

Render as a JSON-ready dict with tag, attrs, and content.

This is the shape the client-side manager consumes when it constructs the element in the browser.

View source

DependencyRecord class

Bases: tuple

One "this component instance rendered" note, collected during a render.

The dependencies extension appends one of these to the render-scoped CitryContext.extra per component render, and the notes bubble up to the root as nested renders are consumed. At serialize time the collected records are resolved into the actual Script/Style tags. The exact class is retained until serialization so hot replacement cannot mix a rendered old body with a new class's assets; heavy content lives in the cache.

View source

class_id attribute

class_id: str

Component.class_id of the rendered component's class.

View source

component_id attribute

component_id: str

The render id of the component instance (component.id).

View source

js_vars_hash attribute

js_vars_hash: str | None

Hash of the instance's js_data() result, or None when it has none.

View source

css_vars_hash attribute

css_vars_hash: str | None

Hash of the instance's css_data() result, or None when it has none.

View source

component_class attribute

component_class: type[Component] | None

Exact class version that produced the record, retained for delayed serialization.

View source

Script class

Bases: Dependency

One <script> tag.

With url set, renders <script src="...">; otherwise renders the content inline as <script>...</script>.

Example::

Script(content="console.log('hi');", attrs={"type": "module"}, wrap=False)
# <script type="module">console.log('hi');</script>
View source

wrap attribute

wrap: bool

Wrap inline content in a self-executing function, so its top-level variables do not leak into (or collide with) other scripts on the page::

(function() {
console.log('hi');
})();

Only applies to classic scripts (no type attribute or a JS MIME type); module/importmap/other types are never wrapped.

View source

to_json function

to_json() -> dict[str, Any]

Serialize for cache storage; the inverse of :meth:from_json.

View source

from_json function

from_json(data: dict[str, Any]) -> Script

Rebuild from :meth:to_json output.

View source

Style class

Bases: Dependency

One stylesheet tag.

With url set, renders <link rel="stylesheet" href="..."/>; otherwise renders the content inline as <style>...</style>.

Example::

Style(url="/static/print.css", attrs={"media": "print"})
# <link media="print" rel="stylesheet" href="/static/print.css"/>
View source

to_json function

to_json() -> dict[str, Any]

Serialize for cache storage; the inverse of :meth:from_json.

View source

from_json function

from_json(data: dict[str, Any]) -> Style

Rebuild from :meth:to_json output.

View source

render function

render() -> SafeString
View source

DependenciesExtension class

Bases: Extension

The built-in extension owning the Dependencies secondary-asset class.

The loading half reads each component or reusable definition base's preserved Dependencies declaration, resolves and merges declarations lazily in :meth:resolve, and drops a class's derived state when its files are reset or its final registry alias is removed.

The emission half (docs/design/dependencies.md): records each component render (on_component_data), bubbles the records up as nested renders are consumed (on_render_context_merge), and at serialize time turns them into <script>/<style>/<link> tags placed into the page (on_serialize, implemented in emission.py).

View source

export_render_cache function

export_render_cache(ctx: OnRenderCacheExportContext) -> dict[str, object]

Detach selected dependency records and exact variable-script values.

View source

stage_render_cache function

stage_render_cache(ctx: OnRenderCacheStageContext) -> StagedRenderCacheContribution

Validate dependency payloads and prepare exact cache repairs.

View source

resolve function

resolve(comp_cls: type[Component]) -> CitryDependencies

Resolve and merge comp_cls's secondary assets, cached per class.

Merge order is bases first, own entries last: list order becomes document order at emission and CSS breaks equal-specificity ties by document order, so the more specialized class's styles must come later to win (docs/design/asset_loading.md section 7.3).

Component.Dependencies.extend picks the bases:

  • True - inherit JS/CSS from Component.Dependencies of Component's base classes
  • False - no inheritance; only the class's own entries (if any)
  • a list - exactly those classes + their bases, in the order given

An explicit Dependencies = None declaration means no own entries and no inheritance.

View source

OnDependenciesContext class

Context for the on_dependencies hook, owned by the dependencies extension (not a "core" hook: any extension that defines an on_dependencies method receives it, via the manager's emit).

Fires at serialize time with the final, deduplicated tag lists (possibly empty), just before they are rendered into the page. Mutate the lists in place to add, remove, or reorder entries.

View source

citry attribute

citry: Citry

The Citry instance the render belongs to.

View source

scripts attribute

scripts: list[Dependency]

The <script> entries about to be emitted, in document order (mutable).

View source

styles attribute

styles: list[Dependency]

The stylesheet entries about to be emitted, in document order (mutable).

View source

context attribute

context: CitryContext

The root render's CitryContext. Its extra carries everything that bubbled up during the render, so an extension can read back what its render-time hooks collected.

View source

strategy attribute

strategy: str

The serialize(deps_strategy=...) value this emission runs under ("document", "simple", or "fragment").

View source

before_manifest attribute

before_manifest: list[Dependency]

Entries rendered as tags immediately before the data-citry page manifest tag (mutable). For anything that must already be in the DOM when the client-side manager processes the manifest, e.g. the events extension's own manifest tag. Only the strategies that emit the page manifest render these ("document" and "fragment"); under "simple" they are not emitted.

View source

get_dependencies function

get_dependencies(comp_cls: type[Component]) -> CitryDependencies

The merged secondary assets of a component class.

Routes through the class's Citry instance to its built-in dependencies extension. Users reach this through Card.get_dependencies().

Citry version: 0.3.0