Flask - 将反馈数据存储到服务器 [英] Flask - Store feedback data to server
问题描述
from flask import Flask,render_template,request
将pandas导入为pd
pd.set_option('display .max_colwidth',-1)
app = Flask(__ name__)
@ app.route(/)
def index():
返回render_template('index.html')
@ app.route(/ submit,methods = ['GET','POST'])
def submit_table():
if request.method =='POST':
如果request.form ['submit'] =='开始!':
last_month_txn = pd.read_csv(./ Predicted_Reoccuring_Payments.csv )
last_month_txn = last_month_txn [[processing_dt,narrative,transaction_amt,trans_type_desc]]
if __name__ =='__main__':
app.run(port = 5050,threaded = True)
submit_table函数将在熊猫数据框中读取并发送到页面。这在用户登录时有效。我没有包含登录表单。我有一个引导模式,如果一个人想要对表中的一个条目提出异议,就会提出这个模式。
< form method =POSTaction =submit>
< div class =modal fade modal-adminid =myModalrole =dialog>
< div class =modal-dialog modal-lg>
<! - 模态内容 - >
< div class =modal-content>
< div class =modal-header>
< button type =buttonclass =closedata-dismiss =modal>& times;< / button>
< / div>
< div class =modal-body>
< h4>请提供有关此交易的反馈信息< / h4>
< div class =form-groupstyle =padding:20px;>
< label for =text_area>其他信息< / label>
< / div>
< div class =modal-footer>
< button type =buttonclass =btn btn-primaryid =submit_modaldata-dismiss =modal>提交< / button>
< button type =buttonclass =btn btn-primarydata-dismiss =modal>关闭< / button>
< / div>
< / div>
< / div>
< / div>
我需要记录这些数据,特别是textarea的内容,转换成服务器上的某个文件。我不希望网页在提交时重定向/刷新。
有没有一种方法让烧瓶得到这个表单的内容,而不会影响网址/网址?
我认为您必须在JavaScript中使用Ajax请求。你可以用jquery(这很容易)或经典的javascript来完成。
编辑:你可以在google上找到一些文档!
小例:
$。ajax({
类型:'POST',
url:'/ path_url',
dataType:json,
contentType:'application / json; charset = UTF-8',
data:JSON.stringify(dict_var),
success:function(result){
console.log(result)
},
error:function(error){
console.log(error);
}
});
使用dict_var这是一个字典,您只需从表单中恢复数据(非常简单)
在烧瓶中,您只需要读取路线中的数据:
<$ p $
$ data $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b
和平
I am using a flask application as the server. I simply return a users transactions. However, I need to allow the user to give the server feedback if a transaction was wrong.
from flask import Flask, render_template, request
import pandas as pd
pd.set_option('display.max_colwidth', -1)
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/submit", methods=['GET','POST'])
def submit_table():
if request.method == 'POST':
if request.form['submit'] == 'Get Started!':
last_month_txn = pd.read_csv("./Predicted_Reoccuring_Payments.csv")
last_month_txn = last_month_txn[["processing_dt", "narrative","transaction_amt", "trans_type_desc"]]
if __name__ == '__main__':
app.run(port=5050, threaded=True)
The submit_table function will read in a pandas dataframe and send it to the page. This works when a user logs in. I have not included the log in form. I have a bootstrap modal which is brought up if a person wants to dispute an entry in the table.
<form method = "POST" action="submit">
<div class="modal fade modal-admin" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title" name="feedback">Submit Feedback</h3>
</div>
<div class="modal-body">
<h4>Please provide feedback about this transaction</h4>
<div class="form-group" style="padding:20px;">
<label for="text_area">Additional information</label>
<textarea class="form-control mywidth" id="text_area" rows="3" name="text_area"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="submit_modal" data-dismiss="modal">Submit</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
I need to log this data, specifically the contents of the textarea, into some file on the server. I don't want the webpage to redirect/refresh when they hit submit. Is there a way for flask to get the contents of this form without affecting the url/web address?
I think you have to use the Ajax Request in javascript. You can do it with Jquery (which is pretty easy) or in classic javascript.
Edit : You can find some docs on google !
Small exemple :
$.ajax({
type : 'POST',
url : '/path_url',
dataType : "json",
contentType: 'application/json;charset=UTF-8',
data : JSON.stringify(dict_var),
success : function (result) {
console.log(result)
},
error : function (error) {
console.log(error);
}
});
With dict_var which is a dictionnary, you just need to recover data from your form (pretty simple) and send it through dictionnary.
And in flask, you just need to read data in your route:
data = json.loads(request.data)
tmp = data["key"]
Peace
这篇关于Flask - 将反馈数据存储到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!