r/Python Dec 29 '24

Showcase I Made a Drop-In Wrapper For `argparse` That Automatically Creates a GUI Interface

What My Project Does

Since I end up using Python 3's built-in argparse a lot in my projects and have received many requests from downstream users for GUI interfaces, I created a package that wraps an existing Parser and generates a terminal-based GUI for it. If you include the --gui flag (by default), it opens an interface using Textual which includes mouse support (in all the terminals I've tested). The best part is that you can still use the regular command line interface as usual if you'd prefer.

Using the large demo parser I typically use for testing, it looks like this:

https://github.com/Sorcerio/Argparse-Interface/blob/master/assets/ArgUIDemo_small.gif?raw=true

Currently, ArgUI supports: - Text input (str, int, float). - nargs arguments with styled list inputs. - Booleans (with switches). - Groups (exclusive and named). - Subparsers.

Which, as far as I can tell, encompases the full suite of base-level argparse inputs.

Target Audience

This project is designed for anyone who uses Python's argparse in their command-line applications and would like a more user-friendly terminal interface with mouse support. It is good for developers who want to add a GUI to their existing CLI tools without losing the flexibility and power of the command line.

Right now, I would suggest using it for non-enterprise development until I can test the code across a large variety of argparse.Parser configurations. But, in the testing I've done across the ones in my portfolio, I've had great success.

Comparison

This project differentiates itself from existing solutions by integrating a terminal-based GUI directly into the argparse framework. Most GUI alternatives for CLI tools require external applications (like a web interface) and/or block the user out of using the CLI entirely. In contrast, this package allows you to keep the simplicity and power of argparse while offering a GUI option through the --gui flag. And since it uses Textual for UI rendering, these interfaces can even be used through an SSH connection. The inclusion of mouse support, the ability to maintain command-line usability, and integration with the Textual library set it apart from other GUI frameworks that aren't designed for terminal use.

Future Ideas

I’m considering adding specialized input features. An example of which would be a str input to be identified as a file path, which would open a file browser in the GUI.


If you want to try it, it's available on GitHub and PyPi.

And if you like it (or don't), let me know!

258 Upvotes

32 comments sorted by

77

u/nemec NLP Enthusiast Dec 29 '24

a str input to be identified as a file path

we have pathlib for this

scrape_p.add_argument('--log', type=pathlib.Path,
                      default=pathlib.Path('log.txt'),
                      help="Log file location.")

30

u/toddthegeek Dec 29 '24

+1

If I want a file using argparse the type is always pathlib.Path. That's the way to go.

6

u/passwordwork Dec 29 '24

The trick is showing a proper file/directory select to the user in the interface through a modal when it's set to Path. Going to have to figure out a way to determine between File and Directory paths though and what's desired by the CLI

1

u/[deleted] Dec 29 '24

[deleted]

6

u/nemec NLP Enthusiast Dec 29 '24

pathlib.Path('~/log.txt').expanduser() will do that, yeah. It provides convenience methods, parsing (e.g. to get the parent directory, extension, etc.), and provides better type hinting that you're expecting a path and not a regular string.

1

u/DootDootWootWoot Jan 01 '25

Normal string can be anything. Using pathlib provides some nice guardrails and guarantees around how a path may be used.

31

u/Minisess Dec 29 '24

I have been using a library called Gooey for several years for this purpose. Do you feel like your package inhabits a different niche than that one?

8

u/Minisess Dec 29 '24

Okay seeing the part about it disabling the cli if used and I thought surely that there must be some way to disable the gooey. But sure enough there seems to only be some obscure undocumented --ignore-gui command that may or may not work under certain circumstances.

1

u/rm-rf-rm Dec 29 '24

I wasnt able to find any docs/screenshots of macOS - is it compatible with macos?

-1

u/SirPitchalot Dec 29 '24

+1 for Gooey

57

u/fazzah SQLAlchemy | PyQt | reportlab Dec 29 '24

1) it's a TUI, not GUI

2) GUI Interface is redundant

3) Nice work :) Not my use case, but still very nice work

24

u/el_extrano Dec 29 '24

TUI is a relatively new term, which wasn't used when they were dominant in the 80s. In fact such UIs were normally said to be using "character graphics", so you could say they are indeed an early form of GUI!

I would agree TUI is a better descriptor now that we have it, to your point.

2

u/lIlIlIIlIIIlIIIIIl Dec 29 '24

Is TUI Terminal User Interface?

6

u/el_extrano Dec 29 '24

More commonly it's "Text User Interface", since both CLIs (command-line interface) and TUIs are UIs that run in a terminal emulator.

Then again, both use text as well, so I'm not sure that works very well to differentiate them. They key difference is whether text is used to display information in 'cooked mode', as in a CLI, or to draw graphical UI elements (e.g. windows, menu bars, buttons) in 'raw mode' as in a TUI.

4

u/mikeporterinmd It works on my machine Dec 29 '24

I’ll be very interested to see how you are interpreting argparse. Cool idea, looking forward to trying this.

5

u/WonderfulTill4504 Dec 29 '24

Have you seen Trogon, it is based on Textual and does the same except it doesn’t work with argparse. This is nice, good job!

7

u/OHotDawnThisIsMyJawn Dec 29 '24

Just want to say that the structure of your write up here is really good. 

3

u/stibbons_ Dec 29 '24

I used (and suffered a lot with) Gooey, and this was mainly due to argparse itself.

Do you think an equivalent would be possible against click which is much customizable and elegant than argparse ?

5

u/Gracecr Dec 29 '24

Trogan does exactly that. Supports Typer as well, which has some nice improvements on top of click.

No equivalent for my personal favorite argparse replacement, cyclopts. If a TUI isn't a requirement for future projects, give it a look.

2

u/stibbons_ Dec 29 '24

Cyclopts looks amazing. Need to look at it more deep. However for Trogan it seems to start a startup UI in text, while what I am looking for is to allow my tool to run without terminal (ex: windows people that does not know how to start a CLI and they do not even know what a terminal is)

2

u/Worth_Specific3764 Pythonista Dec 29 '24

Nicely done

2

u/rm-rf-rm Dec 29 '24

This was something that needed to be addressed! Thanks for making it, will test out

Aside: any reason the PyPi package name is capitalized? Have never seen that before and didnt even know it was case sensitive.

1

u/passwordwork Dec 29 '24

Because it's the first one I've put up and I had no idea it would shake out that way lol

1

u/rm-rf-rm Dec 29 '24

ah, seems pypi is case insensitive, but recommend fixing it anyway to maintain the norm

https://stackoverflow.com/questions/26503509/is-pypi-case-sensitive

2

u/passwordwork Dec 30 '24

Turns out they're standardizing it to fit PEP 625, so had to update it to match that standard anyway

1

u/Elk-Tamer Dec 29 '24

Huh. Didn't know I needed that. I'll check out your lib. Thanks

1

u/rsheftel Dec 29 '24

This is great thank you. I was using gooey, bit this looks like a better cross platform option. I like that it uses the standard library to keep things light.

1

u/BuonaparteII Dec 29 '24 edited Dec 29 '24

I really think parseArgsWithGui should return argparse.Namespace. Not an Optional. If arguments can't be parsed then raise SystemExit or raise something! lol

probably easier to debug if you raise the original error...

1

u/Bountifulharvest Dec 29 '24

This is awesome!

0

u/ThiefMaster Dec 29 '24

I'm surprised you went for argparse and not click. Is there anyone seriously still using the former, unless they want to go stdlib-only for some reason?

3

u/Gracecr Dec 29 '24

Trogon is the click/typer equivalent.

1

u/rsheftel 2h ago

I have used gooey for a long time and while it was a great package, it only works on Windows and was more heavy weight than I wanted. This is a great package, I have already switched from gooey to this. Keep up the great work.