0

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
  • Please edit your question and fix the indentation of your python code. Commented May 17, 2019 at 9:39

2 Answers 2

1
  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>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.