Supported types
Various types are supported:
- scalars
- functions
- enums
- well-known objects (
Path
,datetime
) - iterables (like
list[Path]
) - custom classes (somewhat)
- union types (like
int | None
)
Basic usage
Usage in a dataclass
Take a look how it works with the variables organized in a dataclass:
from dataclasses import dataclass
from pathlib import Path
from mininterface import run
@dataclass
class Env:
my_number: int = 1
""" A dummy number """
my_boolean: bool = True
""" A dummy boolean """
my_conditional_number: int | None = None
""" A number that can be null if left empty """
my_path: Path = Path("/tmp")
""" A dummy path """
m = run(Env) # m.env contains an Env instance
m.form() # Prompt a dialog; m.form() without parameter edits m.env
print(m.env)
# Env(my_number=1, my_boolean=True, my_path=PosixPath('/tmp'),
# my_point=<__main__.Point object at 0x7ecb5427fdd0>)
Usage in a dict
Variables organized in a dict:
Along scalar types, there is (basic) support for common iterables or custom classes.
from mininterface import run
class Point:
def __init__(self, i: int):
self.i = i
def __str__(self):
return str(self.i)
values = {"my_number": 1,
"my_list": [1, 2, 3],
"my_point": Point(10)
}
m = run()
m.form(values) # Prompt a dialog
print(values) # {'my_number': 2, 'my_list': [2, 3], 'my_point': <__main__.Point object...>}
print(values["my_point"].i) # 100
Examples
All the examples need some imports:
Scalars
@dataclass
class Env:
my_file: str
""" This is my help text """
my_flag: bool = False
""" My checkbox """
my_number: float = 1.1
""" Any scalar possible """
run(Env).form()
Functions
Will appear as buttons.
def my_callback():
print("I'm here!")
@dataclass
class Env:
my_file: Callable = my_callback
run(Env).form()
Or use the with
statement to redirect the stdout into the mininterface.
Warning
When used in a form like this m.form({'My callback': my_callback)
, the value is left intact. It still points to the function. This behaviour might reconsidered and changed. (It might make more sense to change it to the return value instead.)
Enums
To constraint a value, either pass an enum object or use handy additional type SelectTag that might speed you up a bit.
Well-known objects
We've added extra functions for known objects like Path
or datetime
(file exists check etc.), see Tag
subclasses in Custom types section (PathTag, DatetimeTag, ...).
Iterables
from pathlib import Path
@dataclass
class Env:
my_file: list[int] = field(default_factory=lambda: [1, 2, 3])
my_paths: list[Path] = field(default_factory=lambda: [])
run(Env).form()
Union types
An enormously useful feature is to let the user not set a variable.
Additional
We've added some other useful custom types that can be imported mostly from mininterface.tag
.
- Tag subclasses – The descendants of the
Tag
, the object for storing values. Normally, you don't need to use or know much about those but they can be helpful when you need to further specify the functionality, such as restricting aPath
to directories only (PathTag). - Tag aliases – Userful shortcuts.
- Prepared annotations – Useful types to be used for nifty CLI parsing.