我正在编写一个Web应用程序,其中我必须从目录中一张一张地显示图像,并且只有在我完成从一个图像中的响应收集之后,我才能显示下一个图像,即我只能在点击下一个图像时显示下一个图像。按钮。
@app.route("/", methods=["GET", "POST"])
def index():
# set session for image results
if "file_urls" not in session:
session["file_urls"] = []
# list to hold our uploaded image urls
file_urls = os.listdir("./static/memes")
print(file_urls)
if request.method == "POST":
session["file_urls"] = file_urls
return redirect(url_for("results"))
if request.method == "GET":
return render_template("index.html")
return render_template("index.html")
我在这里获取图像路径并将其存储在列表中。
@app.route("/results", methods=["GET", "POST"])
def results():
form = annotationform()
if "file_urls" not in session or session["file_urls"] == []:
return redirect(url_for("index"))
images = session["file_urls"]
session.pop("file_urls", None)`
for image in images:
directory = "./static/memes"
image = os.path.join(directory, image)
if request.method == "GET":
# redirect to home if no images to display
# set the file_urls and remove the session variable
return render_template("results.html", file_urls=image,
form=form)
if request.method == "POST":
csv_list = []
csv_list.append(form.sarcasam.data)
csv_list.append(form.humour.data)
csv_list.append(form.Offence.data)
with open("data.csv", "a") as csvFile:
writer = csv.writer(csvFile)
writer.writerow(csv_list)
csvFile.close()
print(form.sarcasam.data)
print(form.humour.data)
print(form.Offence.data)
if(len(images) > 0):
return redirect(url_for("results"))
else:
return redirect(url_for("results"))
我尝试遍历它们。请帮助我。谢谢。
答案 0 :(得分:0)
您的问题是这个
for image in images:
# some code
return # something
当您在循环中执行return
语句时,它将中断循环,因此您将只返回一次,在这种情况下,仅返回第一张图像。
您将设计另一个程序流程。