【发布时间】:2020-11-06 20:13:54
【问题描述】:
从烧瓶进口烧瓶 从烧瓶导入渲染模板,重定向,请求 从 flask_restful 导入资源,API 导入请求 进口泡菜
app = Flask(名称) api = Api(应用程序)
类 HelloWorld(资源): def post(自我): r = requests.get('https://samples.openweathermap.org/data/2.5/weather?zip=16802,us&appid=157d320b7d029e653c67902f982784ff')
json_object_r = r.json()
temp_k = float(json_object_r['main']['temp'])
temp_c = temp_k - 273.15
tempp_c = int(temp_c)
pickle_in = open("var.pickle", "wb")
tempp_c = pickle.load(pickle_in)
# pressure
p = requests.get('https://samples.openweathermap.org/data/2.5/weather?zip=16802,us&appid=157d320b7d029e653c67902f982784ff')
json_object_p = p.json()
press_k = float(json_object_p['main']['pressure'])
# wind
# speed
w = requests.get('https://samples.openweathermap.org/data/2.5/weather?zip=16802,us&appid=157d320b7d029e653c67902f982784ff')
json_object_w = w.json()
wind_k = float(json_object_w['wind']['speed'])
# gust
g = requests.get('https://samples.openweathermap.org/data/2.5/weather?zip=16802,us&appid=157d320b7d029e653c67902f982784ff')
json_object_g = g.json()
gust_g = float(json_object_g['wind']['gust'])
return {tempp_c}
api.add_resource(HelloWorld, '/')
如果 name == 'main': app.run(debug=True)
【问题讨论】:
-
试试
return json.dumps(tempp_c)。如果它不是 json 可序列化的 - 考虑一些二进制格式 - 你甚至可以发回一个腌制对象 - 如果端点是用于应用程序的。 -
如何将腌制的物品送回?
标签: python json flask flask-restful