简体   繁体   中英

Displaying results from user input in flask

From what i can gether the script below is accepting user input and running the script, can someone please tell me how i can output the results from the script once its run?

app = Flask(__name__)

this is the script itself.

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

this is the function that accepts the input and runs the script

@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')

this is the function that i want to display the results of the script once its been run.

@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)

and heres the html for the results page -

<!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>

Here is 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>

I understand this is messy but im new to flask and am at a loss trying to whats wrong with the code. Ive read loads of tutorials and none of them have helped me.

Given the information which is not clear. i can think this is what you want

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

You need set key.html form to call results route . eg 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)

your result.html - assuming you result is dictionary returned

  <!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM