Skip to content
Advertisement

Python Flask Executor | How to pass results from a background task to a currently active html page

To setup a simple html front-end and python flask back-

  1. Create a html script (index.html) and save D:Projectstest_backgroundtasktemplatesviews

JavaScript
  1. Create a python script and save D:Projectstest_backgroundtask:

Code

JavaScript

To create a background task, flask has a package called flask_executor Updated python script using excutor to create a background

JavaScript

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:

JavaScript

Advertisement

Answer

Python script

JavaScript

Html code

JavaScript

For a simple test example , this method will be fine. However, if there are other data being displayed, the disadvantage of using this method is that I need to refresh the entire page, this results in reloading the previous tables. The alternative solution is to use sockets. My solution in Flask socketio | Emitting a pandas dataframe from a background task using Flask executor to an Html script resolve this issue.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement