DEV Community

Cover image for A Friendlier Interface with Inquirer for Python
lakota
lakota

Posted on

A Friendlier Interface with Inquirer for Python

As a new programmer, the primary allure of python over javascript is how readable its code is. Indeed, much of the syntax of python is so close to just being a basic sentence that it can be read and understood without any coding experience:
Unfortunately, while it may have javascript beat on the readability of its syntax, python alone is not able to interface as well as javascript, if for nothing else because of the lack of a clear frontend with which the user can interface. Javascript allows users to interact through front end websites filled with navigation bars filled with buttons that change colors to denote they've been clicked, scroll down menus, search bars and forms they can fill out. Python, on the other hand, is more often used for projects without the need of the frontend for users to interface with, and for that reason it does not have much in the way of user friendly interfacing on its own. For projects where readability to the user and ease of use are important, we have inquirer.
Originally a npm package used in javascript, inquirer for python is a pipenv package used to create a more user friendly interface system on the command line (command line interface).
To start using inquirer for python is as simple as navigating to the root directory of the project you want to use it with, then entering the install command:

pipenv install inquirer
Enter fullscreen mode Exit fullscreen mode

From there, any file in your project that you would like to use inquirer with, simply at this code at the top of the file page:

import inquirer
Enter fullscreen mode Exit fullscreen mode

Straightforward, right? From here we can make user interfacing with the program easier and friendler than ever before.Let's look at my top three favorite features:

Confirm:
Sometimes the simplest tool tends to be the one you use the most, and a simple yes or no prompt at the command line can be one of the most powerful tools for allowing the user to easily nagivate your program. The code to allow this, and all inquirer features, is formatted more or less like this:

questions =[
    inquirer.Confirm("boho", message="Is this the real life?"),
    inquirer.Confirm("rhap", message="Is this just fantasy?", default=True),
]

answers = inquirer.prompt(questions)
Enter fullscreen mode Exit fullscreen mode

The result on the command line will be:

[?] Is this the real life? (y?N): Y
[?] Is this just fantasy? (Y/n):

Checkbox:
Ever wish you could take the check box from a javascript website app and put it into your python code for your users? Well checkbox is inquirer's answer! With this, you can allow your users to check off whatever they want from an O to an X, here's how to code it:

questions = [
    inquirer.Checkbox(
        "interests",
        message="What would you like in your boba tea?",
        choices=["yogurt","cheese top","boba","mango popping 
         boba", "oreo", "grass jelly", "The souls of the 
         innocent", "marshmallow fluff"],
   ),
]
answers = inquirer.prompt(questions)
Enter fullscreen mode Exit fullscreen mode

This will return what amounts to the sort of interactive menu you would see on something like doordash:

[?] What would you like in your boba tea?
O yogurt
O cheese top
O boba
O mango popping boba
O oreo
O grass jelly
O The souls of the innocent
O mashmallow fluff

Further, by adding in a single line of code, we can set a few of our items to automatically be selected:

questions = [
    inquirer.Checkbox(
        "interests",
        message="What would you like in your boba tea?",
        choices=["yogurt","cheese top","boba","mango popping 
        boba", "oreo", "grass jelly", "The souls of the innocent", 
        "marshmallow fluff"],
        default=["boba", "oreo"],
   ),
]
answers = inquirer.prompt(questions)

Enter fullscreen mode Exit fullscreen mode

List:
This is my personal favorite, and one of the most versatile. List works very similarly to checkbox in its format, offering the user a list of options to choose from that they are able to scroll up and down through using the up and down arrow keys. But what makes list a particularly useful feature is that it is perfect for setting up dialogue trees, wherein selecting a certain answer prompts a linked, new question:

 questions = [
                inquirer.List(
                    "chosen_menu",
                    message = "Welcome to the RPG weapon perking 
                    simulator, what would you like to do?",
                    choices = [
                        "View Weapons List",
                        "View Perk List ",
                        "View Perked Weapons",
                        "Exit"
                    ]
                )
            ]
            response = inquirer.prompt(questions)
            if response["chosen_menu"] == "View Weapons List",
                view_weapon()
            elif response["chosen_menu"] == "View Perk List",
                view_perk()
            elif response["chosen_menu"] == "View Perked Weapons",
                view_perked_weapons()
            else:
                exit()
Enter fullscreen mode Exit fullscreen mode

"view_weapon()", "view_perk()", and "view_perked_weapons()" are all themselves lists that link to even more lists in a dialogue tree that allwos the user to very efficiently and easily navigate the app fromt the command line.

With these tools out your disposal, its never been easier to make python as user friendly to interface with as javascript's frontend!

Top comments (0)