You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

52 lines
2.0 KiB

from flask import Flask, render_template, request, json
import requests
# import urllib.request
# from urllib.request import urlopen
app = Flask(__name__)
@app.route("/")
def index():
# return ['data', 'AAA']
return render_template("weather-form.html")
@app.route("/datasTaipei", methods=['GET', 'POST'])
def datas():
if request.method == 'POST':
r = requests.get("http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-061?Authorization=CWB-6B764FB3-BD6E-4198-96C0-43FB4EBA1375&limit=10&offset=0&format=JSON&elementName=T&sort=time")
list_of_dicts = r.json()
list_of_dicts2 = list_of_dicts["records"]["locations"][0]
list_of_dicts3 = list_of_dicts2["location"]
location = list_of_dicts3[0]["locationName"]
location_name = list_of_dicts3[0]
list_of_dicts5 = location_name["weatherElement"][0]
list_of_dicts6 = list_of_dicts5["time"]
list_of_dicts7 = list_of_dicts6[0]["dataTime"]
Tv = list_of_dicts6[0]["elementValue"][0]["measures"]
Tm = list_of_dicts6[0]["elementValue"][0]["value"]
print(Tm)
return render_template('weather_data.html', name=location, weather=list_of_dicts7, T=Tv, Tm=Tm )
@app.route("/datasChiayi", methods=['GET', 'POST'])
def datasTaichung():
r = requests.get("http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-061?Authorization=CWB-6B764FB3-BD6E-4198-96C0-43FB4EBA1375&limit=10&offset=0&format=JSON&elementName=T&sort=time")
list_of_dicts = r.json()
list_of_dicts2 = list_of_dicts["records"]
return list_of_dicts2
@app.route("/datasKaohsiung", methods=['GET', 'POST'])
def datasKaohsiung():
r = requests.get("http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-061?Authorization=CWB-6B764FB3-BD6E-4198-96C0-43FB4EBA1375&limit=10&offset=0&format=JSON&elementName=T&sort=time")
list_of_dicts = r.json()
list_of_dicts2 = list_of_dicts["records"]
return list_of_dicts2
if __name__ == "__main__":
app.run(debug=True)