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

2024-04-25 17:42:42 发布

您现在位置:Python中文网/ 问答频道 /正文

据我所知,下面的脚本正在接受用户输入并运行脚本,有人能告诉我如何在脚本运行后输出结果吗?你知道吗

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>

这里是密钥.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>

我知道这是混乱的,但我是新的烧瓶,并在一个不知所措的尝试什么是错误的代码。我读了很多教程,但没有一本对我有帮助。你知道吗


Tags: keydivform脚本appifhtmllink
1条回答
网友
1楼 · 发布于 2024-04-25 17:42:42

给出了不清楚的信息。我想这就是你想要的

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

您需要设置key.html表单来调用结果路由。e、 g.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)

你的结果.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>

相关问题 更多 >