Classes¶
Thread workers¶
- class pantra.workers.base.BaseWorkerServer[source]¶
Abstract thread management and communication server class
It has basic thread manager implementation and abstract communication methods. Read Threading model and Message queue for more information
- class Listener[source]¶
Abstract communication listener class
- abstractmethod async send(session_id: str, data: bytes)[source]¶
Send packed message to client by session_id
- classmethod task_processor()[source]¶
Single task thread processor.
It extracts new function call with args from task_queue and let it run in this thread
- classmethod tasks_controller()[source]¶
Common task controller
It supervises all task threads life cycle. See also Threading model.
- class pantra.workers.base.BaseWorkerClient(session_id: str, ws: WebSocket, app: str, lang: list[str], params: dict[str, str])[source]¶
Abstract thread management and communication client class
It contains abstract communication methods.
Routers¶
- class pantra.routes.BaseRouter[source]¶
Basic router class.
It has all needed to startup and shutdown router, and also has websocket processor. All other derived classes are responsible for processing: * static files, including engine JS * any additional API calls
It is recommended to inherit from
DevRouterorCachedRouterinstead of base class- static forbidden(message: str) Response[source]¶
Shortcut method to return 403
- Parameters:
message (str)
- Return type:
Response
- static not_found(message: str) Response[source]¶
Shortcut method to return 404
- Parameters:
message (str)
- Return type:
Response
- class pantra.routes.DevRouter[source]¶
Router intended for development process.
It provides logic to use raw components, generate data on the fly, slow yet handy
- class pantra.routes.CachedRouter[source]¶
Router intended for production.
It uses pre-cached components data only. Fast and efficient.
- pantra.routes.route(pattern: str, method: str = None)[source]¶
The decorator to mark method as a router to specific regex pattern
Drag’n’drop action¶
- class pantra.components.controllers.DragOptions(mouse_button: int = 0, allow_move_by_x: bool = True, allow_move_by_y: bool = True, border_top: int | None = None, border_left: int | None = None, border_bottom: int | None = None, border_right: int | None = None)[source]¶
Initial drag’n’drop control options
- Parameters:
- mouse_button¶
code of mouse button to push for drag
- Type:
- class pantra.components.controllers.DragController(node: HTMLElement)[source]¶
Abstract class to perform drag’n’drop action
- Parameters:
node (HTMLElement)
- from_x¶
initial x coordinate
- from_y¶
initial y coordinate
- x¶
current x coordinate
- y¶
current y coordinate
- delta_x¶
changes of x coordinate since last time
- delta_y¶
changes of y coordinate since last time
- options¶
control options
- in_moving¶
box in moving process
- get_options(node: HTMLElement)[source]¶
get options for node
Override this method for custom options for specified node
- Parameters:
node (HTMLElement)
Common classes¶
- class pantra.common.HTML[source]¶
Mark string as safe HTML content.
Example:
refs["message"].set_text(HTML("<h1>Big one</h1>"))
- class pantra.common.DynamicDict(*args, _lazy_mode: bool = False, **kwargs)[source]¶
Dictionary with dynamic (computable) values.
It allows to set lambda functions for evaluation and repeated evaluations later:
x = 2 d = DynamicDict() d["f"] = lambda : x**2 print(d["f"]) # -> 4 x = 3 d.refresh() print(d["f"]) # -> 9
- Parameters:
_lazy_mode (bool) – don’t evaluate functions at the first assignment
- class pantra.common.DynamicClasses(*args, **kwargs)[source]¶
Extension to
DynamicDictto support HTML classes manipulation.It allows basic arithmetic operations intuitively:
c = DynamicClasses("colored bold animated") c['selected'] = lambda: True c += "hidden" c -= "bold" c *= "active" c *= "active" # switch class on and off again print(c) # -> "colored animated selected hidden"
- class pantra.common.DynamicStyles(style: dict | str | None = None)[source]¶
Extension to
DynamicDictto support styles parsingExample:
s = DynamicStyles('left: 0; top: 0; width: 100%; height: 100%') print(s['left']) # -> "0" print(s['width']) # -> "100%" s -= "width" s["position"] = lambda: "absolute" s += "width: 80%" print(s) # -> "left: 0; top: 0; height: 100%; width: 80%; position: absolute"
Session class¶
- class pantra.session.Session(session_id: str, app: str, lang: list[str], params: dict[str, str])[source]¶
Session class.
Session is created on new user connection and contains isolated user-related data.
- session_id¶
unique session id, generated on client side
- title¶
application title to the browser page (see
set_title())
- user¶
current user ID, if used
- locale¶
current locale (see
set_locale()and more)- Type:
Locale
- params¶
URL params (http://localhost/app/?a=1&b=2&c=3)
- last_touch¶
last time event was triggered on this session
- Type:
datetime
- pending_errors¶
(class variable) all pending errors queue, to send to next user on next session
- Type:
Queue[str]
- server_worker¶
(class variable) main server worker to host all sessions
- Type:
- get_node(oid: int) AnyNode | None[source]¶
shortcut to get node by OID (read here)
- Parameters:
oid (int)
- Return type:
AnyNode | None
- kill_all_tasks(ctx: UniqueNode = None)[source]¶
kill all tasks for specified context and all
- Parameters:
ctx (UniqueNode)
- call(method: str, *args)[source]¶
call JavaScript method by name and arguments
- Parameters:
method (str)
- zgettext(message: str, *args, plural: str = None, n: int = None, ctx: str = None, many: bool = False)[source]¶
unite translation function
- gettext(message: str) str[source]¶
simple translation function
- set_storage(storage_cls: type[SessionStorage] | SessionStorage)[source]¶
set session storage
- Parameters:
storage_cls (type[SessionStorage] | SessionStorage)
- bind_state(name: str, dict_ref: dict[str, Any], key: str = 'value')[source]¶
bind component state to session storage
- bind_states(bindings: dict[str, dict | tuple[dict, str]])[source]¶
bind several components states using short form of dict
- Parameters:
bindings (dict[str, dict | tuple[dict, str]]) – dict of dict references or tuples of dict references and keys
Example:
session.bind_states({ "api_token": refs['token'], "api_user_id": refs['user_id'], "other_value": (refs['other'], "property") })
- sync_storage()[source]¶
gather changes and save to the session storage