Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 9, 2022 08:22 pm GMT

Forecast Your Weather With PythonScript

What we are going to build?
In order to check your place's weather. With the help of inbuilt libraries of python like requests, json and IPython. From "IPython" library, we use modules like "Image" and "display".

Step 1: Importing Dependencies

import requestsimport jsonfrom IPython.display import Image, display




Step 2: Enter your api-key
You can use https://api.openweathermap.org/ and get your API key. So that, you will get access of data.

# api-keyappId="94*********************"




Step 3 : Enter your place name

# place inputquery=input("Enter Your Place to Check Weather : ")




Step 4: Make queries for URL
Here you can write more specific queries for URL.

# queriesunit="metric"




Step 5: Create Dynamic URL
Design your URL. With query variables and make sure that you are using api key or id.

# API urlurl="https://api.openweathermap.org/data/2.5/weather?q="+f"{query}"+"&appid="+f"{appId}"+"&units="+f"{unit}"




Step 6: Send GET request and store response of URL hit

# get response from api-hitresponse=requests.get(url,stream=True)




Step 7: Store data from response

# get data (in bytes form)data=response.content




Step 8: Convert "bytes" format to json

# get json file from "bytes" typejsn=json.loads(data.decode("utf-8"))




Step 9: Store important data from converted json file

# get temperaturetemp=jsn["main"]["temp"]# get weather iconicon=jsn["weather"][0]["icon"]# get weather descriptionweatherDesc=jsn['weather'][0]["description"]




Step 10: Send GET request and store response of URL to fetch Image

# get request with imageUrl to fetch png imageimageUrl="https://openweathermap.org/img/wn/"+f"{icon}"+"@2x.png"response2=requests.get(imageUrl,stream=True)




Step 11: Display Outputs

# display pngdisplay(Image(response2.content))# display temperatureprint(f"Temperature : {temp}C (Degree Celcius)")# display place nameprint(f"Place : {query}")# display weather descriptionprint(f"Weather Description : {weatherDesc}")

Sample Output :

Forecast-Weather


Github Link For Full Code : Click Here


Original Link: https://dev.to/prkskrs/forecast-your-weather-with-python-script-2724

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To