Linux Fu: Python GUIs For Command Line Programs (Almost) Instantly

Not every programmer likes creating GUI code. Most hacker types don’t mind a command line interface, but very few ordinary users appreciate them. However, if you write command line programs in Python, Gooey can help. By leveraging some Python features and a common Python idiom, you can convert a command line program into a GUI with very little effort.

The idea is pretty simple. Nearly all command line Python programs use argparse to simplify picking options and arguments off the command line as well as providing some help. The Gooey decorator picks up all your options and arguments and creates a GUI for it. You can make it more complicated if you want to change specific things, but if you are happy with the defaults, there’s not much else to it.

At first, this article might seem like a Python Fu and not a Linux Fu, since — at first — we are going to focus on Python. But just stand by and you’ll see how this can do a lot of things on many operating systems, including Linux.

Hands On

We had to try it. Here’s the code from the argparse manual page, extended to live inside a main function:

import argparse

def main():

   parser = argparse.ArgumentParser(description='Process some integers.')
   parser.add_argument('integers', metavar='N', type=int, nargs='+',
         help='an integer for the accumulator')
   parser.add_argument('--sum', dest='accumulate', action='store_const',
        const=sum, default=max,
   help='sum the integers (default: find the max)')

   args = parser.parse_args()
   print(args.accumulate(args.integers))

main()

You can run this at the command line (we called it iprocess.py):

python iprocess.py 4 33 2
python iprocess.py --sum 10 20 30

In the first case, the program will select and print the largest number. In the second case, it will add all the arguments together.

Creating a GUI took exactly two steps (apart from installing Gooey): First, you import Gooey at the top of the file:

from gooey import Gooey

Then add the decorator @Gooey on the line before the main definition (and, yes, it really needs to be on the line before, not on the same line):

@Gooey
def main():

The result looks like this:

You might want to tweak the results and you can also add validation pretty easily so some fields are required or have to contain particular types of data.

Sure That Works on Linux, But…

Python, of course, runs on many different platforms. So why is this part of Linux Fu? Because you can easily use it to launch any command line program. True, that also should work on other operating systems, but it is especially useful on Linux where there are so many command line programs.

We first saw this done on Chris Kiehl’s blog where he does a GUI — or Gooey, I suppose — for ffmpeg which has a lot of command line options. The idea is to write a simple argparse set up for the program and then tell GUI what executable to actually launch after assembling the command line.

Chris Kiehl ffmpeg GUI

Here’s Chris’ code:

from gooey import Gooey, GooeyParser

@Gooey(target="ffmpeg", program_name='Frame Extraction v1.0', suppress_gooey_flag=True)
def main():
   parser = GooeyParser(description="Extracting frames from a movie using FFMPEG")
   ffmpeg = parser.add_argument_group('Frame Extraction Util')
      ffmpeg.add_argument('-i',
      metavar='Input Movie',
      help='The movie for which you want to extract frames',
      widget='FileChooser')
   ffmpeg.add_argument('output',
      metavar='Output Image',
      help='Where to save the extracted frame',
      widget='FileSaver')
   ffmpeg.add_argument('-ss',
      metavar='Timestamp',
      help='Timestamp of snapshot (in seconds)')
   ffmpeg.add_argument('-frames:v',
      metavar='Timestamp',
      default=1,
      gooey_options={'visible': False})

parser.parse_args()

if __name__ == '__main__':
   main()

 

You even have the option of creating a JSON file that Gooey can read if you don’t want to write Python. The utility of this is easy to see, but I’d love to hear some concrete examples of where you think it will come in handy. If you’re already using Gooey, or plan to give it a shot after reading this article, let us know in the comments below.

Of course, not all Python GUIs are created equal. Neither are all Python graphics.

7 thoughts on “Linux Fu: Python GUIs For Command Line Programs (Almost) Instantly

  1. I’ve written a little python utility to be used in an office environment. Tried Gooey to present it as a simple desktop program. I like the concept, but you’ll end up replacing argparse with gooey in order to get access to the really useful parts of gooey. For example the browse buttons to select files. Not being able to keep using argparse is a big drawback. Also, when using gooey instead of argparse, the gui is started by default and you ‘loose’ the cli (there’s a flag to tell gooey to use the cli, but I can’t and don’t want to remember).
    For my use case, it would be very nice if all gooey configuration was done with decorators and the gui optional with a – – gui flag. The utility would be started from a start menu or desktop icon anyway. There must be ways to detect the code is called from the gui or cli.

  2. Python always looks like “ALMOST) INSTANTLY” in carefully selected bookworm examples. But when you try to implement something useful in real world it became ugly after first 200 lines of code.

    Python funboys don’t get frustrated though, hence you can see “python software” which drops a traceback, when Ctrl+C is pressed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.