I have some buttons on my flask app, that when clicked trigger a function on a different route. I want to add a loading gif when the page is waiting for the function to complete. I have tried a dozen examples from SO but I've had no luck. Note,I am on bootstrap 3.3.7 so I can't use the
HTML: for route /dashboard (button lives here)
{% block styles %}
<!-- Bootstrap latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Custom CSS -->
<link href="{{ url_for('static', filename='css/custom-base.css') }}" rel="stylesheet">
<!-- Font Awesome-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Pretty loading buttons -->
<link href="{{ url_for('static', filename='css/loading-btn.css') }}" rel="stylesheet">
{% endblock %}
<!-- run function-->
<div class="container">
<div class="jumbotron">
<h2>Run Model</h2>
<div class="container" align="left">
<a href="/run_model" target="">
<button class="btn btn-primary ">Run</button></a>
</div>
</div>
</div>
{% block scripts %}
<!-- JQuery -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Bootstrap JS -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
{% endblock %}
Routes: the button links to the run_model function, executes and redirects to the dashboard page.
{% block content %}
@app.route('/run_model')
def run_model():
try:
# run a machine learning mode
except:
# if errors found flash some error
flash('Looks like something went wrong. Please try again! :(', 'danger')
return redirect(url_for('dashboard'))
{% endblock %}
So essentially, I am looking to animate the button while still on the /dashboard page when the browser is "Waiting for 127.0.0.1:5000/run_model
to complete and be redirected back to /dashboard
So this is a javascript issue, and not really a flask issue. After you click on a link, and get redirected, all javascript/css is killed. So after clicking on a button, first display your loading icon, and then redirect to whatever page you want:
Full example:
Modified from: https://www.w3schools.com/howto/howto_css_loader.asp