Patternite Logo

Write better Python CLIs with Typer

0

Typer is a powerful library for creating useful, straightforward command line interfaces

Loading...

In this application, we're interested in getting the current weather conditions on Mars. Thanks to the NASA InSight Lander, we can get these measurements for the last 7 Martian days (referred to as "sols"). Here, we build a Typer app, which makes it easy to interface with the NASA InSight API. Typer is a powerful command line interface (CLI) library for Python, and is generally a better choice than something like argparse.

First we define a Typer app, as well as some sub-applications on lines #9-13. We'll also make sure to wrap our app() call in an if __name__ == "__main__": conditional on lines #71-72. On lines #17-20, we call the NASA InSight weather API to get the latest Martian weather report, filter out unnecessary fields, and retain only the latest sol data.

Now we can start adding commands to our Typer app. In our case, we're interested in getting the current season with respect to either the north or south hemisphere, the maximum or minimum atmospheric pressure, and the maximum or minimum atmospheric temperature.

Season data

We want to be able to get the current Martian season for either the north or south hemisphere. Typer makes use of Python 3.6+ type hints, so we can create a new Enum type called Hemisphere. Hemisphere is a class which inherits from Enum, and defines a set of acceptable values - namely "north" and "south". Now we can create a command called season, which is defined as a Typer command with the @app.command decorator. season takes in an argument hemisphere of Hemisphere type, and returns the relevant season data.

We can now get seasonal data by running our Typer app in the CLI:

$ python weather.py season north
late winter

Note that by defining hemisphere to be of Hemisphere type (i.e. an Enum), Typer will actually disallow any values that are not in Hemisphere - that is, anything other than "north" and "south". Additionally, calling season --help will list the valid values for our season command.

Pressure and temperature data

Next, we want to access the pressure readings or temperature readings, however, these values might not be available for the given sol.

Starting with pressure, we'll define a callback on the pressure_app we defined above. This callback runs any time a command on the pressure_app is called. We define a pressure_main function to check if the pressure measurement is available, and if not, exit the program. Now we can define some commands on the pressure_app to get the maximum or minimum pressure (lines #47-53). We can run these commands as follows:

$ python weather.py pressure maximum
743.1983

$ python weather.py pressure minimum
698.7664

The temperature implementation is almost identical to pressure.

Conclusion

Typer is an excellent library for writing powerful CLIs in a straightforward way. If you still use argparse, it might be time to consider switching over.

Check out the Typer docs for a more in-depth guide.

Profile picture for duncster

| edited

Patternite © 2021

Patternite Logo