Single line of code – and you get GUI, TUI, CLI and config file parsing. It is frequently said Python comes with batteries included. But you may not yet heard about this battery cell.
https://github.com/CZ-NIC/mininterface
Background
I have a lot of ideas. I’ve always been frustrated that even though something is simple to implement, I spend a lot of time designing the UI. When I want to use a utility on a remote computer, I need an interactive TUI. When using it locally, I want a GUI (since GUI is the only option for non-skilled users). But for advanced users, I needed a CLI so they could automate scripts.
Years and years passed, and I kept researching whether something like this already existed. In May, I did a big study and compared 30 current libraries. No match. Finally, I've created the perfect program, which I’ve been using for all my projects for the past six months. It can save a lot of work for others as well.
Hello world
The thing is, you use a dataclass(es) to hold the program configuration. This is the only condition you have. Mininterface takes care of the rest. Forget about argparse.
Take a look to this dataclass. You wrap it to the run(Env)
and that's all.
from dataclasses import dataclass
from mininterface import run
@dataclass
class Env:
my_str: str = "Hello"
my_int: int = 10
m = run(Env)
m.form()
print(m.env.my_int) # 10
When you invoke the m.form()
method, a dialog is raised. In a graphical environment, it looks like this:
When there is no display available, we stick to the textual interface. Still fully interactive (you can even use the mouse).
Every attribute is available through command line too.
And finally, if a file named program.yaml
exists, all the values pick their default from there. Simple, isn't it?
Much more
Since my initial impulse to write this library was frustration, you may find a lot of details that will eliminate your frustration as well.
Nested configuration, choice handling (smooth!), validation, other useful dialog templates, image displaying (yes, even in the terminal)... In the past weeks, we've added a great calendar for date/time selection. Not just some corporate-style calendar that looks nice but is frustrating to use. This one is designed for real users. The arrow keys behave intelligently, and everything can be controlled entirely via the keyboard.
Take a look to the documentation. I know from experience that if a code snippet doesn't work immediately, if a code snippet is but a stub, I tend to discard the whole project. That’s why I make sure that all code examples are self-contained and always include a screenshot, so it’s clear what each part of the program does.
Thoughts
The best part? You don’t have to keep reinventing the interface on your own.
Annoyed by how images are displayed? Improve it once, and it will enhance not just your programs but also every project that uses mininterface. We can join forces.
Even though I strive to keep the interface stable and highly intuitive, I’m open to changes and new features. What do you think, what's missing? What should look better? What's the biggest pain? What would persuade you to give it a chance?
Top comments (0)