0% found this document useful (0 votes)
9 views5 pages

Industrial Programming (Python) - Case Study 2 - Puviyarasu A

The document outlines a case study for designing a live weather application using Python, which retrieves current weather data from the OpenWeather API. It details the application's functionality, including fetching and displaying temperature and weather conditions, as well as the GUI setup using tkinter. The code provided demonstrates how to implement the application while adhering to specified constraints on library usage.

Uploaded by

howyoudoin011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Industrial Programming (Python) - Case Study 2 - Puviyarasu A

The document outlines a case study for designing a live weather application using Python, which retrieves current weather data from the OpenWeather API. It details the application's functionality, including fetching and displaying temperature and weather conditions, as well as the GUI setup using tkinter. The code provided demonstrates how to implement the application while adhering to specified constraints on library usage.

Uploaded by

howyoudoin011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Industrial Programming (Python)

Case Study 2 – Libraries and Consuming Web APIs

Puviyarasu Ayyappan Student ID :991798393

Objectives: To design a live weather application


Design and code a Python program that retrieves and shows current weather details in a
proper way. Your program will display the real weather forecast in your city obtained from
https://openweathermap.org/current, including current temp, feels_like temp, max and min
temps, and forecast description (e.g “shower rain”).

Choices of the UI design are up to you, but a good UI design with good amount of details and
familiar look and feel is expected. Do not use others’ projects, and do not use libraries other
than json and urllib to perform the tasks.

Part I: OpenWeather.

OpenWeather is a cloud-based platform that provides a variety of weather services through


Application Programming Interfaces (APIs). It is popular among programmers as it is very useful
to retrieve real-time weather updates, forecasts, and historical data.

Key Features:
- Real-Time Weather Data: Provides live weather data for cities worldwide. (Covers data
from over 200,000 cities)
- Forecasting: Provides daily and hourly forecasts.
- Customizable Metrics: Supports both metric and imperial units.
- API Integration: APIs use REST architecture and JSON for data representation, making
them easy to integrate into applications.

To access OpenWeather's services, developers need to sign up for an account and generate an
API key. The unique key generated is then used to authenticate requests.
Part II: Code
#Importing Libraries for API functionality
import json
import urllib.request

#Importing Library for GUI


from tkinter import Tk, Label, Entry, Button, StringVar

# Function to fetch weather data from API


def fetch_weather():
city = city_var.get()
api_key = "96deae45184f660a9f3be797ea38e036" # OPEN WEATHER API KEY
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"

try:
# Fetching and parsing the JSON data
with urllib.request.urlopen(url) as response:
data = json.load(response)

# Extracting weather details


temp = data["main"]["temp"]
feels_like = data["main"]["feels_like"]
temp_min = data["main"]["temp_min"]
temp_max = data["main"]["temp_max"]
description = data["weather"][0]["description"].capitalize()

# Updating the weather details on the screen


result_var.set(f"Weather in {city}:\n\n"
f"Temperature: {temp}°C\n"
f"Feels Like: {feels_like}°C\n"
f"Min Temp: {temp_min}°C\n"
f"Max Temp: {temp_max}°C\n"
f"Condition: {description}")
except Exception as e:
result_var.set("Error fetching data. Please check the city name or API key.")

# Setting up the GUI (using tkinter)


app = Tk()
app.title("Weather Application")
app.geometry("500x400")

# Setting background color


app.configure(bg="#87CEFA") # Light sky blue

# Adding a title label


Label(app, text="Weather Application", font=("Helvetica", 24, "bold"), bg="#87CEFA", fg="white").pack(pady=10)

# Field for entering the city name


Label(app, text="Enter City Name:", font=("Helvetica", 14, "italic"), bg="#87CEFA", fg="black").pack(pady=5)
city_var = StringVar()

Entry(app, textvariable=city_var, font=("Helvetica", 14), bg="white", fg="black").pack(pady=5, ipadx=50)


# Fetch weather button
Button(app, text="Get Weather", command=fetch_weather, font=("Helvetica", 14, "italic"), bg="#4682B4",
fg="white").pack(pady=10)

# Labeling to display the results


result_var = StringVar()
Label(app, textvariable=result_var, font=("Arial", 12), justify="left", bg="#87CEFA", fg="black",
wraplength=400).pack(pady=10)

# Running the GUI application


app.mainloop()

Explanation:
We retrieve real-time weather data for the user-specified city using the OpenWeather API. This
data includes temperature, "feels like" temperature, minimum and maximum temperatures, and a
description of the city’s current weather condition (e.g., cloudy, sunny). This information is
displayed on a clean and interactive GUI built using Python's tkinter GUI library.

1. Importing Libraries
● json: Parses the JSON data received from the API.
● urllib.request: Handles the HTTP request to fetch data from the OpenWeather API.
● tkinter: A Python library to create the graphical interface with widgets like labels,
buttons, and entry fields.

2. Weather Fetching Functionality


● User Input: Captures the city name using tkinter's input field.
● The API key is an unique authentication token provided by OpenWeather.
● The Open Weather URL includes the city name, API key, and parameters for
temperature units.
● Extracting Weather Details:
● temp: Current temperature.
● feels_like: Perceived temperature.
● temp_min and temp_max: Minimum and maximum temperatures of the day.
● description: Textual description of the weather condition.
● Display: Formats and updates weather data into a string for display.
● Error Handling: Alerts the user if the API key or city name is invalid or if there’s a
connection issue.
3. GUI
● Includes a label prompting the user to enter a city name.
● Provides a text box for the user to type the city name.
● A button labeled "Get Weather" fetches weather data when clicked.
● A section to display formatted weather data, by wrapping text neatly, ensuring readability
even for long city names or descriptions.
● The GUI remains active and responsive until the user closes the application.

Part III: Screenshots

You might also like