Hey what's going on friends, my name is Amir from Bek Brace channel, and in this tutorial I am going to show you how to build a Weather checker CLI application using only Python language and openWeatherMap API.
Now, I know what you're thinking. "But isn't Python just for data analysis and machine learning?" Sure, those are some of its main use cases, but Python is also incredibly versatile and can be used for a wide range of applications, including building a weather checker CLI app [ I'm a Ms-Dos generation - 40 Y.O. - so CLI apps warm my heart :D ].
Ok. Let's dive into the code.
We start by importing the necessary libraries - requests, argparse, chalk, and pyfiglet. Don't worry if you're not familiar with these libraries, we'll explain them as we go along. We also define our API key and base URL for the OpenWeatherMap API, which we'll be using to get the weather information.
Next, we use the argparse library to parse the command-line arguments. This allows the user to specify the country or city they want to check the weather for. We construct the API URL with the query parameters, using the country argument provided by the user.
We then make the API request using the requests library and check the response status code. If the status code is not 200, we print an error message and exit the program. Assuming we get a successful response, we parse the JSON data and extract the weather information we're interested in, including the temperature, feels like temperature, description, icon, city, and country.
Finally, we construct the output string with the weather icon and print it to the console using the chalk library for some added color.
Overall, building a weather checker CLI app with Python is a fun and practical way to explore the language and as I always say in my videos and write in my blogs: Invest more time and effort in learning the core concepts of the language itself, do not jump too soon to Django, Express or Next.js.
Keep learning, keep coding and I will see you in the next post.
Top comments (0)