front.elements.elements#
Abstract base classes for the composite tree of front UI elements.
Front elements (containers, inputs, outputs, etc.) are arranged in a tree
mirroring the rendering specification. This module defines the base
FrontElementBase and FrontContainerBase classes that concrete frontends
(e.g. streamlit) subclass to provide their own rendering.
Functions
|
Instantiate the element factory found under |
|
Make one input element spec per parameter of |
Classes
|
Boolean input (checkbox-like); values are cast with |
|
Container that executes |
|
File upload input, restricted to the given file |
|
Float input with optional bounds and |
|
A leaf element the user interacts with (an input, an output, a text section). |
|
An element with children, each built from a keyword argument holding an element spec. |
|
Base of every front element: a renderable node of the element tree. |
|
Base of input components: a value bound to state under |
|
Integer input with optional bounds; values are cast with |
|
Input for a |
|
An input whose value can come from several child input components. |
|
Base of numeric inputs, with an optional display |
|
Base of output components; |
|
Input choosing one value among |
|
Text input; the view value defaults to the empty string. |
|
A component displaying text |
- class front.elements.elements.BooleanInputBase(obj=None, name=None, display=True, input_key=None, value=ValueNotSet, on_value_change=None, bound_data_factory=None, is_noneable=False, disabled=False)[source]#
Bases:
InputBaseBoolean input (checkbox-like); values are cast with
bool, defaulting to False.
- class front.elements.elements.ExecContainerBase(obj, inputs, output, name=None, display=True, auto_submit=False, on_submit=None)[source]#
Bases:
FrontContainerBaseContainer that executes
objwith the values of its input children.Builds one input child per parameter of
obj(seemk_input_element_specs) plus anoutputchild._submitcallsobjwith the collected inputs, hands the result to the firstOutputBasechild and renders it, then callson_submitwith the result if given. Concrete subclasses implementrenderand_noneable(how an optional input is presented).
- class front.elements.elements.FileUploaderBase(obj=None, name=None, display=True, input_key=None, value=ValueNotSet, on_value_change=None, bound_data_factory=None, is_noneable=False, disabled=False, type=None, accept_multiple_files=False, clear_after_submit=True)[source]#
Bases:
InputBaseFile upload input, restricted to the given file
type(extension(s)) if any.With
clear_after_submit(the default), a front implementation should empty the uploader once its container has successfully submitted, so the next upload starts from a clean selection instead of the previous one sticking. Set it to False to keep the selection (e.g. to resubmit the same files with other parameters).
- class front.elements.elements.FloatInputBase(obj=None, name=None, display=True, input_key=None, value=ValueNotSet, on_value_change=None, bound_data_factory=None, is_noneable=False, disabled=False, format=None, min_value=None, max_value=None, step=None)[source]#
Bases:
NumberInputBaseFloat input with optional bounds and
step; values are cast withfloat, defaulting to 0.0.
- class front.elements.elements.FrontComponentBase(obj=None, name=None, display=True)[source]#
Bases:
FrontElementBaseA leaf element the user interacts with (an input, an output, a text section).
- class front.elements.elements.FrontContainerBase(obj=None, name=None, display=True, **kwargs)[source]#
Bases:
FrontElementBaseAn element with children, each built from a keyword argument holding an element spec.
Every extra keyword argument is a child spec: the key becomes the child’s
name(unless the spec overrides it) and the container’sobjis passed down. Concrete containers define the layout inrender.>>> from dataclasses import dataclass >>> @dataclass ... class Hello(FrontElementBase): ... def render(self): ... return f"hello {self.name}" >>> class Box(FrontContainerBase): ... def render(self): ... return [child() for child in self.children] >>> box = Box(name='box', greeting={ELEMENT_KEY: Hello, 'name': 'you'}, other={ELEMENT_KEY: Hello}) >>> box() ['hello you', 'hello other']
- class front.elements.elements.FrontElementBase(obj=None, name=None, display=True)[source]#
Bases:
ABCBase of every front element: a renderable node of the element tree.
Subclasses implement
render. Calling the element runs the lifecyclepre_render→render→post_renderwhendisplayis truthy, and does nothing (returns None) otherwise.nameanddisplaymay be given as callables ofobj; they are resolved at construction.>>> from dataclasses import dataclass >>> @dataclass ... class Hello(FrontElementBase): ... def render(self): ... return f"hello {self.name}" >>> Hello(name='world')() 'hello world' >>> Hello(obj=len, name=lambda obj: obj.__name__)() 'hello len' >>> Hello(name='hidden', display=False)() is None True
See also
FrontContainerBase: an element with children.FrontComponentBase: a leaf element the user interacts with.
- class front.elements.elements.InputBase(obj=None, name=None, display=True, input_key=None, value=ValueNotSet, on_value_change=None, bound_data_factory=None, is_noneable=False, disabled=False)[source]#
Bases:
FrontComponentBaseBase of input components: a value bound to state under
input_key.objis theinspect.Parameterthe input feeds. At construction,valueis wrapped in aBoundDatamade bybound_data_factory(unless it already is one), and seeded with the given value or the parameter’s default if nothing is set yet. Two companion keys,view_keyandnone_key, hold the widget’s displayed value and its “is None” toggle.- Raises:
ValueError – If
bound_data_factoryis None when aBoundDatais needed.
- class front.elements.elements.IntInputBase(obj=None, name=None, display=True, input_key=None, value=ValueNotSet, on_value_change=None, bound_data_factory=None, is_noneable=False, disabled=False, format=None, min_value=None, max_value=None)[source]#
Bases:
NumberInputBaseInteger input with optional bounds; values are cast with
int, defaulting to 0.
- class front.elements.elements.KwargsInputBase(obj=None, name=None, display=True, input_key=None, value=ValueNotSet, on_value_change=None, bound_data_factory=None, is_noneable=False, disabled=False, inputs=None, func_sig=None)[source]#
Bases:
InputBaseInput for a
**kwargsparameter, with one sub-input per name infunc_sig.
- class front.elements.elements.MultiSourceInputBase(obj=None, name=None, display=True, input_key=None, value=ValueNotSet, on_value_change=None, bound_data_factory=None, is_noneable=False, disabled=False, **kwargs)[source]#
Bases:
InputBaseAn input whose value can come from several child input components.
Extra keyword arguments are child input specs; each child shares this input’s
input_key,valueand binding settings.
- class front.elements.elements.NumberInputBase(obj=None, name=None, display=True, input_key=None, value=ValueNotSet, on_value_change=None, bound_data_factory=None, is_noneable=False, disabled=False, format=None)[source]#
Bases:
InputBaseBase of numeric inputs, with an optional display
format.
- class front.elements.elements.OutputBase(obj=None, name=None, display=True)[source]#
Bases:
FrontComponentBaseBase of output components;
outputis set by the executing container before render.
- class front.elements.elements.SelectorBase(obj=None, name=None, display=True, input_key=None, value=ValueNotSet, on_value_change=None, bound_data_factory=None, is_noneable=False, disabled=False, options=None)[source]#
Bases:
InputBaseInput choosing one value among
options(a sequence, a mapping – its keys –, or a callable returning one).If no options are given and the parameter is annotated with a
Literal, the literal’s values are the options.
- class front.elements.elements.TextInputBase(obj=None, name=None, display=True, input_key=None, value=ValueNotSet, on_value_change=None, bound_data_factory=None, is_noneable=False, disabled=False, type=None)[source]#
Bases:
InputBaseText input; the view value defaults to the empty string.
- class front.elements.elements.TextSectionBase(content, kind='text', obj=None, name=None, display=True, **kwargs)[source]#
Bases:
FrontComponentBaseA component displaying text
contentof a givenkind(e.g. “text”, “markdown”).contentandkindmay be callables ofobj, resolved at construction. Extra keyword arguments are kept inself.kwargsfor the concrete renderer.
- front.elements.elements.mk_element_from_spec(spec)[source]#
Instantiate the element factory found under
ELEMENT_KEYwith the other keys.>>> from dataclasses import dataclass >>> @dataclass ... class Hello(FrontElementBase): ... def render(self): ... return f"hello {self.name}" >>> mk_element_from_spec({ELEMENT_KEY: Hello, 'name': 'x'})() 'hello x'
- Raises:
RuntimeError – If
spechas noELEMENT_KEY.
- front.elements.elements.mk_input_element_specs(obj, inputs)[source]#
Make one input element spec per parameter of
obj, from a type-keyedinputsspec.inputsmaps parameter types (and/or parameter names) to element specs; theDEFAULT_INPUT_KEYentry is merged under every type entry. Each parameter’s spec is looked up by its annotation (or the type of its default), with anOptional[X]annotation or aNonedefault marking it asis_noneable. Unions of more than one non-None type are not supported.>>> def foo(a: int, b='hi', c: float = None): ... pass >>> specs = mk_input_element_specs( ... foo, {DEFAULT_INPUT_KEY: {'disabled': False}, int: {'min_value': 0}} ... ) >>> list(specs) ['a', 'b', 'c'] >>> {k: v for k, v in specs['a'].items() if k != 'obj'} {'disabled': False, 'min_value': 0, 'input_key': 'foo_a', 'is_noneable': False} >>> specs['c']['is_noneable'] True
- Raises:
NotImplementedError – If a parameter is annotated with a Union of several non-None types.