Rendering logic¶
pantra is intended to manipulate DOM tree and content, where each component is declared in HTML-like text file. This part of magic named is doing by rendering engine, or renderer:
Locate component related template file.
Read and parse it by ANTLR4 parser.
Generate
HTMLTemplatetemplates objects tree.Use template objects tree as a factory to render components:
Generate specific render nodes
Precompile local scripts
Run context initializers
Update components states on demand.
Additionally, renderer controls access to the static and media files, so, it is connected with routing.
Node templates¶
- class pantra.components.template.HTMLTemplate(tag_name: str, parent: Self | None = None, attributes: dict | None = None, text: str = None)[source]¶
The rendering node template based on HTML-like source file
- filename¶
component source file name (for the root node)
- Type:
Path
- code_metrics¶
compiled Python code metrics
- Type:
CodeMetrics
- class pantra.components.template.MacroCode(type: MacroType, reactive: bool, evaluated: bool, code: CodeType | list[str] | str | None, src: str)[source]¶
Python code adaptation for scripts and expressions
- Parameters:
- pantra.components.template.collect_template(name: str, session: Session | None = None, app: str | None = None) HTMLTemplate | None[source]¶
Use DEFAULT_RENDERER to collect template of the specified component name
- Parameters:
- Return type:
Optional[HTMLTemplate]
Rendering model¶
- class pantra.components.render.renderer_base.RendererBase(ctx: Context)[source]¶
Renderer base class
- Parameters:
ctx (Context)
- ctx¶
Local context of component
- abstractmethod classmethod collect_template(name: str, session: Session | None = None, app: str | None = None) HTMLTemplate | CodeType | None[source]¶
Abstract method to collect component’s template by given name
- Parameters:
- Returns:
HTMLTemplateor factory- Return type:
Optional[HTMLTemplate | CodeType]
- add(tag_name: str, parent: RenderNode) HTMLElement[source]¶
Add HTML element with specified tag to this context.
- Parameters:
tag_name (str) – tag name (“div”, “p”, etc.)
parent (RenderNode) – parent node inside this context, if specified
- Return type:
- abstractmethod build_node(template: Any, parent: RenderNode | None = None) RenderNode | None[source]¶
Build child node from given template within current context
- Parameters:
template (Any) – template node or factory
parent (Optional[RenderNode]) – parent node inside this context, if specified
- Return type:
Optional[RenderNode]
- update_children(node: RenderNode)[source]¶
Update all child nodes recursively
- Parameters:
node (RenderNode) – node to update
- abstractmethod update(node: RenderNode, recursive: bool = False)[source]¶
Update rendered node, to sync changes
- Parameters:
node (RenderNode) – node to update
recursive (bool) – update children recursively
- render(template: str | HTMLTemplate | CodeType, parent: RenderNode = None, locals: dict = None, build: bool = True)[source]¶
Render new node.
- Parameters:
template (str | HTMLTemplate | CodeType) – template to render or code
parent (RenderNode) – parent node inside this context, if specified
locals (dict) – locals dict for current context
build (bool) – whether to build the node
There are two builtin renderers:
RendererHTML- to render from HTML-like filesRendererCached- to render from cached classes
Default renderer is specified by parameter DEFAULT_RENDERER in configuration.