1

我的 Flask 应用程序根据用户表单输入进行计算。这些计算大约需要 10 秒才能完成。这些计算的输出需要显示在同一页面上的 div 中,在表单旁边(在图表/表格等中)。

我尝试了两种方法。第一个,使用普通的 Flask,重新加载整个页面,这远非理想。第二种方法,使用 Sijax,只更新 div。但在这种情况下,我不知道如何访问表单输入。

我很困惑如何让这个工作。将不胜感激任何方向!

方法1:只是烧瓶(缺点:整个页面重新加载)

form_test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Testpage</title>
</head>
<body>
    <form  action="{{ url_for('do_the_math') }}" method="post">
        A = <input type="number" name="input_A">
        B = <input type="number" name="input_B">
        <input type="submit" value="Submit">
    </form>
<div id="destination_div">A + B = {{ result }}</div>
</body>
</html>

app_normal.py:

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route("/")
def show_home():
    return render_template("form_test.html", result='unknown yet')

@app.route("/do_the_math", methods=['POST'])
def do_the_math():
    A = request.form.get('input_A')
    B = request.form.get('input_B')
    sum = float(A) + float(B)

    # reloads whole page
    return render_template("form_test.html", result=sum)

    # what i want: reload just update destination_div with new HTML
    # return render_template("#destination_div", "A + B = " + str(sum))

if __name__ == '__main__':
    app.run(debug=True)

方法 2:使用 Sijax(更新 div,但如何访问表单输入?)

form_test_sijax.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Testpage with sijax</title>
    <script type="text/javascript" src="/static/js/sijax/sijax.js"></script>
    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
</head>
<body>
    <form method="post">
        A = <input type="number" name="input_A">
        B = <input type="number" name="input_B">
        <button type="button" onclick="Sijax.request('submit_form');">calc</button>
    </form>
<div id="destination_div">A + B = unknown</div>
</body>
</html>

app_sijax.py

from flask import Flask, render_template, g
import flask_sijax
import os

app = Flask(__name__)

# init sijax
app.config["SIJAX_STATIC_PATH"] = os.path.join('.', os.path.dirname(__file__), 'static/js/sijax/')
app.config["SIJAX_JSON_URI"] = '/static/js/sijax/json2.js'
flask_sijax.Sijax(app)


def submit_form_handler(obj_response):
    A = 5  # how do get to the values entered in the form?
    B = 3
    sum = A + B
    obj_response.html("#destination_div", "A + B = " + str(sum))


@flask_sijax.route(app, "/")
def show_home():
    result = 'unknown'
    if g.sijax.is_sijax_request:
        g.sijax.register_callback('submit_form', submit_form_handler)
        return g.sijax.process_request()
    return render_template("form_test_sijax.html")


if __name__ == '__main__':
    app.run(debug=True)
4

1 回答 1

8

您可以使用ajaxwithjquery使用计算结果动态更新页面,而无需刷新页面:

在 html 文件中:

<html>
  <header>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  </header>
 <body>
    <div class='wrapper'>
      A = <input type="number" id="input_A">
      B = <input type="number" id="input_B">
      <button class='get_result'>Calculate</button>
      <div class='result'></div>
    </div>
 </body>
 <script>
    $(document).ready(function(){
      $('.wrapper').on('click', '.get_result', function(){
         var val1 = $("#input_A").val();
         var val2 = $("#input_B").val();
         $.ajax({
          url: "/calculate_result",
          type: "get",
          data: {val1: val1, val2:val2},
          success: function(response) {
            $(".result").html('<p>'+response.result.toString()+'</p>');
          },
         });
      });
    });
 </script>
</html>

然后,在主应用程序文件中,创建计算最终结果的路由:

@app.route('/calculate_result')
def calculate_result():
  a = int(flask.request.args.get('val1'))
  b = int(flask.request.args.get('val2'))
  return flask.jsonify({"result":a+b})
于 2018-09-15T15:03:01.073 回答