How to re-use expensive data in a WSGI app?











up vote
0
down vote

favorite












I built a WSGI page that contains both tabulated data and a scatterplot of the same data from a database. (I'm using flask and matplotlib but that doesn't matter). This generates two separate requests: One for the HTML page and one for the dynamically generated image called from the tag. Since the database is rather slow and since both requests need exactly the same data I'd like to make this work with just one SQL query. Two approaches come to mind:




  1. After querying the DB in the HTML view function, generate the scatterplot and save that in a PNG file somewhere. Then pass the tabulated data on to the template and serve up the cached PNG once the browser requests the image.


  2. Somehow embed the image data in the HTML itself and have the browser render it using Javascript.



Approach 1. is simple and straightforward, but I also need a way to get rid of the cached images when they are not needed any more. This is prone to get messy. Since the app is purely http-request driven I would have to scan my cache dir on each request and decide which file is old enough to be deleted. Alternatively I could have an "onload" javascript function call my app a third time to trigger deletion of the image. Maybe clever, but robust?




  1. I have no idea how to do this, let alone in a browser-compatible way.


Any suggestions?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I built a WSGI page that contains both tabulated data and a scatterplot of the same data from a database. (I'm using flask and matplotlib but that doesn't matter). This generates two separate requests: One for the HTML page and one for the dynamically generated image called from the tag. Since the database is rather slow and since both requests need exactly the same data I'd like to make this work with just one SQL query. Two approaches come to mind:




    1. After querying the DB in the HTML view function, generate the scatterplot and save that in a PNG file somewhere. Then pass the tabulated data on to the template and serve up the cached PNG once the browser requests the image.


    2. Somehow embed the image data in the HTML itself and have the browser render it using Javascript.



    Approach 1. is simple and straightforward, but I also need a way to get rid of the cached images when they are not needed any more. This is prone to get messy. Since the app is purely http-request driven I would have to scan my cache dir on each request and decide which file is old enough to be deleted. Alternatively I could have an "onload" javascript function call my app a third time to trigger deletion of the image. Maybe clever, but robust?




    1. I have no idea how to do this, let alone in a browser-compatible way.


    Any suggestions?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I built a WSGI page that contains both tabulated data and a scatterplot of the same data from a database. (I'm using flask and matplotlib but that doesn't matter). This generates two separate requests: One for the HTML page and one for the dynamically generated image called from the tag. Since the database is rather slow and since both requests need exactly the same data I'd like to make this work with just one SQL query. Two approaches come to mind:




      1. After querying the DB in the HTML view function, generate the scatterplot and save that in a PNG file somewhere. Then pass the tabulated data on to the template and serve up the cached PNG once the browser requests the image.


      2. Somehow embed the image data in the HTML itself and have the browser render it using Javascript.



      Approach 1. is simple and straightforward, but I also need a way to get rid of the cached images when they are not needed any more. This is prone to get messy. Since the app is purely http-request driven I would have to scan my cache dir on each request and decide which file is old enough to be deleted. Alternatively I could have an "onload" javascript function call my app a third time to trigger deletion of the image. Maybe clever, but robust?




      1. I have no idea how to do this, let alone in a browser-compatible way.


      Any suggestions?










      share|improve this question













      I built a WSGI page that contains both tabulated data and a scatterplot of the same data from a database. (I'm using flask and matplotlib but that doesn't matter). This generates two separate requests: One for the HTML page and one for the dynamically generated image called from the tag. Since the database is rather slow and since both requests need exactly the same data I'd like to make this work with just one SQL query. Two approaches come to mind:




      1. After querying the DB in the HTML view function, generate the scatterplot and save that in a PNG file somewhere. Then pass the tabulated data on to the template and serve up the cached PNG once the browser requests the image.


      2. Somehow embed the image data in the HTML itself and have the browser render it using Javascript.



      Approach 1. is simple and straightforward, but I also need a way to get rid of the cached images when they are not needed any more. This is prone to get messy. Since the app is purely http-request driven I would have to scan my cache dir on each request and decide which file is old enough to be deleted. Alternatively I could have an "onload" javascript function call my app a third time to trigger deletion of the image. Maybe clever, but robust?




      1. I have no idea how to do this, let alone in a browser-compatible way.


      Any suggestions?







      matplotlib flask wsgi






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 12:04









      musbur

      335




      335
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I've been on Usenet for 25 years and still posting a question is the best method to find the answer yourself after a few minutes:



          <img src="data:image/png;base64, {{imgdata}}">


          and in the view function:



          return flask.render_template('chart_page.html', imgdata=base64.b64encode(pixbuf))


          End of story. No javascript.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189129%2fhow-to-re-use-expensive-data-in-a-wsgi-app%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I've been on Usenet for 25 years and still posting a question is the best method to find the answer yourself after a few minutes:



            <img src="data:image/png;base64, {{imgdata}}">


            and in the view function:



            return flask.render_template('chart_page.html', imgdata=base64.b64encode(pixbuf))


            End of story. No javascript.






            share|improve this answer

























              up vote
              0
              down vote













              I've been on Usenet for 25 years and still posting a question is the best method to find the answer yourself after a few minutes:



              <img src="data:image/png;base64, {{imgdata}}">


              and in the view function:



              return flask.render_template('chart_page.html', imgdata=base64.b64encode(pixbuf))


              End of story. No javascript.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I've been on Usenet for 25 years and still posting a question is the best method to find the answer yourself after a few minutes:



                <img src="data:image/png;base64, {{imgdata}}">


                and in the view function:



                return flask.render_template('chart_page.html', imgdata=base64.b64encode(pixbuf))


                End of story. No javascript.






                share|improve this answer












                I've been on Usenet for 25 years and still posting a question is the best method to find the answer yourself after a few minutes:



                <img src="data:image/png;base64, {{imgdata}}">


                and in the view function:



                return flask.render_template('chart_page.html', imgdata=base64.b64encode(pixbuf))


                End of story. No javascript.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 7 at 12:33









                musbur

                335




                335






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189129%2fhow-to-re-use-expensive-data-in-a-wsgi-app%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings