Loading video player…

Developing CLIs and TUIs

CLI Libraries linked in this lesson:

Real Python CLI Resources:

TUI Libraries:

Real Python TUI Resources:

00:00 In the previous lesson, I covered some of the libraries for web development. In this lesson, I’ll cover some ways

00:09 When you write a script to run in a terminal, you may want to give that script arguments to control what it does. This is called the command-line interface, and there are libraries out there to help you parse the parameters to your program.

00:22 There’s a convention to how command-line arguments look. You don’t have to follow it, but it makes it easier for users to understand your program if you stick with it.

00:30 The convention breaks things down into arguments, that’s something like a filename, and flags, which usually are preceded by one or two hyphens. If you’ve ever done --help or -h, you were using this capability. To start out, Python comes with a very powerful, very flexible, sometimes a little difficult to use library for handling arguments and flags.

00:53 In fact, Python comes with several, but the most modern one that you should be using is argparse. Since argparse is a bit complicated, that tends to be a consequence of powerful and flexible, there are also third-party libraries out there that have simpler interfaces.

01:09 Click uses decorated functions to minimize the amount of code you need to write for arguments and flags. And a competitor to Click is Typer. It’s by the same folks as FastAPI and uses a similar approach.

01:22 Personally, I use argparse, but that’s mostly because I already put the effort in to learn it. If you’re just getting started and you’re okay with using third-party libraries for your project, either Click or Typer are good choices.

01:36 This is a tutorial on using argparse, and this one compares argparse, Click, and another library I didn’t mention called Docopt.

01:45 This tutorial is specific to using Click. And finally, if you want to try a full project that just happens to use the command line, this one shows you how to build a script that prints out directory trees.

01:56 You control how it behaves with arguments handled through argparse.

02:01 You’ve probably heard of GUI development with windows and widgets—your typical desktop interface. Well, a TUI is the same kind of idea, but using only text widgets.

02:11 As such, it runs completely inside your terminal. This can be helpful as terminals are more standardized than GUI environments, so these kinds of programs are more likely to work across platforms. Most of the time, you can also run these on remote machines through SSH as well.

02:28 There’s been a sort of renaissance lately with TUIs, partially due to a newer library called Textual, and there are a load of projects out there, some seriously useful and some just fun.

02:40 This is Tip Top, which I grabbed using the typical pip install command. This program gives you similar information to the top Unix command showing you just what’s running on your system, but it does it with a colorful display and graphs.

02:54 Tools like Textual allow you to lay out the screen with just a few lines of code, along with colorization and other features. There’s some neat stuff out there including a terminal-based piano.

03:06 Yeah, you heard me right, a piano. For color and stylization of your terminal output, start with a library called Rich. It’s by the same folks as Textual.

03:18 In fact, Textual is built on top of Rich and it uses CSS to do the layout of the screen, so if you already know your web stuff, you’re ahead of the game. Another TUI toolkit is asciimatics.

03:30 This one’s a little more focused on terminal animations. The demo includes a text-based squid flying around the screen. A smaller part of the library includes forms and windowing, which covers a lot of the same kind of ground as Textual. Although TUIs have been around for a long time, that renaissance I mentioned is rather recent, but there are still tutorials for you at Real Python.

03:53 This first one teaches you how to stylize your output with Rich. While this one uses Rich to build a Wordle clone, sshhh, don’t tell the New York Times.

04:04 And if you want something a little bigger, this project uses both Textual and SQL tools to build a contact book application.

04:13 Next up, I’ll move from text to graphics.

Become a Member to join the conversation.