Raining Outside? Build a Weather CLI App With Python

Python

You’re stuck inside because of torrential rains—again! You wonder what the weather’s like in that faraway city where your friends from the Real Python community live. You’d rather stick around in your command-line interface (CLI) than look it up in your browser. If that sounds like a task you’d want to tackle by building a command-line weather app using only the Python standard library, then this tutorial is for you.

In this tutorial, you’ll learn how to:

Build a functional weather lookup tool using only Python standard library modules
Build a Python CLI app using argparse
Use configparser to handle API secrets
Make API calls from your Python script
Create visually attractive CLI output using ANSI escape codes, emojis, f-strings, and Python’s string mini-language

Looking out the window confirms your decision. With that rain, there’s no way to frolic in the meadows today. Time to write a Python weather app and dream of distant places right from your CLI! You might even discover a couple of surprising city names along the way.

Get Source Code: Click here to get the source code you’ll use to build your weather app.

Demo

While you wait for a rainbow to appear outside your window, you’ll add your own colors to your weather app. For that, you’ll use some formatting tricks that’ll make the CLI output snazzier than plain text:

If you like the look of this command-line app and want to train your Python coding skills while using the standard library, then you’ve come to the right place!

Note: This app was developed on macOS for the Zsh shell. Your mileage with some of the color display and emojis may vary depending on your setup. On Windows, PowerShell displays the formatting reasonably well, and if you’re on a newer version of the operating system, you should see good results with the defaults in Windows Terminal.

If you’d prefer another color scheme or want to use different emojis for your own app, you’ll be able to customize it while working along with this tutorial.

Project Overview

Before you start to write any code, it’s often a good idea to think about the specifications of the program you’re planning to build. First, take out a pen and paper and jot down your ideas for what a perfect weather app would look like. Once you’ve noted your ideas, you can click on the heading below to read what specifications you’ll consider when working through this tutorial:

The weather app you’ll build in this tutorial will:

Take a city name as required input
Take an optional flag to display the output in Fahrenheit instead of Celsius, if desired
Call an online weather API to fetch the weather data
Display the city name, the current weather conditions, and the current temperature
Format the output visually using colors, spacing, and emojis

With these quick notes, you have a rough plan of what you’ll build, and you’ve defined the scope of what the app will be able to do.

If you have different or additional features in mind, then keep your notes around and implement them on top of your weather app once you’ve finished building this initial version.

Note: Writing great command-line interfaces can be challenging. You want your app to be user-friendly and still provide all the functionality you need. Using higher-level libraries might make it easier for you to build out your applications.

In this tutorial, you’ll work with Python’s built-in argparse module, which assists you in creating user-friendly CLI apps, for example by providing a useful –help option out of the box.

You’ll get the current weather data by city name from different locations around the world, using the weather API from OpenWeather. This application programming interface (API) is free to use, with a generous quota of API calls. However, you’ll need to create a personal API key to make requests, which you’ll do in just a moment.

Before you start digging in, take another look at the expected prerequisites so that you know where you can brush up on your skills in case you get stuck somewhere along the way.

Prerequisites

To complete this tutorial, you should be comfortable with the following concepts:

Building command-line interfaces using argparse
Reading public data from the Internet with APIs
Integrating APIs in Python code
Working with JSON data
Importing modules and libraries
Defining your own functions
Handling errors with Python exceptions
Formatting your text using f-strings
Using conditional expressions

You’ll also need an API key for the weather API from OpenWeather, and you’ll learn how to get access to one in the upcoming section.

If you don’t have all of the prerequisite knowledge before starting this tutorial, that’s okay! In fact, you might learn more by going ahead and getting started. You can always stop and review the resources linked here if you get stuck.

Step 1: Get Access to a Suitable Weather API

Read the full article at https://realpython.com/build-a-python-weather-app-cli/ »

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]