Mininterface
mininterface.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. You don't have to check for it when invoking an interface through safe methods run
or get_interface
.
facet: Facet
env: EnvClass | SimpleNamespace = EnvInstance
Parsed arguments from the EnvClass. Contains whole configuration (previously fetched from CLI and config file).
__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("What number", int) + 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, annotation=str, validation=None)
Prompt the user to input a value – text, number, ...
By default, it resembles the input()
built-in.
You may specify the type:
The type might be complex. This enforces a list of paths.
from mininterface import run
from pathlib import Path
m = run()
m.ask("Enlist input files", list[Path])
Also, you can further specify the value with a Tag. This enforces an existing file:
from mininterface import run
from mininterface.tag import PathTag
m = run()
m.ask("Give me input file", PathTag(is_file=True))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The question text. |
required |
annotation |
Type[TagValue] | Tag[TagValue]
|
str
|
|
validation |
Iterable[ValidationCallback] | ValidationCallback | None
|
None
|
Returns:
Type | Description |
---|---|
TagValue
|
The type from the |
confirm(text, default=True)
Display confirm box and returns bool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Displayed text. |
required |
default |
bool
|
Focus the button with this value. |
True
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
Whether the user has chosen the Yes button. |
select(options, title='', default=None, tips=None, multiple=None, skippable=True, launch=True)
Prompt the user to select. Useful for a menu creation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
options |
OptionsType[TagValue]
|
You can denote the options in many ways. Either put options in an iterable, or to a dict with keys as labels.
You can also use tuples for keys to get a table-like formatting. Use the Enums or nested Tags...
See the |
required |
title |
str
|
Form title |
''
|
default |
TagValue | OptionsType[TagValue] | None
|
None
|
|
tips |
OptionsType[TagValue] | None
|
Options to be highlighted. Use the list of choice values to denote which one the user might prefer. |
None
|
multiple |
Optional[bool]
|
If True, the user can choose multiple values and we return a list. |
None
|
skippable |
bool
|
If there is a single option, choose it directly, without a dialog. |
True
|
launch |
bool
|
True
|
Returns:
Name | Type | Description |
---|---|---|
TagValue |
TagValue | list[TagValue] | Any
|
The chosen value. |
list |
TagValue | list[TagValue] | Any
|
If multiple=True, return chosen values. |
Any |
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 SelectTag.options
.
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: The original dict values are updated and a new dict (where all Tags are resolved to their values) is returned.
|
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 |
Info
For minimal installation (pip install mininterface
only), using Type[DataClass]
like m.form(Env)
will end up with Install the missing dependency by running: pip install mininterface[basic]
.
However using a dataclass instance m.form(Env())
will work. A lot of the stuff under the hood is needed to instantaniate a dataclass with all the checks.