I’m trying to add a “loader” while my flask application runs a task.
Here is my HTML for for my button
JavaScript
x
21
21
1
{% extends "base.html" %}
2
{% block head %}
3
{# <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles/leaf.css') }}"> #}
4
{% endblock %}
5
{% block content %}
6
<div id="loader"></div>
7
<div id="vo_budget_file_settings">
8
{# <a href="/generatecleanbudgetfile" class="btn btn-primary">Upload Final CRO Budget File</a> #}
9
<p>Please upload the final CRO budget File</p>
10
<form class="" action="/generatecleanbudgetfile" method=POST enctype=multipart/form-data>
11
<input type="file" name="data_file" accept=".xls, .xlsx, .xlsm"/>
12
<input type="submit" value="Begin Format" onclick="loading();"/>
13
</form>
14
</div>
15
<script type="text/javascript">
16
function loading(){
17
$("#loader").show();
18
}
19
</script>
20
{% endblock %}
21
Here is the CSS that I’ve added:
JavaScript
1
14
14
1
#loader {
2
border: 16px solid #f3f3f3; /* Light grey */
3
border-top: 16px solid #3498db; /* Blue */
4
border-radius: 50%;
5
width: 120px;
6
height: 120px;
7
animation: spin 2s linear infinite;
8
}
9
10
@keyframes spin {
11
0% { transform: rotate(0deg); }
12
100% { transform: rotate(360deg); }
13
}
14
Any ideas on how to call this correctly?
Advertisement
Answer
Make it visible from the start, without ever showing it with javascript. Then hide it with js like so:
JavaScript
1
4
1
$(document).ready(() => {
2
$("#loader").hide();
3
})
4
EDIT: I now realize you said on button click in the title. I am assuming you want to hide the loading screen on button click? If not correct me in a comment. Otherwise, use this code:
JavaScript
1
4
1
$("#button").click(() => {
2
$("#loader").hide();
3
})
4
P.S.
Your script should be at the bottom of the body, and your CSS and JS (and my code) reference #loader
, when the body has <div id="loading">