Python Flask,渲染模板后重定向
Python Flask, redirect after rendering a template
我正在尝试在呈现模板后重定向用户。本质上,我的代码会等到变量变为 False,然后重定向用户。该代码呈现加载动画并告诉用户等待。代码呈现 html 和 css 模板,但是当等待结束时不会将用户重定向到新的 url。我的代码:
from flask import Flask, redirect, url_for, request, render_template
import json
import requests
import webbrowser
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
global is_busy
is_busy = False
app = Flask(__name__, template_folder='template')
@app.route('/')
def main_menu():
return redirect(url_for('enteruserid'))
@app.route('/EnterID')
def enteruserid():
global is_busy
return render_template('enterid.html')
def waitUntil(condition, output):
time.sleep(1)
@app.route('/Scrape', methods=['POST', 'GET'])
def login():
global is_busy
if request.method == 'POST':
id = request.form['nm']
try:
int(id)
return render_template('waitingscreen.html')
waitUntil(is_busy, False)
return redirect(url_for('scrapingtheinternet', target_id=id))
except:
return redirect(url_for('notanint'))
else:
id = request.args.get('nm')
if __name__ == '__main__':
app.run(debug=True)
此代码不包含发生“抓取”的函数。
enterid.html:
<!DOCTYPE html>
<html>
<head>
<title>DCF</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Reverse image search discord user id's">
<meta name="keywords" content="Discord">
<style>
h1 {
font-family: Arial, sans-serif;
color: #2f2d2d;
text-align: Center;
}
p {
font-family: Arial, sans-serif;
font-size: 14px;
text-align: Center;
color: #2f2d2d;
}
</style>
</head>
<body>
<form action = "http://localhost:5000/Scrape" method = "post">
<h1>Reverse Image Search</h1>
<p>Enter User ID: </p>
<p><input type = "text" name = "nm" /></p>
<p><input type = "submit" value = "submit" /></p>
</body>
</html>
waitingscreen.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Loader Bar</title>
<style>
body {
background-color: #262626;
font-family: Lato, sans-serif;
}
.loader {
width: 150px;
margin: 150px auto 70px;
position: relative;
}
.loader .loading_1 {
position: relative;
width: 100%;
height: 10px;
border: 1px solid yellowgreen;
border-radius: 10px;
animation: turn 4s linear 1.75s infinite;
}
.loader .loading_1:before {
content: "";
display: block;
position: absolute;
width: 0;
height: 100%;
background-color: yellowgreen;
box-shadow: 10px 0px 15px 0px yellowgreen;
animation: load 2s linear infinite;
}
.loader .loading_2 {
position: absolute;
width: 100%;
top: 10px;
color: green;
font-size: 22px;
text-align: center;
animation: bounce 2s linear infinite;
}
@keyframes load {
0% {
width: 0%;
}
87.5% {
width: 100%;
}
}
@keyframes turn {
0% {
transform: rotateY(0deg);
}
6.25%,
50% {
transform: rotateY(180deg);
}
56.25%,
100% {
transform: rotateY(360deg);
}
}
@keyframes bounce {
0%,
100% {
top: 10px;
}
12.5% {
top: 30px;
}
}
</style>
</head>
<body>
<div class="loader">
<div class="loading_1"></div>
<div class="loading_2">Please Wait...</div>
</div>
</body>
</html>
如有任何帮助,我将不胜感激。谢谢:)
基本上,return语句在几乎所有语言中结束函数。
说
def func():
print("hi")
return 0
print("after return")
func()
输出:
hi
return之后的任何内容都不会被执行(尽管参见关于finally
)。
在您的上下文中,您可以尝试在单击登录按钮时在前端预览“waitingscreen.html”
并避免从后端渲染它。
一些简单的 JS 会有所帮助。
参考:Display a ‘loading’ message while a time consuming function is executed in Flask
希望它能清除您的查询。
我正在尝试在呈现模板后重定向用户。本质上,我的代码会等到变量变为 False,然后重定向用户。该代码呈现加载动画并告诉用户等待。代码呈现 html 和 css 模板,但是当等待结束时不会将用户重定向到新的 url。我的代码:
from flask import Flask, redirect, url_for, request, render_template
import json
import requests
import webbrowser
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
global is_busy
is_busy = False
app = Flask(__name__, template_folder='template')
@app.route('/')
def main_menu():
return redirect(url_for('enteruserid'))
@app.route('/EnterID')
def enteruserid():
global is_busy
return render_template('enterid.html')
def waitUntil(condition, output):
time.sleep(1)
@app.route('/Scrape', methods=['POST', 'GET'])
def login():
global is_busy
if request.method == 'POST':
id = request.form['nm']
try:
int(id)
return render_template('waitingscreen.html')
waitUntil(is_busy, False)
return redirect(url_for('scrapingtheinternet', target_id=id))
except:
return redirect(url_for('notanint'))
else:
id = request.args.get('nm')
if __name__ == '__main__':
app.run(debug=True)
此代码不包含发生“抓取”的函数。
enterid.html:
<!DOCTYPE html>
<html>
<head>
<title>DCF</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Reverse image search discord user id's">
<meta name="keywords" content="Discord">
<style>
h1 {
font-family: Arial, sans-serif;
color: #2f2d2d;
text-align: Center;
}
p {
font-family: Arial, sans-serif;
font-size: 14px;
text-align: Center;
color: #2f2d2d;
}
</style>
</head>
<body>
<form action = "http://localhost:5000/Scrape" method = "post">
<h1>Reverse Image Search</h1>
<p>Enter User ID: </p>
<p><input type = "text" name = "nm" /></p>
<p><input type = "submit" value = "submit" /></p>
</body>
</html>
waitingscreen.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Loader Bar</title>
<style>
body {
background-color: #262626;
font-family: Lato, sans-serif;
}
.loader {
width: 150px;
margin: 150px auto 70px;
position: relative;
}
.loader .loading_1 {
position: relative;
width: 100%;
height: 10px;
border: 1px solid yellowgreen;
border-radius: 10px;
animation: turn 4s linear 1.75s infinite;
}
.loader .loading_1:before {
content: "";
display: block;
position: absolute;
width: 0;
height: 100%;
background-color: yellowgreen;
box-shadow: 10px 0px 15px 0px yellowgreen;
animation: load 2s linear infinite;
}
.loader .loading_2 {
position: absolute;
width: 100%;
top: 10px;
color: green;
font-size: 22px;
text-align: center;
animation: bounce 2s linear infinite;
}
@keyframes load {
0% {
width: 0%;
}
87.5% {
width: 100%;
}
}
@keyframes turn {
0% {
transform: rotateY(0deg);
}
6.25%,
50% {
transform: rotateY(180deg);
}
56.25%,
100% {
transform: rotateY(360deg);
}
}
@keyframes bounce {
0%,
100% {
top: 10px;
}
12.5% {
top: 30px;
}
}
</style>
</head>
<body>
<div class="loader">
<div class="loading_1"></div>
<div class="loading_2">Please Wait...</div>
</div>
</body>
</html>
如有任何帮助,我将不胜感激。谢谢:)
基本上,return语句在几乎所有语言中结束函数。
说
def func():
print("hi")
return 0
print("after return")
func()
输出:
hi
return之后的任何内容都不会被执行(尽管参见finally
)。
在您的上下文中,您可以尝试在单击登录按钮时在前端预览“waitingscreen.html” 并避免从后端渲染它。
一些简单的 JS 会有所帮助。
参考:Display a ‘loading’ message while a time consuming function is executed in Flask
希望它能清除您的查询。