简体   繁体   中英

Connect HTML page with Elasticsearch using Python flask

I am new to web development. I am trying to create a web page which will display index from elastic search database. I am using python flask for backend. I see html page and python console shows index. But I am not able to fetch index from HTML page.

I am not sure what could be the issue

Python code is as follows:

from flask import Flask,render_template, request
from elasticsearch import Elasticsearch

app = Flask(__name__)
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

doc1 = {"food": "Japanese", "spice_level": "moderate"}
doc2 = {"food": "Italian", "spice_level": "mild"}
doc3 = {"food": "Indian", "spice_level": "spicy"}
es.index(index="food", doc_type="spice_level", id=1, body=doc2)
resp = es.get(index="food", doc_type="spice_level", id=1)
print(resp)

@app.route('/')
def home():
    return render_template('index.html')

  app.route('/dashboard', methods=['GET', 'POST'])

if __name__ == '__main__':

 app.run(host='0.0.0.0', port=5000)

HTML code is as follows:

<!DOCTYPE html>
  <BODY bgcolor="cyan">
    <form method="GET" action="/dashboard">
     <center>
      <H1>Database UI </H1> <br>
      search here <input type = "text" name= "index" /> <br>
      <input type = "submit">
     </center>
    </form>
  </BODY>
</html>

Whenever I type a index name and click on search button, page gives me error as :

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

I cannot see any other error then this, and it's really difficult to debug with less information about error.

  1. why your /dashboard return 404 ?

    because lack of view function to response.

    app.route('/dashboard', methods=['GET', 'POST']) is invalid.

  2. How to access /dashboard of elascticsearch ?

    In your case, the simplest way is modify the index.html

<!DOCTYPE html>
  <BODY bgcolor="cyan">
    <form method="POST" action="http://localhost:9200/dashboard">
     <center>
      <H1>Database UI </H1> <br>
      search here <input type = "text" name= "index" /> <br>
      <input type = "submit">
     </center>
    </form>
  </BODY>
</html>

can you use this here? for Parse data from html to python code you need to have POST inside @app.route like this:

@app.route("/", methods=['GET', 'POST'])
def home():

    return render_template('index.html')

if you want to pase data into index.html you can use this here:

somedata = "variable string"

render_template('index.html', somedata=somedata)

inside index.html do {{ somedata }}

<!DOCTYPE html>
  <BODY bgcolor="cyan">
    <form method="POST" action="">
     <center>
      <H1>Database UI </H1> <br>
      <!-- it will show (variable string) -->
      {{ somedata }}
      search here <input type = "text" name= "index" /> <br>
      <input type = "submit">
     </center>
    </form>
  </BODY>
</html>

happy codeing.

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