Forecast Your Weather With Python Script

prkskrs
Catalysts Reachout
Published in
2 min readSep 9, 2022

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 requests
import json
from 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-key
appId="944e28e*********************"

Step 3 : Enter your place name

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

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

# queries
unit="metric"

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

# API url
url="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-hit
response=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" type
jsn=json.loads(data.decode("utf-8"))

Step 9 : Store important data from converted json file

# get temperature
temp=jsn["main"]["temp"]
# get weather icon
icon=jsn["weather"][0]["icon"]
# get weather description
weatherDesc=jsn['weather'][0]["description"]

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

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

Step 11 : Display Outputs

# display png
display(Image(response2.content))
# display temperature
print(f"Temperature : {temp}°C (Degree Celcius)")
# display place name
print(f"Place : {query}")
# display weather description
print(f"Weather Description : {weatherDesc}")

Sample Output :

Github Link For Full Code : Click Here

--

--

prkskrs
Catalysts Reachout

backend developer and data scientist. currently studying computer science and constantly learning!