Helper types
ErrorMessage = TypeVar('ErrorMessage')
A string, callback validation error message.
TagValue = TypeVar('TagValue', bound=Any)
Any value. It is being wrapped by a Tag.
UiValue = TagValue | str
Candidate for the TagValue. Produced by the UI. Might be of the same type as the target TagValue, or str (as the default input type for interfaces that do not implement the given type further).
ValidationCallback = Callable[['Tag'], ValidationResult | tuple[ValidationResult, TagValue]] | BaseMetadata | GroupedMetadata
Being used at Tag.validation.
Either use a custom callback function, a provided validator, or an annotated-types predicate. (You can use multiple validation callbacks combined into an itarable.)
ValidationResult is a bool or the error message that implicitly means it has failed. Optionally, you may add a second argument to specify the tag value (to ex. recommend a better value).
Custom function
Handles the Tag.
from mininterface.tag import Tag
def my_validation(tag: Tag):
return tag.val > 50
m.form({"number", Tag("", validation=my_validation)})
Provided validators
Found in the mininterface.validators module.
from mininterface.validators import not_empty
m.form({"number", Tag("", validation=not_empty)})
# User cannot leave the field empty.
You may use the validation in a type annotation.
from mininterface import Tag, Validation
@dataclass
class Env:
my_text: Annotated[str, Validation(not_empty)] = "will not be emtpy"
# which is an alias for:
# my_text: Annotated[str, Tag(validation=not_empty)] = "will not be emtpy"
annotated-types predicate.
The annotated-types are de-facto standard for types restraining.
Currently, Gt, Ge, Lt, Le, MultipleOf and Len
are supported.
ValidationResult = bool | ErrorMessage
Being used at Tag.validation.
A bool or the error message (that implicitly means the ValidationCallback has failed) as shows the following table. Optionally, you may add a second argument to specify the tag value (to ex. recommend a better value).
return | description |
---|---|
bool | True if validation succeeded or False if validation failed. |
str | Error message if the validation failed. |
tuple[bool|str, TagVal] | The first argument is the same as above. The second is the value to be set. |
This example shows the str error message:
def check(tag: Tag):
if tag.val < 10:
return "The value must be at least 10"
m.form({"number", Tag(12, validation=check)})
This example shows the value transformation. For val=50+
, the validation fails.
OptionsType = list[TagValue] | tuple[TagValue, ...] | set[TagValue] | dict[RichOptionLabel, TagValue] | Iterable[Enum] | Type[Enum]
You can denote the options in many ways.
Either put options in an iterable or to a dict {labels: value}
.
Values might be Tags as well. Let's take a detailed look. We will use the run.select(OptionsType)
to illustrate the examples.
Iterables like list
Either put options in an iterable:
Dict for labels
Or to a dict {name: value}
. Then name are used as labels.
Dict with tuples for table
If you use tuple as the keys, they will be joined into a table.
Tags for labels
Alternatively, you may specify the names in Tags
.
Enums
Alternatively, you may use an Enum.
Alternatively, you may use an Enum instance. (Which means the default value is already selected.)
Alternatively, you may use an Enum instances list.
Further examples
See mininterface.select or SelectTag.options
for further usage.
DataClass = TypeVar('DataClass')
Any dataclass. Or a pydantic model or attrs.
EnvClass = TypeVar('EnvClass', bound=DataClass)
Any dataclass. Its instance will be available through Mininterface.env after CLI parsing. Its fields or whole class might be annotated with tyro conf flags.
The following example turns down boolean flag conversion.
from dataclasses import dataclass
from mininterface import run
from tyro.conf import FlagConversionOff
@dataclass
class Env:
my_bool: bool = False
m = run(FlagConversionOff[Env])
Whereas by default, both flags are generated:
FormDict = dict[Hashable, TypeVar('FormDictRecursiveValue', TagValue, Tag, 'Self')]
Nested dict that can have descriptions (through Tag) instead of plain values.
mininterface.interfaces.InterfaceType = Type[Mininterface] | InterfaceName | None
Either a class symbol or a shortcut string.