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>
答案 0 :(得分:2)
鉴于您的路线设置query
很可能None
,因此您将None
传递给.getDataFromQuery
方法。
@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 )
您还应删除捕获<query>
的路线定义,因为您似乎正在混淆path parameters和query string parameters的概念
修改强>
这看起来像你要做的是搜索表单提交,所以我会做以下
@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文件
<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>
使用一些Jinja2逻辑明确声明没有任何东西可以提供更好的反馈