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

DependenciesConfig class

Bases: ExtensionConfig

Typed runtime view of one component's Dependencies declaration.

Citry creates this value from the component's nested Dependencies class. Read it through component.dependencies while the component is rendering. Use Component.get_dependencies() when you need the resolved, inherited asset list.

Attributes

  • js Any - JavaScript dependency declarations.
  • css Any - CSS dependency declarations.
  • extend bool | list[type[Component]] - Whether to inherit dependencies, or which component classes to inherit them from.
  • local_files str - Whether local dependency files are inlined or served from Citry's asset routes.
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 tells Citry what to do with it when it writes the page: inline the file content, emit a src/href tag, or output the tag verbatim.

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 Script and 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: "core" (scripts Citry adds itself to run its browser runtime, such as the runtime, the script that loads it for a fragment, and the data it reads to mount components), "component" (a component's own js or css), "variables" (a generated stylesheet carrying css_data() values), or "extra" (anything else, such as a Dependencies entry).

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() -> Markup

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

Always None: js_data() values reach the browser inside the page's Vue app data.

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"},
)
# <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; from_json() reverses it.

View source

from_json function

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

Rebuild a dependency from the output of to_json().

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; from_json() reverses it.

View source

from_json function

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

Rebuild a dependency from the output of to_json().

View source

DependenciesExtension class

Bases: Extension

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

The first time resolve() is called for a class, the extension reads its Dependencies declaration, merges it with its bases' declarations, and caches the result. It drops that result when the class's files are reset or its last registered name is removed.

During a render, the extension records each component render (on_component_data) and passes the records up to the parent as nested renders finish (on_render_context_merge). When the page is serialized (on_serialize), it turns the records into <script>/<style>/<link> tags placed into the page.

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.

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

selected_render attribute

selected_render: CitryRender

The exact render selected for serialization and asset filtering.

View source

strategy attribute

strategy: str

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

View source

early_scripts attribute

early_scripts: list[Dependency]

Scripts that must run before the other dependency scripts (mutable). Static output writes them as tags ahead of the dependency scripts; under simple they are emitted with the other direct dependency tags. On an interactive page the Vue app loads them after the Vue runtime and ahead of scripts, in list order, under the same rules as any entry in scripts. When the page loads, these scripts run before Citry creates the page's first Vue app, so one of them can call Citry.vue.use() to install a Vue plugin for that app.

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.9.0