open-source-ideas / ideas

💡 Looking for inspiration for your next open source project? Or perhaps you've got a brilliant idea you can't wait to share with others? Open Source Ideas is a community built specifically for this! 👋
6.55k stars 223 forks source link

GUI implementation of the Python library Typer for CLI apps. #293

Open spookylukey opened 2 years ago

spookylukey commented 2 years ago

Project description

A library that auto-generates a simple GUI interface for simple scripts and programs, based on Python type annotations.

Explanation

Typer allows you to use Python type annotations to make it easy to create command line interface applications, complete with nice auto-generated documentation. For example:

import typer

def main(name: str):
    """
    Says hello to you
    """
    typer.echo(f"Hello {name}")

if __name__ == "__main__":
    typer.run(main)

You can then do this, for example:

$ python main.py --help
Usage: main.py [OPTIONS] NAME

    Says hello to you

Arguments:
  NAME  [required]

Options:
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it or customize the installation.
  --help                Show this message and exit.

Now, I love CLIs, but some people prefer GUIs (e.g. my wife), and there are times when she would like to use programs I've written (e.g. for managing family finances). It would be awesome if I could easily convert a script or program written like this into a simple, easy-to-use GUI that:

To do this, you could create a full implementation of the typer API, but it would create a GUI instead. So, for example, for the above script you would get:

Screenshot from 2021-09-15 11-20-13

For more complex options, you'd have more complex widget types. For example, if the program defined an argument as an enum, you would present this using a dropdown list to choose from. If an argument was a Path, this would probably be a file/directory picker widget etc. If the type annotations specified a list of files, the UI would have a + button that would allow another instance of the widget to appear, to select multiple files.

There are a range of cross-platform GUI libraries for Python you could use e.g.:

Using the library

Let's assume our new project is called typer_gui.

One way to use the library would be to manually switch from typer to typer_gui. Instead of import typer as above, you would do import typer_gui as typer, and that's all you'd need to do, because typer_gui would function as a drop-in replacement for typer. Also, you could do:

try:
     import typer_gui as typer
except ImportError:
    # fallback to CLI
    import typer

Better than either of these would be to make it easy to use either the CLI or the GUI version, depending on context. If someone runs an app from a terminal, they get the CLI (the GUI libraries would not even be imported, for speed). If they run it by double clicking on the app from a file manager, they get the GUI version:

from typer_gui.auto_choose import typer

# `typer` will now be either normal `typer` or `typer_gui`

Challenges and questions

How should you display output from the script, and get any further interactive input?

Relevant Technology

Understanding of how Typer works, and some basic GUI technology.

Complexity and required time

Complexity

Required time (ETA)

Although it would take a lot of work to complete, I think it wouldn't take too long to get something visible that worked for very simple cases, which would be quite helpful for motivation.

Categories

KOLANICH commented 2 years ago

Typer allows you to use Python type annotations to make it easy to create command line interface applications

You may also want to look at plumbum and click

KOLANICH commented 2 years ago

It would be awesome if I could easily convert a script or program written like this into a simple, easy-to-use GUI

I have a better idea - parse CLI --help output and generate GUI from it. And if I remember right, there are already apps doing this.

spookylukey commented 2 years ago

It would be awesome if I could easily convert a script or program written like this into a simple, easy-to-use GUI

I have a better idea - parse CLI --help output and generate GUI from it. And if I remember right, there are already apps doing this.

I imagine that parsing --help output could be quite fragile, and will end up losing information.

However, I have found Gooey does something very similar to this, but for apps using argparse. It seems like a module for Gooey that works with the Typer API instead of the argparse API would be exactly what I'm looking for.

KOLANICH commented 2 years ago

I imagine that parsing --help output could be quite fragile

The better approach is to specify CLI args in a formal language (conversion by the means of libs), compile it into a binary blob, and embed this blob into binaries. So, to get a spec for CLI args one would just need to lookup an exported symbol and parse the blob. And alternatively, a command to print the blob into stdout.

See https://github.com/CLIUtils/CLI11/issues/561

akovner commented 2 years ago

Any thought of converting to html instead? Perhaps even a framework like React or Vue?

so-rose commented 7 months ago

I've been weighing the idea of trying to apply one of these to a Typer project:

The first supports click, which typer is directly based on - so that kind of effort would likely be mainly gluing it all together.

The second is very argparse-based, which I'm unsure if is being used somewhere deep under the hood by click. Likely not - but it's not totally unlikely that there could be some semi-simple way of taking the click representation and reshaping it into an argparse representation. Worth investigating.