Node classes

class pantra.common.UniNode(parent: Self | None = None)[source]

Simple tree of nodes

Parameters:

parent (Optional[Self])

property parent: Self

parent property

property children: list[Self]

children property

append(node: Self)[source]

append node to children

Parameters:

node (Self)

remove(node: Self)[source]

remove node from children

Parameters:

node (Self)

clear()[source]

clear all children

index()[source]

return index of current node

move_child(from_i: int, to_i: int)[source]

move child node from one index to another

Parameters:
move(to_i: int)[source]

move this node to another index

Parameters:

to_i (int)

path() str[source]

return path of current node

Return type:

str

property root

return root node of this tree

select(predicate: Callable[[Self], bool], depth: int = None) Generator[Self][source]

select all nodes by predicate condition

Parameters:
  • predicate (Callable[[Self], bool]) – lambda expression, checking each item for condition

  • depth (int) – amount of levels depth for recursion

Yields:

selected node

Return type:

Generator[Self]

upto(predicate: str | Callable[[Self], bool]) Self | None[source]

find the closest ascendant by specified predicate

Parameters:

predicate (Union[str, Callable[[Self], bool]]) – lambda expression, checking each item for condition, or string representation of expected node

Returns:

first matched node or None

Return type:

Optional[Self]

downto(predicate: Callable[[Self], bool], depth: int = None) Self | None[source]

find the closest descendant by specified predicate

Parameters:
  • predicate (Callable[[Self], bool]) – lambda expression, checking each item for condition, or string representation of expected node

  • depth (int) – amount of levels depth for recursion

Returns:

first matched node or None

Return type:

Optional[Self]

class pantra.common.UniqueNode(parent: Self = None)[source]

Extension to UniNode with unique OID and weakref support

Parameters:

parent (Self)

oid_gen

oid generator

oid

unique object identifier (usually, within session)

class pantra.components.render.render_node.RenderNode(parent: RenderNode | None, shot: ContextShotLike | None = None, session: Session | None = None, context: Context | None = None)[source]

Base class for rendered nodes (extension to UniqueNode).

Parameters:
  • parent (Optional[RenderNode])

  • shot (Optional[ContextShotLike])

  • session (Optional[Session])

  • context (Optional[Context])

context

reference to main component context

Type:

Context

shot

snapshot manager to update changes

Type:

ContextShot

session

reference to current session

Type:

Session

scope

reference to the scope

Type:

DynamicDict

rebind_requested

whether this node should be rebound to another parent

Type:

bool

render_this_node

whether this node should be rendered

Type:

bool

property top_most_scope: ScopeDict

get parent scope or this one

rebind()[source]

request rebind for this node after parent changed

render(template: str | HTMLTemplate, locals: dict = None, build: bool = True)[source]

render new child node.

Parameters:
  • template (Union[str, HTMLTemplate]) – template to render or code

  • locals (dict) – locals dict for current context

  • build (bool) – whether to build the node

add(tag_name: str, attributes: dict = None, text: str = None) HTMLElement | Context | None[source]

add and render component context or HTML element

Parameters:
  • tag_name (str) – tag name (lowercase for HTML node)

  • attributes (dict) – attributes to set

  • text (str) – text to set

Returns:

added node

Return type:

HTMLElement | Context | None

empty() Self[source]

remove all child nodes

Return type:

Self

remove(node: RenderNode | None = None)[source]

remove this or specified node

Parameters:

node (Optional[RenderNode])

update(with_attrs: bool = False)[source]

send node changes to the client-side

Parameters:

with_attrs (bool) – whether to update dynamic attributes

update_tree()[source]

update and send this node and all its children

frozen_clone(new_parent: RenderNode | None = None) HTMLElement | TextNode | None[source]

make visual copy of the node and all its children

Visual copy means - all dynamic nodes become static, freezing last states and values

Parameters:

new_parent (Optional[RenderNode]) – optionally, new parent node

Returns:

new node

Return type:

Optional[HTMLElement, TextNode]

select(predicate: str | Iterable[str] | Callable[[RenderNode], bool], depth: int = None) Generator[RenderNode][source]

select all nodes by condition

Parameters:
  • predicate (Union[str, Iterable[str], Callable[[RenderNode], bool]]) – lambda function, string representation of list of string representations

  • depth (int) – depth of selection

Yields:

selected node

Return type:

Generator[RenderNode]

contains(predicate: str | Iterable[str] | Callable[[RenderNode], bool], depth: int = None) bool[source]

check if node is contained by predicate

Arguments are similar to select()

Parameters:
Return type:

bool

kill_task(func_name: str | Callable)[source]

kill running task by context OID and function name

Read more: session tasks

Parameters:

func_name (str | Callable)

kill_all_tasks()[source]

kill all running tasks in this context

class pantra.components.context.Context(template_name: str, parent: RenderNode | None = None, shot: ContextShot | None = None, session: Session | None = None, locals: dict | None = None)[source]

Context of the component.

Parameters:
  • template_name (str)

  • parent (Optional[RenderNode])

  • shot (Optional[ContextShot])

  • session (Optional[Session])

  • locals (Optional[dict])

name

name of this component

Type:

str

attributes

initial attributes defined with component

Type:

DynamicDict

locals

context local variables, evaluated by Python script

Type:

ReactDict

refs

references to named nodes

Type:

dict[str, ‘Context’ | HTMLElement]

ref_name

reference name if specified

Type:

str

template

template to build component context

Type:

HTMLTemplate | CodeType

ns_type

namespace ID

Type:

NSType

code_metrics

code metrics of the Python script

Type:

CodeMetrics

data_nodes

collection of data nodes

Type:

dict[str, RenderNode]

renderer

associated renderer

Type:

RenderBase

allowed_call

set of allowed function names

Type:

set[str]

rebuild()[source]

clear component and build from template

div(classes: str = '', src: HTMLElement | None = None, from_x: int = 0, from_y: int = 0, attributes: dict | None | None = None)[source]

add positioned <div> container

Usually used to make temporary box to visualize drag-and-drop.

Parameters:
  • classes (str) – classes list separated by space

  • src (HTMLElement) – source HTML element for box metrics

  • from_x (int) – x coordinate to move source box

  • from_y (int) – y coordinate to move source box

  • attributes (dict[str, Any]) – any other custom attributes of container

get_caller(action: str) Callable | Awaitable | None[source]

get callable local action by name

Parameters:

action (str)

Return type:

Callable | Awaitable | None

call(action: str, *args, **kwargs)[source]

call local action by name

Parameters:

action (str)

set_quietly(key, value)[source]

shortcut to set local context variables quietly

allow_call(method: str)[source]

add method name to allowed calls set for JS callback

Parameters:

method (str)

is_call_allowed(method: str) bool[source]

check if method is allowed for JS callback

Parameters:

method (str)

Return type:

bool

has_slot(name: str = None) bool[source]

check if component has slot by name

Parameters:

name (str)

Return type:

bool

static(subdir: str, file_name: str) str[source]

get URL for static file

Parameters:
  • subdir (str)

  • file_name (str)

Return type:

str

class pantra.components.context.HTMLElement(tag_name: str, parent: RenderNode, attributes: dict[str, Any] | None = None, text: str = '', context: Context = None)[source]

HTML element node

Parameters:
name

name of this HTML tag

Type:

str

attributes

HTML node attributes

Type:

DynamicDict

classes

HTML classes

Type:

DynamicClasses

style

CSS style attributes

Type:

DynamicStyles

data

custom node data

Type:

DynamicDict

value_type

HTML “type” attribute

Type:

str

ref_name

reference name if specified

Type:

str

localize

localize time and date values, read for more

Type:

bool

text

text

Type:

str

get_caller(action: str) Callable | Awaitable | None[source]

get context caller by name

Parameters:

action (str)

Return type:

Callable | Awaitable | None

classmethod parse(element: str, parent: RenderNode, text: str = '') HTMLElement | None[source]

create HTMLElement from string

Example:

div = HTMLElement.parse('input type=text value="Hello"', parent)
Parameters:
Return type:

Optional[HTMLElement]

request_measures()[source]

get position and size of this element, rendered in user browser

Warning

Take in account getBoundingClientRect() repeated calls side effect.

property measures: MeasuresData

get cached position and size of this element, rendered in user browser

Measures is cached after the first request. To update call request_measures().

set_measures(m: MeasuresData | list[int | str], from_x: int = 0, from_y: int = 0, shift_x: int = 0, shift_y: int = 0, grow: int = 0)[source]

change this element position and size in user browser

Calling this method sets position: fixed style of element, and then changed metrics

Parameters:
  • m (Union[MeasuresData, list[Union[int, str]]]) – MeasuresData or sequential list of metrics

  • from_x (int) – new x position

  • from_y (int) – new y position

  • shift_x (int) – shift x position

  • shift_y (int) – shift y position

  • grow (int) – grow width and height by amount of units

reset_measures()[source]

reset measures cache

property value

request value of this element <input> from user browser

It cached once requested.

validate()[source]

check validity state of this element

move_box(delta_x, delta_y)[source]

shift element location by delta_x, delta_y

Designed for drag’n’drop

set_focused()[source]

mark this element as focused

This state reset after send.

add_class(class_name)[source]

shortcut for classes += class_name and update

remove_class(class_name)[source]

shortcut for classes -= class_name and update

toggle_class(class_name)[source]

shortcut for classes *= class_name and update

set_text(text: str)[source]

set text and update

Parameters:

text (str)

set_quietly(key, value)[source]

shortcut to set variable value suppressing reactions in local context

reset_loop_cache()[source]

reset LoopNode cache for all child nodes

class pantra.components.context.NSElement(tag_name: str, parent: RenderNode, attributes: dict[str, Any] | None = None, text: str = '', context: Context = None)[source]

Extension to HTMLElement with namespace support

Parameters:
ns_type

namespace type

Type:

NSType

class pantra.components.context.LoopNode(parent: RenderNode, template: HTMLTemplate | None)[source]

Node for loops {{#for …}}

Parameters:
reset_cache()[source]

reset index cache

class pantra.components.context.ConditionNode(parent: RenderNode, template: HTMLTemplate | None)[source]

Node for conditional {{#if …}}, {{#elif …}}, {{#else}}

Parameters:
class pantra.components.context.TextNode(parent: RenderNode, text: ValueOrCode)[source]

node for raw text content

intended to render sliced text blocks:

<div>
    Text one
    <p>...</p>
    {{get_content()}}
    <p>...</p>
    Text three
</div>
Parameters:
class pantra.components.context.EventNode(parent: RenderNode, selector: str | None = None, events: DynamicDict = None)[source]

Event node

Parameters:
class pantra.components.context.SetNode(parent: RenderNode, template: HTMLTemplate | Callable[[...], None] | None)[source]

Set node

Parameters:
class pantra.components.context.ReactNode(parent: RenderNode, var_name: str, action: str | Callable[[...], None])[source]

:ref`React node <reactive tag>`

Parameters:
class pantra.components.context.ScriptNode(parent: RenderNode, uid: str, attributes: dict[str, Any] = None, text: str = '', put_to_head: bool = True)[source]

JavaScript node

Parameters: