简体   繁体   中英

Dynamically changing HTML whilst running python script in Flask

I want to create a webapp for my ML project. I have this code in flask:

from flask import Flask, render_template, request, url_for, flash, redirect

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route('/')
def index():
    
    return(
        render_template('index.html')


    )

The problem is that the index function returns the render_template. Instead, I want to run some python code from my ML project and at the same time update the HTML in index.html as the code runs.

For example, imagine you have some python code:

print('Reading text from csv file...')
dataframe = pd.read_csv('helo.csv')
print('Done reading...')

How can I have this code run in flask, but instead of printing; I change the innerHTML of a

tag in my main html file?

You could use XMLHttpRequest in javascript. So the browser will have the index.html loaded and than in the background. It could ask the server some data every few or so seconds (use setTimeOut for this). Once the server confirms it's done running the ML code. The browser could automatically react to it.

https://www.w3schools.com/xml/xml_http.asp

https://www.w3schools.com/jsref/met_win_settimeout.asp

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