Skip to content

Run

mininterface.run(env_or_list=None, ask_on_empty_cli=False, title='', config_file=True, *, add_help=True, add_verbose=True, add_version=None, add_version_package=None, add_quiet=False, ask_for_missing=True, interface=None, args=None, settings=None, **kwargs)

The main access, start here. Wrap your configuration dataclass into run to access the interface. An interface is chosen automatically, with the preference of the graphical one, regressed to a text interface for machines without display. Besides, if given a configuration dataclass, the function enriches it with the CLI commands and possibly with the default from a config file if such exists. It searches the config file in the current working directory, with the program name ending on .yaml, ex: program.py will fetch ./program.yaml.

Parameters:

Name Type Description Default
env_or_list Type[EnvClass] | list[Type[EnvClass]] | ArgumentParser | None
  • dataclass Dataclass with the configuration. Their values will be modified with the CLI arguments.
  • list of dataclasses let you create multiple commands within a single program, each with unique options. You may use Command descendants to be automatically run.
  • argparse.ArgumentParser Not as powerful as the dataclass but should you need to try out whether to use the Mininterface instead of the old argparse, this is the way to go.
  • None You need just the dialogs, no CLI/config file parsing.
None
ask_on_empty_cli bool

If program was launched with no arguments (empty CLI), invokes self.form() to edit the fields. (Withdrawn when ask_for_missing happens.)

@dataclass
class Env:
    number: int = 3
    text: str = ""
    m = run(Env, ask_on_empty=True)

$ program.py  #  omitting all parameters
# Dialog for `number` and `text` appears
$ program.py --number 3
# No dialog appears
False
title str

The main title. If not set, taken from prog or program name.

''
config_file Path | str | bool

File to load YAML to be merged with the configuration. You do not have to re-define all the settings in the config file, you can choose a few. If set to True (default), we try to find one in the current working dir, whose name stem is the same as the program's. Ex: program.py will search for program.yaml. If False, no config file is used. See the Config file section.

True
add_help bool

Adds the help flag.

True
add_verbose bool | int | Sequence[int]

The default base Python verbosity logging level is logging.WARNING. Here you can add the verbose flag that automatically increases the level to logging.INFO (-v) or logging.DEBUG (-vv). Either, the value is True (the default) which means the base logging level stays at logging.WARNING and the flag is added. False means no flag is added. Also, it can be int to determine the default logging state (i.g. some programs prefer to show INFO by default) or a sequnce of ints for even finer control.

The add_vebose=True example:

import logging
from mininterface import run
logger = logging.getLogger(__name__)

m = run(add_verbose=True)
logger.info("Info shown") # needs `-v` or `--verbose`
logger.debug("Debug shown")  # needs `-vv`
$ program.py
# no output

$ program.py --verbose
Info shown

$ program.py -vv
Info shown
Debug shown

Apart from True, it can also be an int, claiming the base logging level. By default, in Python this is logging.WARNING. Here, we change it to logging.INFO.

m = run(add_verbose=logging.INFO)
$ program.py
Info shown

$ program.py -v
Info shown
Debug shown

Finally, it can be a sequence of ints, first of them is the base logging level, the others being successing levels.

m = run(add_verbose=(logging.WARNING, 25, logging.INFO, 15, logging.DEBUG))
logger.warning("Warning shown") # default
logger.log(25, "Subwarning shown") # needs `-v`
logger.info("INFO shown")  # needs `-vv`
logger.log(15, "Subinfo shown") # needs `-vvv`

When user writes more -v than defined, the level sets to logging.NOTSET.

True
add_version Optional[str]

Your program version. Adds the version flag.

run(add_version="1.2.5")

$ program.py --help
usage: _debug.py [-h] [--version]

╭─ options ───────────────────────────────────────────────────────────╮
 -h, --help           show this help message and exit                 --version            show program's version number (1.2.5) and exit ╰─────────────────────────────────────────────────────────────────────╯
None
add_quiet bool

Decrease verbosity, only print warnings and errors.

import logging
logger = logging.getLogger(__name__)

m = run(add_quiet=True)
logger.error("Error shown") # needs `-v` or `--verbose`
logger.warning("Warning shown") # strip with `-q` or `--quiet`
logger.info("Info shown")

$ program.py
Error shown
Warning shown

$ program.py --quiet
Error shown
False
ask_for_missing bool

If some required fields are missing at startup, we ask for them in a UI instead of program exit.

@dataclass
class Env:
    required_number: int
m = run(Env, ask_for_missing=True)
$ program.py  # omitting --required-number
# Dialog for `required_number` appears
True
interface Type[Mininterface] | Literal['gui'] | Literal['tui'] | Literal['text'] | Literal['web'] | None

Which interface to prefer. By default, we use the GUI, the fallback is the TUI. You may write "gui" or "tui" literal or pass a specific Mininterface type, see the full list of possible interfaces. If not set, we look also for an environment variable MININTERFACE_INTERFACE and in the config file.

None
args Optional[Sequence[str]]

Parse arguments from a sequence instead of the command line.

None
settings Optional[MininterfaceSettings]

Default settings. These might be further modified by the 'mininterface' section in the config file.

None

Kwargs: The same as for argparse.ArgumentParser.

Returns:

Type Description
Mininterface[EnvClass]

An interface, ready to be used.

You cay context manager the function by a with statement. The stdout will be redirected to the interface (ex. a GUI window).

from dataclasses import dataclass
from mininterface import run

@dataclass
class Env:
    my_number: int = 4

with run(Env) as m:
    print(f"Your important number is {m.env.my_number}")
    boolean = m.confirm("Is that alright?")

Small window with the text 'Your important number' The same in terminal'