To setup a simple html front-end and python flask back-
- Create a html script (index.html) and save D:\Projects\test_backgroundtask\templates\views
<html>
<section>
<div>
<h>Test background task</h>
</div>
</section>
</html>
- Create a python script and save D:\Projects\test_backgroundtask:
Code
import pandas as pd
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
return render_template('views/index.html')
@app.route('/post', methods=['POST'])
def post():
return "recived: {}".format(request.form)
if __name__ == "__main__":
app.run( port = '5004')
To create a background task, flask has a package called flask_executor Updated python script using excutor to create a background
from flask import Flask, render_template, request
import pandas as pd
from flask_executor import Executor
global input_val
app = Flask(__name__)
def background_task_func(input_val):
if input_val==1:
data = {'Name': ['Tom', 'Joseph', 'Krish', 'John'], 'Age': [20, 21, 19, 18]}
test_val= pd.DataFrame(data)
print(test_val)
@app.route('/', methods=['GET'])
def index():
global input_val
input_val=1
executor.submit(background_task_func,input_val)
return render_template('views/index.html')
@app.route('/post', methods=['POST'])
def post():
return "recived: {}".format(request.form)
if __name__ == "__main__":
executor = Executor(app)
app.run( port = '5004')
Required Output:
Once the results are completed, the table containing test_val should be displayed.
NB: the html page index.html is currently being viewed by the user and it has other tables already displayed. The user is also interacting with other buttons on this page.
Additional Info
NB: I use Django within <script>...</script>
in my html script.
An example of how I used it to toggle a table or button:
<script>
function myFunctionDemoFeat() {
var x = document.getElementById("demofeatDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>