I'm new to Flask but I'm trying to show a 'rolling ball' while a page loads.
This link: Display a ‘loading’ message while a time consuming function is executed in Flask has been helpful but not giving me the desired results.
from flask import Flask
from flask import request
from flask import render_template
import time
app = Flask(__name__)
def long_load(typeback):
time.sleep(5) #just simulating the waiting period
return "You typed: %s" % typeback
@app.route('/', methods=("POST", "GET"))
def test():
if request.method == 'GET':
return render_template('index.html')
elif request.method == 'POST':
query = request.form['anything']
outcome = long_load(query)
return render_template("post_template.html" , display=outcome)
if __name__ == '__main__':
app.run()
Excerpts from index.html:
<head>
<style>
div#loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 2s linear infinite ;
}
/* Safari */
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
<script type="text/javascript">// <![CDATA[
document.onreadystatechange = function() {
if (document.readyState !== "complete") {
document.querySelector("body").style.visibility = "hidden";
document.querySelector("#loader").style.visibility = "visible";
} else {
document.querySelector("#loader").style.display = "none";
document.querySelector("body").style.visibility = "visible";
}
};
// ]]></script>
</head>
<form action="." method="post">>
<body>
<div class="caption">
<table class="center">
<tr>
<td class="NEONpinktxt"> </td>
<td align = "center"> <input type="submit" name="anything_submit" href="#" value="Search Results" id="loader" > </td>
</tr>
<div id="loader"></div>
</table>
</div>
</body>
</form>
When the page loads or refreshes, the rolling ball shows but when the 'Search Results' is clicked on, nothing happens.