-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
44 lines (32 loc) · 1.32 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
43
44
from flask import Flask, render_template
import pandas as pd
app = Flask(__name__)
stations = pd.read_csv("data_small/stations.txt", skiprows=17)
stations = stations[["STAID", "STANAME "]]
# Homepage
@app.route("/")
def home():
return render_template("home.html", data=stations.to_html())
@app.route("/api/v1/<station>/<date>")
def api(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}
@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
@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)