0

I'm very new to Python and Flask and I can't figure out why I don't have the list of data available to me here (ranked_list is empty).

If I comment out all the Flask parts and just call getDataFromQuery to get the data like a normal script, I can see the data and print it out. Can anyone see what I'm doing wrong? The data is a list of tuples. The index.html page is below the code and it is in the templates folder. All I get is a blank table with the header row.

from flask import Flask
from flask import render_template
from flask import request
from queryProcessing_and_Ranking import *

app = Flask(__name__)

@app.route("/")
@app.route("/<query>")
def index():
    query = request.args.get("query")

    Processing = QueryProcessing()
    ranked_list = Processing.getDataFromQuery( query )
    return render_template( "index.html", ranked_list = ranked_list, user_input = query )

if __name__ == '__main__':
    port = int( os.environ.get('PORT', 5000 ) )
app.run( host='0.0.0.0', port=port, debug=True)
<html>
    <head>
        <title>Product Vertical Search Engine</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
    </head>
    <body>
        <form>
            Product Search: <input type="text" name="query"> <input type="Submit" value="Search">
        </form>
        <h1>Ranked product listing for: {{user_input}}</h1>
        <table border = "1">
            <tr>
                <th>Title</th><th>Price</th>
            </tr>
            {% for item in ranked_list %}
                <tr>
                    <td>{{item[0]}}</td>
                </tr>
            {% endfor %}
        </table>
    </body>
</html>
poke
  • 369,085
  • 72
  • 557
  • 602
Sam
  • 25
  • 4
  • Please add the code from `Processing.getDataFromQuery`, otherwise there is no way to know what ranked_list actually is. Also, you could try outputting ranked_list directly in your template: `{{ranked_list}}`, without any assumptions on its type and content. – YSelf Dec 18 '17 at 09:13
  • shouldn't your form come with `method="GET" ` and `action={{url_for('index', query=query)}}` or something like that? – senaps Dec 18 '17 at 09:13
  • The data was a list of tuples [(a,b,c)]. I figured out the issue was with flask itself. Something must have been corrupted. I was doing this in a VM so I went back to an earlier snapshot and re-installed flask and the page was now working. – Sam Dec 18 '17 at 21:14

1 Answers1

2

Given your route setup query is likely None and thus you are passing None to your .getDataFromQuery method.

@app.route('/')
def index():

    '''
    Returns "None" if not found.
    So when you open your browser to localhost:5000
    this value is None
    unless you visit localhost:5000/something
    then query = "something"
    '''
    query = request.args.get('query') 
    Processing = QueryProcessing()

    ranked_list = Processing.getDataFromQuery(query) # Value could be None
    return render_template( "index.html", ranked_list = ranked_list, user_input = query )

You should also remove your route definition that captures <query> as it looks like you are mixing up the concept of path parameters and query string parameters

EDIT

This looks like what you are trying to do is search on a form submission so I'd do the following

@app.route('/')
def index():

    user_input = None
    ranked_list = None
    if request.method == 'POST':
        user_input = request.form['query']
        Processing = QueryProcessing()
        ranked_list = Processing.getDataFromQuery(user_input)

    return render_template("index.html", ranked_list=ranked_list, user_input=user_input) 

HTML file

<html>
    <head>
        <title>Product Vertical Search Engine</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
    </head>
    <body>
        <form>
            Product Search: <input type="text" name="query"> <input type="Submit" value="Search">
        </form>
        {% if user_input %} <!-- See if you have searched yet -->
        <h1>Ranked product listing for: {{user_input}}</h1>
        <table border = "1">
            <tr>
                <th>Title</th><th>Price</th>
            </tr>
            {% for item in ranked_list %}
                <tr>
                    <td>{{item[0]}}</td>
                </tr>
            {% endfor %}
        </table>
        {% else %} <!-- tell user to search for data or that there is no data -->
        <h1>Search for data</h1>
        {% endif %}
    </body>
</html> 

Use some Jinja2 logic to explicitly state that nothing is there for a bit better feedback

Adam
  • 3,992
  • 2
  • 19
  • 39
  • Thanks that was very useful. One edit I am making is the end if. I think it needs to be endif (at least that is what worked for me) – Sam Dec 20 '17 at 01:42