我尝试在网址(' / hello ')中的网址( / predict )中呈现变量('预测')。我是网络开发的初学者。如果有人知道,请帮助。
app = Flask(__name__)
@app.route('/predict', methods=['POST'])
def apicall(responses = None):
test_json = request.get_json()
test = pd.read_json(test_json, orient='records')
query_df = pd.DataFrame(test)
clf = 'kmeans__model.pkl'
print("Loading the model...")
lin_reg_model = None
with open(clf,'rb') as f:
lin_reg_model = joblib.load(f)
# lin_reg_model = joblib.load('/home/q/new_project/models/kmeans_model.pkl')
print("The model has been loaded...doing predictions now...")
predictions = lin_reg_model.predict(test)
print(predictions)
prediction_series = list(pd.Series(predictions))
final_predictions = pd.DataFrame(list(zip(prediction_series)))
responses = jsonify(predictions=final_predictions.to_json(orient="records"))
responses.status_code = 200
return (responses)
@app.route('/hello')
def hello():
**What should be used here to render *predictions*?**
return 'Hello, World '
答案 0 :(得分:0)
如果您愿意,可以返回模板并以html格式显示。创建templates
文件夹并创建html文件。
import requests
def hello():
response = requests.get(url_for('apicall'))
return render_template('<yourtemplate.html>', predictions=response.json())
<强> HTML 强>
{{ predictions }}
注意:如果线程和部署Handling multiple requests in Flask
出现任何问题,请点击此链接