I’m writing a Flask application that takes user input (supposed ‘name’) using the html form. On the other hand, I have a function that makes http requests to a webpage and return the results (supposed ‘name’ : ‘pass/ongoing/fail’).
If the person from the user input is in the data return from http request, I will display it on the html page like …
Name | Result |
---|---|
Alex | Pass |
Jim | Ongoing |
Rose | Ongoing |
The question is… I’d like to make a http request every X minutes to track the result. I prefer not reloading the whole webpage but only the function or process. I tried to attempt using python apscheduler module and threading, however, I couldn’t make it work due to my code structure.
# Http requests
def request_data() -> list:
output:list = [str]
data = requests.get("www.website.com")
output.append(data)
return output
# Main route
@app.route("/", methods=["GET", "POST"])
def index():
final_data: list = [str]
if request.method == "POST":
input_data = request.form.get("input_textbox")
data_from_http_request = request_data()
for x in input_data:
if x in data_from_http_request:
final_data.append(x)
return render_template("index.html", data = final_data)
Above is my code sample. By using apscheduler module, I tried following but does not work.
from apscheduler.schedulers.background import BackgroundScheduler
import atexit
scheduler = BackgroundScheduler()
scheduler.add_job(request_data, 'interval', seconds=5)
scheduler.start()
atexit.register(lambda: scheduler.shutdown())
I also google it and found ways to use AJAX, JS to get it working but I couldn’t follow since the code structure they use is totally different from mine.
Thanks for any help!