Module

app

App event loop and terminal rendering.

Classes

_TerminalRenderer 5
In-place terminal renderer using alternate screen buffer. Uses cursor-home redraws with line clear…

In-place terminal renderer using alternate screen buffer.

Uses cursor-home redraws with line clearing to avoid flicker and prevent frame stacking. Supports declarative ViewState for controlling terminal features (alt screen, cursor, mouse, title).

Methods

start 0
Enter alternate screen buffer and hide cursor.
def start(self) -> None
apply_view_state 1
Diff *view* against current state and apply only the changes.
def apply_view_state(self, view: ViewState) -> None
Parameters
Name Type Description
view
update 1
Redraw the screen with new output.
def update(self, output: str) -> None
Parameters
Name Type Description
output
stop 0
Show cursor and leave alternate screen buffer. Each step is individually guard…
def stop(self) -> None

Show cursor and leave alternate screen buffer.

Each step is individually guarded so a failure in one does not prevent the remaining cleanup from running.

Internal Methods 1
__init__ 0
def __init__(self) -> None
App 10
Main application event loop. Integrates the Store, KeyReader, and kida LiveRenderer into a unified…

Main application event loop.

Integrates the Store, KeyReader, and kida LiveRenderer into a unified event loop.

Methods

from_dir 7 App
Create an App that auto-discovers templates relative to *caller_file*. Looks f…
classmethod
def from_dir(cls, caller_file: str, *, template: str | Any = '', reducer: Callable | None = None, initial_state: Any = None, flow: Flow | None = None, templates_dir: str = 'templates', **kwargs: Any) -> App

Create an App that auto-discovers templates relative to caller_file.

Looks for atemplates/directory next to the given file path and creates a kida environment with that directory as the loader root. Pass__file__from the calling module.

Usage::

app = App.from_dir(
    __file__,
    template="counter.kida",
    reducer=reducer,
    initial_state=State(),
)
app.run()
Parameters
Name Type Description
caller_file
template Default:''
reducer Default:None
initial_state Default:None
flow Default:None
templates_dir Default:'templates'
**kwargs
Returns
App
from_flow 2 App
Create App from a declarative Flow.
classmethod
def from_flow(cls, flow: Flow, **kwargs: Any) -> App
Parameters
Name Type Description
flow
**kwargs
Returns
App
render 3 str
One-shot render of a template with state. Returns the rendered string.
classmethod
def render(cls, template: str, state: Any = None, *, env: Any = None) -> str
Parameters
Name Type Description
template
state Default:None
env Default:None
Returns
str
run 0 Any
Run the event loop. Returns final state.
def run(self) -> Any
Returns
Any
Internal Methods 6
__init__ 12
def __init__(self, *, template: str | Any = '', reducer: Callable | None = None, initial_state: Any = None, middleware: tuple[Callable, ...] = (), tick_rate: float = 0.0, transient: bool = False, target: RenderTarget = RenderTarget.TERMINAL, record: bool | str | Path = False, env: Any = None, flow: Flow | None = None, exit_template: str = '', msg_filter: Callable | None = None) -> None
Parameters
Name Type Description
template Default:''
reducer Default:None
initial_state Default:None
middleware Default:()
tick_rate Default:0.0
transient Default:False
target Default:RenderTarget.TERMINAL
record Default:False
env Default:None
flow Default:None
exit_template Default:''
msg_filter Default:None
_get_env 0 Any
Get or create the kida Environment.
def _get_env(self) -> Any
Returns
Any
_get_template_name 1 str
Get the template name for the current state.
def _get_template_name(self, state: Any) -> str
Parameters
Name Type Description
state
Returns
str
_render_state 3
Render current state through the template.
def _render_state(self, state: Any, env: Any, renderer: _TerminalRenderer) -> None
Parameters
Name Type Description
state
env
renderer
_render_exit 2
Render the exit template once to stdout.
def _render_exit(self, state: Any, env: Any) -> None
Parameters
Name Type Description
state
env
_render_once 1
Single render pass (non-TTY or HTML mode).
def _render_once(self, state: Any) -> None
Parameters
Name Type Description
state

Functions

run 4 Any
Shorthand: App(...).run()
def run(*, template: str, reducer: Callable, initial_state: Any, **kwargs: Any) -> Any
Parameters
Name Type Description
template str
reducer Callable
initial_state Any
**kwargs Any
Returns
Any
render_html 5 str
One-shot HTML render of state through template.
def render_html(state: Any, template: str | Any, *, title: str = '', css: str = '', env: Any = None) -> str
Parameters
Name Type Description
state Any
template str | Any
title str Default:''
css str Default:''
env Any Default:None
Returns
str