Display a ‘loading’ message while a time consuming function is executed in Flask

后端 未结 3 781
一个人的身影
一个人的身影 2020-12-02 08:39

I’m still relatively new to Flask, and a bit of a web noob in general, but I’ve had some good results so far. Right now I’ve got a form in which users enter a query, which i

相关标签:
3条回答
  • 2020-12-02 08:53

    Add this to your index.html or js file (I'm assuming you have jQuery here, you could use standard javascript of course.):

    <script type="text/javascript">// <![CDATA[
            function loading(){
                $("#loading").show();
                $("#content").hide();       
            }
    // ]]></script>
    

    Add this to you html or css file:

    div#loading {
        width: 35px;
        height: 35px;
        display: none;
        background: url(/static/loadingimage.gif) no-repeat;
        cursor: wait;
        }
    

    You can get an adequate GIF from http://www.ajaxload.info/. Download and put it into your static folder.

    Then change your submission button to call above js function:

    <input type="submit" name="anything_submit" value="Submit" onclick="loading();">
    

    and add in a loading and a content div to you base html file:

    <body>
        <div id="loading"></div>
        <div id="content">
            <h3>Type anything:</h3>
            <p>
            <form action="." method="POST">
                <input type="text" name="anything" placeholder="Type anything here">
                <input type="submit" name="anything_submit" value="Submit" onclick="loading();">
            </form>
            </p>
        </div>    
    </body>
    

    Now when you click 'Submit', the js function should hide your content and display a loading GIF. This will display until your data is processed and flask loads the new page.

    0 讨论(0)
  • 2020-12-02 09:10

    This can be done by using a div that contains a 'loading gif' image. When the submit button is clicked, the div is displayed using javascript. To implement this, you can take a look at this website: http://web.archive.org/web/20181023063601/http://www.netavatar.co.in/2011/05/31/how-to-show-a-loading-gif-image-while-a-page-loads-using-javascript-and-css/

    0 讨论(0)
  • 2020-12-02 09:14

    I found the purely CSS-dependent loader very useful. It does not depend on external resources:

    https://www.w3schools.com/howto/howto_css_loader.asp

    0 讨论(0)
提交回复
热议问题