1

I'm trying to submit a form, where the user can potentially upload several files, from which a background process is triggered that will take several minutes to complete. The issue I have currently is that when the user clicks the Submit button, nothing appears to happen for the user for several seconds while the files upload. Is there a way I can redirect to a (static*) holding page while the files upload and the processing happens, and then once the background process completes, this renders a new template?

Using Python 3.6.1 and Flask 0.12.2.

* I say static for now, but at some point in the future, I wish to use AJAX to update this page to give more information to the user

N.B. I have seen several similar questions, and the general answer is to use a module like Celery to perform async operations. Unfortunately, I do not have access to Celery as I cannot download new packages due to corporate policy.

main.py:

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

app = Flask(__name__, static_url_path = "/static", static_folder = "static")

@app.route("/", methods=['GET'])
def home():
    return render_template("index.html")


@app.route("/in_progress", methods=['GET', 'POST'])
def in_progress():
    if request.method == 'GET':

        # Do the background processing here

        return render_template('result.html') # This is the final part of the process

    elif request.method == 'POST':

        # Upload the files and do some pre-processing with the form
        # This takes a couple of seconds so appears to not be doing anything for the user
        # Want to render the GET 'in_progress.html' template at the top of this to give a better user experience

        return render_template('in_progress.html') # Reloads same page using GET


index.html:

...some irrelevant HTML

    <form action="{{ url_for('in_progress') }}" method="POST" id="form" name="form" enctype="multipart/form-data">

...other irrelevant HTML

A.Logan
  • 23
  • 4
  • Just FYI, Flask 0.12.2 is from May 2017. See the [changelog](https://flask.palletsprojects.com/en/1.1.x/changelog/#version-1-1-1) for all the bugs fixed since then. – v25 Jan 20 '20 at 16:37
  • Thanks @v25, but I'm tied down by my work's corporate policy, and I'm unable to update any packages. I've requested more recent versions of all the libraries that I use, but that may be 6+ months before that happens. Is there anything specifically in a newer version that will resolve my issue? – A.Logan Jan 20 '20 at 17:58
  • That policy makes no sense unless they're meticulously auditing the code you write. What's to stop you including a lightweight lib like `rq` directly in your code? I feel your pain, but this probably isn't the place for further discussion on that :-( You may wish to check [this SO thread](https://stackoverflow.com/questions/22615475/flask-application-with-background-threads) which has a number of answers that may avoid 3rd party libs. Your milage may vary. – v25 Jan 20 '20 at 18:21

0 Answers0