Mininterface
The base interface.
You get one through mininterface.run
which fills CLI arguments and config file to mininterface.env
or you can create one directly (without benefiting from the CLI parsing).
Raise
Cancelled: A SystemExit based exception noting that the program exits without a traceback, ex. if user hits the escape.
Raise
InterfaceNotAvailable: Interface failed to init, ex. display not available in GUI.
env: EnvClass = EnvInstance
facet = Facet(None, self.env)
__enter__()
Usage within the with statement makes the program to attempt for the following benefits:
Continual window
Do not vanish between dialogs (the GUI window stays the same)
Stdout redirection
Redirects the stdout to a text area instead of a terminal.
Make the session interactive
If run from an interactive terminal or if a GUI is used, nothing special happens.
However, when run in a non-interactive session with TUI (ex. no display), TextInterface is used which is able to turn it into an interactive one.
piped_in = int(sys.stdin.read())
with run(interface="tui") as m:
result = m.ask_number("What number") + piped_in
print(result)
If the with
statement is not used, the result is the same as if an interactive session is not available, like in a cron job.
In that case, plain Mininterface is used.
alert(text)
Prompt the user to confirm the text.
ask(text)
Prompt the user to input a text.
ask_number(text)
choice(choices, title='', _guesses=None, skippable=True, launch=True, _multiple=True, default=None)
Prompt the user to select. Useful for a menu creation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
choices |
ChoicesType
|
You can denote the choices in many ways. Either put options in an iterable: Or to a dict Alternatively, you may specify the names in Alternatively, you may use an Enum. Alternatively, you may use an Enum instance. Alternatively, you may use an Enum instances list. |
required |
title |
str
|
Form title |
''
|
default |
str | TagValue | None
|
None
|
|
skippable |
bool
|
If there is a single option, choose it directly, without a dialog. |
True
|
launch |
bool
|
If the chosen value is a callback, we directly call it and return its return value. |
True
|
Returns:
Type | Description |
---|---|
TagValue | list[TagValue] | Any
|
The chosen value. |
TagValue | list[TagValue] | Any
|
If launch=True and the chosen value is a callback, we call it and return its result. |
Info
To tackle a more detailed form, see Tag.choices
.
form(form=None, title='', *, submit=True)
Prompt the user to fill up an arbitrary form.
Use scalars, enums, enum instances, objects like datetime, Paths or their list.
from enum import Enum
from mininterface import run, Tag
class Color(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
m = run()
out = m.form({
"my_number": 1,
"my_boolean": True,
"my_enum": Color,
"my_tagged": Tag("", name='Tagged value', description='Long hint'),
"my_path": Path("/tmp"),
"my_paths": [Path("/tmp")],
"My enum with default": Color.BLUE
})
Parameters:
Name | Type | Description | Default |
---|---|---|---|
form |
DataClass | Type[DataClass] | FormDict | None
|
We accept a dataclass type, a dataclass instance, a dict or None.
A checkbox example:
|
None
|
title |
str
|
Optional form title |
''
|
submit |
str | bool
|
Set the submit button text (by default 'Ok') or hide it with False. |
True
|
Returns:
Name | Type | Description |
---|---|---|
dataclass |
FormDict | DataClass | EnvClass
|
If the |
dataclass |
FormDict | DataClass | EnvClass
|
If the |
dict |
FormDict | DataClass | EnvClass
|
If the Whereas the original dict stays intact (with the values updated),
we return a new raw dict with all values resolved
(all
Why this behaviour? You need to do some validation, hence you put
In the case you are willing to re-use the dict, you need not to lose the definitions,
hence you end up with accessing via the |
is_yes(text)
is_no(text)
Display confirm box, focusing no.