Building a CLI Weather Checker App with Python and OpenWeatherMap API

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 i…


This content originally appeared on DEV Community and was authored by Bek Brace

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.


This content originally appeared on DEV Community and was authored by Bek Brace


Print Share Comment Cite Upload Translate Updates
APA

Bek Brace | Sciencx (2023-04-20T18:18:26+00:00) Building a CLI Weather Checker App with Python and OpenWeatherMap API. Retrieved from https://www.scien.cx/2023/04/20/building-a-cli-weather-checker-app-with-python-and-openweathermap-api/

MLA
" » Building a CLI Weather Checker App with Python and OpenWeatherMap API." Bek Brace | Sciencx - Thursday April 20, 2023, https://www.scien.cx/2023/04/20/building-a-cli-weather-checker-app-with-python-and-openweathermap-api/
HARVARD
Bek Brace | Sciencx Thursday April 20, 2023 » Building a CLI Weather Checker App with Python and OpenWeatherMap API., viewed ,<https://www.scien.cx/2023/04/20/building-a-cli-weather-checker-app-with-python-and-openweathermap-api/>
VANCOUVER
Bek Brace | Sciencx - » Building a CLI Weather Checker App with Python and OpenWeatherMap API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/20/building-a-cli-weather-checker-app-with-python-and-openweathermap-api/
CHICAGO
" » Building a CLI Weather Checker App with Python and OpenWeatherMap API." Bek Brace | Sciencx - Accessed . https://www.scien.cx/2023/04/20/building-a-cli-weather-checker-app-with-python-and-openweathermap-api/
IEEE
" » Building a CLI Weather Checker App with Python and OpenWeatherMap API." Bek Brace | Sciencx [Online]. Available: https://www.scien.cx/2023/04/20/building-a-cli-weather-checker-app-with-python-and-openweathermap-api/. [Accessed: ]
rf:citation
» Building a CLI Weather Checker App with Python and OpenWeatherMap API | Bek Brace | Sciencx | https://www.scien.cx/2023/04/20/building-a-cli-weather-checker-app-with-python-and-openweathermap-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.