-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
46 lines (32 loc) · 1.42 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
45
46
from datetime import datetime
from flask import Flask, render_template
import pandas as pd
app = Flask(__name__)
stations = df = pd.read_csv("data_small/stations.txt", skiprows=17)
stations = stations[["STAID", "STANAME "]]
@app.route("/")
def home():
return render_template("home.html", data=stations.to_html())
@app.route("/api/v1/<station>/<date>")
def about(station, date):
filepath = "data_small/TG_STAID" + str(station).zfill(6) + ".txt"
df = pd.read_csv(filepath, skiprows=20, on_bad_lines='skip', 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):
filepath = "data_small/TG_STAID" + str(station).zfill(6) + ".txt"
df = pd.read_csv(filepath, skiprows=20, on_bad_lines='skip', parse_dates=[" DATE"])
result = df.to_dict(orient="records")
return result
@app.route("/api/v1/yearly/<station>/<year>")
def all_data_single_year(station, year):
filepath = "data_small/TG_STAID" + str(station).zfill(6) + ".txt"
df = pd.read_csv(filepath, skiprows=20, on_bad_lines='skip')
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)