我正在运行一个flask restful API(用于天气预报),flask_rest.py和一个Web应用程序以使用名为main.py的API。我正在尝试在名为sample.html的HTML文件中使用AJAX显示输出。但是由于某种原因,结果不会动态显示。 感谢您的帮助
flask_rest.py
df = df.groupby(['year','production_companies'])[['revenue']].max()
idx = df['revenue'].max(level=0).sort_values().index
i = pd.CategoricalIndex(df.index.get_level_values(0), ordered=True, categories=idx)
df.index = [i, df.index.get_level_values(1)]
df = df.sort_values(['year','revenue'], ascending=False)
main.py
revenue
year production_companies
2016 Studio Babelsberg 1153
Walt Disney Pictures 966
Universal Pictures 875
DC Comics 873
Twentieth Century 783
2015 DC Comics 745
Twentieth Century 543
Universal Pictures 433
2014 Twentieth Century 389
Village Roadshow Pictures 356
Columbia Pictures 349
The Kennedy/Marshall Company 320
sample.html
from flask import Flask, request, jsonify, Response
from flask_restful import Resource, Api, reqparse, abort
import pandas as pd
app = Flask(__name__)
api = Api(app)
df = pd.read_csv(r"C:\Users\jayad\Desktop\Grad\Cloud Computing\dailyweather.csv", index_col=False)
data = df.set_index('DATE').to_dict(orient='index')
data_dict = pd.DataFrame(df['DATE'])
data_dict['INDEX'] = 'DATE'
data_dict = data_dict.set_index('INDEX').to_dict(orient='records')
parser = reqparse.RequestParser()
parser.add_argument('DATE', type=int)
parser.add_argument('TMAX', type=float)
parser.add_argument('TMIN', type=float)
class Historical(Resource):
def get(self):
return data_dict
def post(self):
args = parser.parse_args()
val1 = args['DATE']
val2 = args['TMAX']
val3 = args['TMIN']
data[val1] = {"TMAX":val2, "TMIN":val3}
return data[val1], 500
class Spec_historical(Resource):
def get(self, num):
return data[num]
def delete(self, num):
del data[num]
return "DELETION DONE 300"
class forecast(Resource):
def get(self, num):
pred_dates={}
date = int(num)
pred = date - 10000
j=date
for i in range(7):
if date - i in data:
x = data.get(date - i)
v1 = x['TMAX'] + 20
v2 = x['TMIN'] + 20
pred_dates[j] = {'TMAX':v1, 'TMIN':v2}
else:
if pred - i in data:
x = data.get(pred - i)
v1 = x['TMAX'] + 10
v2 = x['TMIN'] + 10
pred_dates[j] = {'TMAX':v1, 'TMIN': v2}
j+=1
return pred_dates
api.add_resource(Historical, '/Historical/')
api.add_resource(Spec_historical,'/Historical/<int:num>')
api.add_resource(forecast,'/forecast/<int:num>')
if __name__=="__main__":
app.run(debug=True, port=5001)
帮助!!!
答案 0 :(得分:0)
您需要将type:'GET'
更改为type:'POST'
。
此外,<div>
标签需要结束标签
所以改变这个:
<div id="result"/>
对此:
<div id="result"></div>
如果您仍然遇到麻烦,请告诉我。