Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson


00:00 In this lesson, I’ll show you how you can use the Python Prompt Toolkit library to build an alternative command line interface for the seq utility.

00:11 For this demonstration of an implementation of seq using the Python Prompt Toolkit, I’m actually going to run this in the terminal, first, and then I’ll go through the code. Because I think once you see how it works, a lot of this Python will make more sense. When I run this…

00:29 I’ll get a little dialog box instead of having to enter in command line arguments directly. And if I print out the help, here, it’ll tell me Print numbers from FIRST to LAST, in steps of INCREMENT. But you’ll notice it’s not really like the conventional usage message, and that’s because when I click this Sequence action, it gives me all the prompting that I need here, right?

00:51 So, I can enter in an argument, FIRST, which is just 1. Maybe I enter in 4 as the INCREMENT, and then maybe for LAST, I’ll just put in 22—and actually, I’ll put in a space after that, just that you can see that if I put in a space it’ll say Ensure that you enter a number, because it’s technically malformed input. So, that’s how you get a little bit of error handling inbuilt into here, which is pretty nice. And it’ll just let me enter it again, so I’ll enter 22, and then it prints out starting at 1, going up by 4, all the way until it gets to 22. So, pretty darn cool!

01:24 And this is not quite as fully-featured as the normal version of seq, because I haven’t added a separator option, and I also haven’t allowed for negative number input, but that’s just to make this a little bit simpler to demonstrate.

01:38 Now, let’s take a look through this code and see how it differs from a more conventional command line interface.

01:44 So, I’m actually going to start from the very bottom here and go down to the main() function. So, the key to this main() function is this button_dialog, which is called by calling its .run() method.

01:58 So, this button_dialog is an inbuilt part of the Prompt Toolkit. You can see it has a title, it has a text and then it has some buttons, which you’ll probably remember from a second ago.

02:09 You have three different buttons and you can choose any of them. And then, of course, the actual result you can see from this section here will be one of these three

02:19 strings, "SEQUENCE", "HELP", or "VERSION", and then the main script runs—or I should say calls—the function that’s mapped to by each of these strings.

02:32 So, "SEQUENCE"—it runs seq_dlg() (seq dialog), "HELP"—it runs help(), and "VERSION"it runs version(). So, going up to these, you can see that help() and version() are just functions that print something, so this is a little bit different from other kinds of command line interface packages that you’ve seen that handle the version and the help by directly printing. This one handles it by calling these functions, which then print things. So, going back down here to this seq_dlg() you can see it has this kind of logic where there are these three labels, some operands, and it has a while True loop that generates an input_dialog and it prompts you to enter an argument, and the argument is based on one of these labels.

03:12 So, you’re looping through these labels and then it does some basic error handling here, checks to see if the input is a digit, and, if so, it appends it to the operands, otherwise gives an error dialog, and then it breaks out when there are three total operands.

03:27 So, you could think of ways to make this better or more concise, but what this is doing is pretty simple. It’s just looping through and prompting you to enter "FIRST", "INCREMENT", and then "LAST", and then breaking out when it gets all the operands.

03:40 And then, it has the operands and it calls seq() which is the same seq() function that I’ve been showing you this whole time.

03:48 So, this kind of interface is what’s called a graphical user interface, or a GUI, or even some people call it a “goo-ey”. So, a GUI is nice because it often allows you to handle errors from the user by simply limiting the kinds of things that they can actually input to your program. That’s what’s awesome about a GUI. On the command line, you rely on your user to know the bounds of the kinds of things that they can actually input to your program, right? They have to input a number, for example, in this seq() case. But with a graphical user interface, you have a lot more flexibility in terms of how you can output an error dialog and then just keep on going and prompt them to enter stuff again.

04:27 That’s not unique to GUIs of course, but it’s something that works pretty easily in a GUI setting, which is quite nice. So, the overall thing to remember here is that Python Prompt Toolkit, which of course you have to install in some way—probably with pip, and then you have to actually import all of the things that you’ll need from that library—the thing to remember is that this is simply an alternative kind of command line interface. It’s not anything better or worse, it’s just a different way of thinking about this philosophy.

04:56 Next up is the conclusion.

Become a Member to join the conversation.