Weather App Application Project Using Python [2023]
In this Weather App Session Using Python, "Aneeq Dholakia," co-founder of Edyst, teaches attendees about
1. Fetching weather data from the internet .
2. Accessing any specific location's real-time weather data.
3. Finding the most stable city based on its weather.
We will have one milestone for this session:
1. We will take the city name and date as input and publish the weather as the output.
Here is the website URL: https://oikolab.com
We will use the above mentioned URL to get the weather data.
Below is the entire program flow:
Also,we will be using 3 simple concepts for creating the Weather App Application in Python:
1. APIs
2. JSON
3. Pandas
Below are the details of these concepts:
Note: You can skip the details of the below concepts if you are familiar with them and proceed towards the main section.
Application Programming Interface (APIs)
APIs is a set of rules and protocols that defines how two or more pieces of software should interact with each other. It is often implemented using HTTP (Hypertext Transfer Protocol) and is accessed using HTTP requests. The most common types of HTTP requests are GET, which retrieves data from the server, and POST, which sends data to the server to be stored or processed. Other types of HTTP requests include PUT, DELETE, and PATCH.
There are many different commands or methods that can be used when working with APIs, depending on the specific API and the desired action. Some common API commands include:
- GET: Used to retrieve data from the server.
- POST: Used to send data to the server to be stored or processed.
- PUT: Used to update data on the server.
- DELETE: Used to delete data from the server.
- PATCH: Used to update a specific part of a resource on the server.
- HEAD: Used to retrieve metadata about a resource, without retrieving the resource itself.
- OPTIONS: Used to retrieve information about the options available for a specific resource.
- CONNECT: Used to establish a tunnel connection to the server.
- TRACE: Used to perform a message loopback test to the server.
- POST: Used to send data to the server to be stored or processed.
These are just a few examples of the types of commands that can be used with APIs. The specific commands and methods available will depend on the API and the actions it supports.
JavaScript Object Notation (JSON)
JSON is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is primarily used to transmit data between a server and a web application, as an alternative to XML. It is also used for storing data in NoSQL databases, such as MongoDB and Couchbase.
Pandas
Pandas is a powerful and flexible open source data analysis and manipulation tool built on top of the Python programming language. It provides fast and efficient data structures for working with structured and time series data, and has a wide range of tools for data cleansing, aggregation, and transformation.
One of the key features of pandas is its ability to work with missing or incomplete data. It provides functions and methods for handling missing values and imputing missing data, as well as tools for aligning and merging data from different sources.
Creating an account with Oikolab
Please note: In order to query Oikolab Weather Database we need an API key, we can get an API key only when we create an account with Oikolab. Here you can go ahead and download and create your account on Oikolab.
Session Codes
Milestone I
import json
import pandas as pd
import requests
city = input()
date = input()
URL = 'https://api.oikolab.com/weather'
OIKO_KEY = 'YOUR_OIKO_KEY_HERE'
resp = requests.get(URL,
params = {
'param': ['temperature'],
'start': date,
'location': city,
'end': date,
'api-key': OIKO_KEY,
'freq': 'D'
}
)
weather_data = resp.json()['data']
weather_data = json.loads(weather_data)
df = pd.DataFrame(index=pd.to_datetime(weather_data['index'],unit='s'),
data=weather_data['data'],
columns=weather_data['columns'])
temp = int(df.iloc[0,4])
print(f"Temperature for {city} on {date} = {temp}C")
Milestone II
import json
import pandas as pd
import requests
import datetime
def get_temperature_variance(city, start_date, end_date):
URL = 'https://api.oikolab.com/weather'
OIKO_KEY = 'YOUR_OIKO_KEY_HERE'
resp = requests.get(URL,
params = {
'param': ['temperature'],
'start': start_date,
'location': city,
'end': end_date,
'api-key': OIKO_KEY,
'freq': 'D'
}
)
weather_data = resp.json()['data']
weather_data = json.loads(weather_data)
df = pd.DataFrame(index=pd.to_datetime(weather_data['index'],unit='s'),
data=weather_data['data'],
columns=weather_data['columns'])
variance = df['temperature (degC)'].var()
return variance
city1 = input()
city2 = input()
start_date = input() # '2022-12-02'
start_date = start_date.split('-') # ['2022', '12', '02']
obj1 = datetime.datetime(int(start_date[0]), int(start_date[1]), int(start_date[2]))
obj2 = obj1 + datetime.timedelta(days=7)
start_date = obj1.strftime('%y-%m-%d')
end_date = obj2.strftime('%y-%m-%d')
var1 = get_temperature_variance(city1, start_date, end_date)
var2 = get_temperature_variance(city2, start_date, end_date)
if var1 < var2:
print(f"We choose {city1} because of less temperature variance")
else:
print(f"We choose {city2} because of less temperature variance")