Option 1: No Javascript
I just want to display a static template that says 'please wait...' while a task is running. Here is a simplified version of the route function:
@app.route('/import/towers/<case_id>/<filename>', methods=['GET', 'POST'])
def import_towers(case_id=None, filename=None):
case_number = TollsCase.get_case_number(case_id)
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb') as f:
csv_f = csv.reader(f)
headers = next(csv_f)
if flask.request.method == 'POST':
# show static progress screen here then process records in csv file
for row in csv_f:
# process record
return 'success'
It won't let me use render_template
without returning, which skips the processing I need done. I need it to display a progress screen while the process runs then show 'success' when done. How do I do this?
Option 2: Javascript
If that can't be done and Javascript must be used, then this is what I've tried so far:
After the html, I have this in the template:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="application/javascript">
$('form').on('submit', function() {
$('#content').empty();
$('#content').html('please wait...');
})
</script>
It works fine for replacing the content to show a progress page on submit, but now the form is not submitted for server-side handling. I tried setting data-ajax="false"
in the form attributes but that didn't resolve the issue. I also tried simply using this.submit();
before clearing the content but that makes it wait on the server-side processing, thus defeating the point of a progress/wait screen. I still need the form data submitted for server-side handling if I use Javascript.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…