-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
42 lines (34 loc) · 1.39 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flask import Flask, render_template
import pandas as pd
app = Flask(__name__)
stations = pd.read_csv("data_small/stations.txt", skiprows=17)
#homepage
@app.route("/")
def home():
return render_template("home.html", data=stations.to_html())
#show temperature based on station & date
@app.route("/api/v1/<station>/<date>")
def about(station, date):
filename = "data_small/TG_STAID" + str(station).zfill(6) + ".txt"
df = pd.read_csv(filename, skiprows=20, parse_dates=[" DATE"])
temperature = df.loc[df[" DATE"] == date][" TG"].squeeze() / 10
return {"station": station,
"date": date,
"temperature": temperature}
#show data for 1 particular station
@app.route("/api/v1/<station>")
def all_data(station):
filename = "data_small/TG_STAID" + str(station).zfill(6) + ".txt"
df = pd.read_csv(filename, skiprows=20, parse_dates=[" DATE"])
result = df.to_dict(orient="records")
return result
#show data for 1 particular year
@app.route("/api/v1/yearly/<station>/<year>")
def yearly(station, year):
filename = "data_small/TG_STAID" + str(station).zfill(6) + ".txt"
df = pd.read_csv(filename, skiprows=20)
df[" DATE"] = df[" DATE"].astype(str)
result = df[df[" DATE"].str.startswith(str(year))].to_dict(orient="records")
return result
if __name__ == "__main__":
app.run(debug=True)