Create a Command Line Tool with Python

A Python Script to Display Weather Predictions (Part 1)

Sandesh Chapagain
Python in Plain English

--

Photo by Sora Shimazaki from Pexels

Checking the weather on a phone, or a desktop browser is not really much of a hustle, so why bother? First, if you’re working on your computer, then the terminal is much more accessible than your phone or even your browser. Second, if you know some Python, or are interested in learning the basics, then why not build something useful of your own? I love creating small projects with whatever languages or skills I learn, and if that sounds like you, let’s get started and make something.

What do I need to know?

I only assume that you have a basic understanding of Python (variables, control-flow, functions, etc.). We’ll be running our python program primarily through the terminal, so an experience in using the command line would be a plus, but is not essential in following along.

What can the final application do?

At the end of this, we’d have built an application that displays weather predictions for any given day up to a week in the future. The interface is completely customizable, and you’re free to stretch your creativity in that area. My terminal looks something like this:

Don’t worry if you’re used to Fahrenheit — we’ll make that customizable as well

Let’s dive into it!

Getting the API key

First things first, we don’t want to write algorithms to predict the weather ourselves, so let’s use an API that allows us to get weather predictions for free. The most used service for this is openweathermap. All you have to do is make a free account, and get an API key, which looks like this:

api key from openweathermap

Setting up the code skeleton

To get started, lets create a new python script , and setup a few variables and functions.

As you might have noticed from the code above, we’re hard-coding the units, lat, long, and the day number for which we want to display the predictions. This is intentional for now, and we’ll add the option for the user to provide them through command line options later.

Using the requests library

Now that we’ve setup all the necessary functions, we’ll start filling in the gaps. First let’s setup the get_weather_data function so that it gives us the relevant information for the given day number.

The requests library is the go-to library for connecting to APIs in Python. It is simple and powerful — the perfect tool for beginners and pros alike. We’ll be using it to get predictions from openweathermap.org.

Alright, a lot of stuff happened in that function. Let’s break it down.

First we setup our lat, long, and the API key. Then, we constructed the api_endpoint string, whose formatting is dictated by openweathermap’s documentation. I chose to break the endpoint into four substrings and add them at the very end, but you could only build one giant string if you prefer.

Then we used requests to fetch data from the API, and if the request succeeded (status code == 200), we parsed the response and extracted only the information we want. If you want to display more information than what we’re extracting in here, (like wind speed, for e.g), please refer to the docs to know more about the structure of the response.

Displaying the information

Now that we have what we want to display, let’s write up the print_weather function.

You might have noticed that earlier, we setup units as “metric” in our main function, so the code above would print something like : “Feels like: 32metric”, which is not what we want. So let’s create a mapping from the system of units to their symbols in the main function, and use it before we attempt to print our data.

Assuming your file is called main.py, and that you’ve correctly set up the latitude, longitude, and api_key in the script above, you should now be able to use your code to get predictions for today. Open up your terminal in the directory where your main.py is located, and then type the following command:

python main.py# displays the following
Description: overcast clouds
Temperature: -4.38°C (-13.62°C to -3.44°C)
Feels like: -8.88°C

Warning : If your version of python is python2 by default, you might need to write python3 main.py

Conclusion

If you’ve made it this far, great job! You have now made a small application that can display current weather using only your terminal window. In the next part of this article, we will:

  • Allow queries for not only today, but also other days within the week
  • Allow users to select the system of units to be used
  • Learn how to write a shell script and add it to our environment PATH so that we can execute our program with a much simpler command

Thank you for reading the article, and I hope you’ve learnt something with this. Please feel free to leave any comments or suggestions you might have.

Happy hacking!

--

--