2 回答
TA贡献1846条经验 获得超7个赞
你的toroman():函数应该返回带有参数的 index.html :
@app.route("/toroman", methods=['POST'])
def toroman():
arabic = request.form['arabic']
# some unnecessarily numerous lines of code that basically turn Arabic-system number to Roman system
return render_template("index.html", data = romanfinal)
data然后你可以像这样在你的 HTML 顶层使用这个值:{{data}}
TA贡献1725条经验 获得超7个赞
烧瓶
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/toroman", methods=['POST'])
def toroman():
arabic = request.data['arabic']
#pass arabic into your translation function
translation = translate()
#return JSON response to AJAX
return jsonify(translation = translation)
if __name__ == "__main__":
app.run(debug=True)
JS
$(document).ready(function(){
document.getElementById('toroman_form').addEventListener('keyup', function() {
$.ajax({
type: 'POST',
url: '/toroman', //flask route to which data is sent
data: $('#arabic').val(), // input field value
contentType:'application/json; charset=utf-8',
dataType: "json",
success: function(data) {
translation = data.translation //response received from flask
$('#translated').text(translation) //display translation in html <p> tag
},
error: function() {
alert("Transaction Failed");
}
});
});
}
HTML
<form id="toroman_form">
<input type="number" id="arabic">
<p id="translated"><!--translation is dislayed here--></p>
</form>
添加回答
举报