Forums

Showing a loading message while python function runs

Hi , does anyone know a clean an easy way of displaying a load message (flash or css loaders) which could be displayed while a python function (which dynamically builds html upon POST request) executes code ? I've tried adding : flash("processing request") but it doesn't seem to do anything , i've looked at javascript solutions but i'm not experienced enough and would like to avoid venturing into this territory. I'm looking a for a simple solution which would just tell the user in a clear way to wait while something is happening. Thanks

You can do the processing outside of the usual request-response cycle by running that code in a separate task: http://help.pythonanywhere.com/pages/AsyncInWebApps/ and then use ajax in the browser to query the progress of the task and show that to the user.

Thanks for taking a look Glenn, the solution you proposed might be a bit to tricky for a newbie like me. I've reverted to trying to employ javascript (the idea is to have a hidden 'loading' gif on my start page and show it after a click of an 'execute' button and leave it displayed after the dynamically constructed html/results are ready). I've pasted below a simplified version where i'm just trying to call a javascript function which would hide the 'textarea' before I return the 'done' html (delay represents the long function I will be running to get the real html) . Clearly i'm missing something here because clicking the button does not seem to call my javascript function, all that happens is a15 sec wait followed by display of 'done' html - any idea what am I doing wrong ?

from flask import Flask, request

app = Flask(name) app.config["DEBUG"] = True

if name == "main": app.run(debug = True)

@app.route("/", methods=["GET", "POST"]) def home_page():

if request.method == "POST":

    import time
    time.sleep(15)


    return "<html>done</html>"

return """

                <html>
                <head>
                <script LANGUAGE="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
                function hide_text()
                {var ele = document.getElementById("text1");
                ele.style.display = "none";
                }
                </script>
                </head>


                <textarea id="text1" rows="15" cols="100" ></textarea>
                <form method="post">
                <p><button onclick="hide_text();" value="Run" >Run </button></p>
                </form>
       """

You can use the browser developer tools to see whether the function is actually called (You can add a breakpoint in the function and see whether the breakpoint gets triggered)