在flask中显示用户输入的结果

时间:2018-04-06 11:01:40

标签: python flask output

我可以通过以下脚本接受用户输入并运行脚本,有人可以告诉我如何在脚本运行后输出结果吗?

app = Flask(__name__)

这是脚本本身。

def elastic_search(text):
    es = Elasticsearch(['10.0.0.9:9200', '10.0.0.15:9200'])
    word = input(text)
    res = es.search(index="pastebin-*", doc_type='doc', body={"query": {"bool": {"must": [{"match": {"key": word}}]}}})
    return res

这是接受输入并运行脚本的函数

@app.route('/key', methods=['POST', 'GET'])
def key():
    if requests.method == 'POST', 'GET':
       result = request.form['key']
       elastic_search(result)
return render_template('key.html')

这是我希望在脚本运行后显示脚本结果的函数。

@app.route('/result', methods=['POST', 'GET'])
def result():
    if request.method == 'POST':
       result = request.form
    return render_template('result.html', result = result)

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

并且继承了结果页面的html -

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Results</title>


    <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">

    <link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet">
    <link href="../static/result.css" rel="stylesheet">

  </head>

  <body>
  <table border = 1>
     {% for key, value in result.res() %}

            <tr>
               <th> {{ resp }} </th>
            </tr>

         {% endfor %}
     </table>

      </form>
      {% if error %}
        <p class="error"><strong>Error:</strong> {{ error }}
      {% endif %}
      </div>

      <footer class="footer">
        <p>&copy; Intel</p>
      </footer>

    </div>
  </body>
</html>

这是key.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Key</title>


    <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="styleshee$>

    <link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet">
    <link href="../static/key.css" rel="stylesheet">

  </head>

  <body>

    <div class="container">
      <div class="header">
        <h3 class="text-muted"><Key/h3>
      </div>

      <div class="jumbotron">
        <h1>Please Enter Pastebin Key</h1>
        <form class="form-signin">
        <form action="" method"post">
        <label for="Key" class="sr-only">Key</label>
        <input type="text" name="inputKey" id="inputKey" class="form-control" placeholder="Enter key here" required value="{{request.form.key }}">
        <input type="submit" value="Submit">
      </form>
      {% if error %}
        <p class="error"><strong>Error:</strong> {{ error }}
      {% endif %}
      </div>


      <footer class="footer">
        <p>&copy; Intel</p>
      </footer>


</html>

我知道这很麻烦,但我很难接受烧瓶而且不知所措,试图弄错了代码。我已阅读大量的教程,但没有一个帮助过我。

1 个答案:

答案 0 :(得分:1)

鉴于信息不明确。我能想到这就是你想要的东西

 # Render key form 
 @app.route('/key', methods=['POST', 'GET'])
 def key():
    return render_template('key.html')

您需要设置key.html表单来调用结果路由。例如localhost:8000/result

 # then you gather data form 
 @app.route('/result', methods=['POST', 'GET'])
 def result():
 if request.method == 'POST':
   key_data= request.form['key']
   result=elastic_search(key_data)
   return render_template('result.html', result = result)

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

你的result.html - 假设你的结果是字典返回

  <!DOCTYPE html>
<html lang="en">
  <head>
    <title>Results</title>


    <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">

    <link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet">
    <link href="../static/result.css" rel="stylesheet">

  </head>

  <body>
  <table border = 1>
     {% for key, value in result.items() %}

            <tr>
               <th> {{key value}} </th>
            </tr>

         {% endfor %}
     </table>

      </form>
      {% if error %}
        <p class="error"><strong>Error:</strong> {{ error }}
      {% endif %}
      </div>

      <footer class="footer">
        <p>&copy; Intel</p>
      </footer>

    </div>
  </body>
</html>