简体   繁体   中英

List generated from class is not available returning render_template in flask

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>

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

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