Develop Your Weather Application with Python in Less Than 10 Lines | by Bharath K | Towards Data Science

Weather is one of the most crucial aspects of our life. It dictates the different kinds of activities that we would like to plan throughout the day, week, or month. One of the older methods of reading weather patterns is to look at the sky and predict whether it would stay sunny, rain, or have a humid weather condition.
This method might not be the most effective if you are not well-versed with climatic such as me and always end up getting your predictions wrong. The other method includes waiting and watching news channels for their daily reports to analyze the weather. However, these ways seem a bit outdated for the modern era we live in today.
There are different devices and tools that will allow you to interpret the current weather conditions from satellite reports and numerous scientific studies. In this article, we will make use of such API technologies with Python to create your own weather reporting application in less than ten lines of code. So, without further ado, let us get started on accomplishing this project.
For this project, we will utilize Python and a weather reporting service that provides us with an API key to interpret the daily weather conditions of any particular place. One of the few library requirements that we will need for this project is the request module that is available in Python. If you don’t have it already, feel free to install the following module with a simple pip install command and import the library as shown below. This tool will allow us to directly access HTTP links for the required weather API applications.
import requests
Now, we will require an API key from a website that stores information about the weather and forecasts them with accurate results. The Open Weather Map website provides a scientific yet simplistic approach for utilizing their technology in our Python code to quickly yield the desired results of weather forecasting. To generate your own API key, simply log in to the following website and create an account for totally free. You can either use one of their suggested API keys or generate one of your own. Paste this generated API key in a variable of your choice, as shown below.
API_Key = ""
Our next step is to allow the user to enter their desired location for which they want to analyze or view the weather conditions. We can perform the following action with an input command allowing the user to type in the required location. Note that you are allowed to type only locations that actually exist. If you mistype the name of the location or of a location that does not exist, you will receive an error message that will be displayed.
We will now create a variable that will store the default URL location to the weather access website. For the final URL, we will combine the default path of the open weather map website with the API key that we have previously generated and stored in the API_Key variable. Finally, in the weather data variable, we will use the requests library to get the weather data and store the information accordingly.
location = input("Enter Your Desired Location: ")weather_url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid="final_url = weather_url + API_Keyweather_data = requests.get(final_url).json()
Now that we have successfully collected our weather information in the respective variable, we can proceed to display the required data to the user. The following action can be done with the print statement, as shown in the below code snippet.
print(weather_data)
You may notice that the information that is displayed from the print statement does not look as pretty as you might have expected it to, as the data collected and displayed are in the raw format. Hence, we can make use of the Data pretty printer module to allow us to print the useful information in a more presentable and readable manner for the user.
from pprint import pprintpprint(weather_data)
In the next section of this article, we will look at the complete code for constructing our weather app as well as analyze the numerous improvements and advancements that we can make to this project.
With the discussion of all of the elementary code snippets from our previous section, we can combine them together to construct our final code block for this project. Feel free to use the following code provided below to experiment with various locations. Below is a result to show the effective working of this project.
import requestsfrom pprint import pprintAPI_Key = ""location = input("Enter Your Desired Location: ")weather_url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid="final_url = weather_url + API_Keyweather_data = requests.get(final_url).json()pprint(weather_data)
Enter Your Desired Location: Bangalore{'base': 'stations', 'clouds': {'all': 15}, 'cod': 200, 'coord': {'lat': 12.9762, 'lon': 77.6033}, 'dt': 1641477819, 'id': 1277333, 'main': {'feels_like': 294.17,          'grnd_level': 912,          'humidity': 45,          'pressure': 1013,          'sea_level': 1013,          'temp': 294.78,          'temp_max': 296.05,          'temp_min': 294.78}, 'name': 'Bengaluru', 'sys': {'country': 'IN',         'id': 2040609,         'sunrise': 1641431598,         'sunset': 1641472610,         'type': 2}, 'timezone': 19800, 'visibility': 10000, 'weather': [{'description': 'few clouds',              'icon': '02n',              'id': 801,              'main': 'Clouds'}], 'wind': {'deg': 118, 'gust': 8.31, 'speed': 4.81}}
One of the unique variations that the users can try out with the project is to display the weather report of your location whenever you start the program. And continue to display it for every few hours to rapidly check the climatic variation to plan your schedule accordingly. You can set your desired location as required in the code and construct the project in a fashion similar to the reminder application that I have covered previously in one of my articles. Feel free to check it out from the link provided below.
towardsdatascience.com
Another additional improvement that the users can make is to construct a GUI application for displaying the weather conditions accordingly. If you are not well acquainted with Graphic tools with Python, you can check out one of my previous articles to learn more about seven of the best GUI’s with starter codes.
towardsdatascience.com
“Wherever you go, no matter what the weather, always bring your own sunshine.” — Anthony J. D’Angelo
Weather, as discussed previously, plays a crucial role in our daily life. Hence, it is highly beneficial to develop a weather application that will help us track this integral element of nature successfully so that we can all plan our schedules accordingly and choose what would be the best course of action to go about in our daily life.
In this article, we learned how to construct a weather report application in about ten lines of Python code. We looked into the simple API key generation and used the request module to access the required information for displaying the accurate weather reports of the particular location. We also analyzed some of the additional improvements that we can accustom to this project to make it more appealing and useful.
If you want to get notified about my articles as soon as they go up, check out the following link to subscribe for email recommendations. If you wish to support other authors and me, then subscribe to the below link.
bharath-k1297.medium.com
If you have any queries related to the various points stated in this article, then feel free to let me know in the comments below. I will try to get back to you with a response as soon as possible.
Check out some of my other articles in relation to the topic covered in this piece that you might also enjoy reading!
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
Thank you all for sticking on till the end. I hope all of you enjoyed reading the article. Wish you all a wonderful day!