Accessing a loaded Table in drop()












2















I am trying to load a simple CSV file in my sketch through the drop() function in p5. I can successfully 'get' the file and call loadTable(), however I would like to do something with the loaded table automatically, and for some reason it seems like I have to let the drop() function completely finish before being able to access the table.



My little test sketch 'gets' a file that is dragged onto the canvas, loads it into a table, and attempts to print out the getRowCount() immediately after loading. This returns 0.... so I also set up a function to run getRowCount() when the mouse is clicked, and this works as expected.



My test CSV file: https://drive.google.com/open?id=1NOluhKiqMxZy10s3dAFLsHLLjoAtV6GT



I only partially understand why this is happening, and I definitely don't know how to get around it. I've been teaching myself Javascript and p5, so I don't know the terms that I need to search to understand what is happening here...



var myTable;

function setup() {
var canvas = createCanvas(400, 400);
canvas.drop(getFile);
}

function draw() {
background(220);
}

function getFile(file) {
myTable = loadTable(file.data);

// Do something with the table *when* I drop it...
console.log("In getFile function: " + myTable.getRowCount());

// Doesn't work either...
extra(myTable);
}

function mouseClicked() {
console.log("On mouse click " + myTable.getRowCount());
}

function extra(table_) {
console.log("In extra function: " + table_.getRowCount());
}









share|improve this question





























    2















    I am trying to load a simple CSV file in my sketch through the drop() function in p5. I can successfully 'get' the file and call loadTable(), however I would like to do something with the loaded table automatically, and for some reason it seems like I have to let the drop() function completely finish before being able to access the table.



    My little test sketch 'gets' a file that is dragged onto the canvas, loads it into a table, and attempts to print out the getRowCount() immediately after loading. This returns 0.... so I also set up a function to run getRowCount() when the mouse is clicked, and this works as expected.



    My test CSV file: https://drive.google.com/open?id=1NOluhKiqMxZy10s3dAFLsHLLjoAtV6GT



    I only partially understand why this is happening, and I definitely don't know how to get around it. I've been teaching myself Javascript and p5, so I don't know the terms that I need to search to understand what is happening here...



    var myTable;

    function setup() {
    var canvas = createCanvas(400, 400);
    canvas.drop(getFile);
    }

    function draw() {
    background(220);
    }

    function getFile(file) {
    myTable = loadTable(file.data);

    // Do something with the table *when* I drop it...
    console.log("In getFile function: " + myTable.getRowCount());

    // Doesn't work either...
    extra(myTable);
    }

    function mouseClicked() {
    console.log("On mouse click " + myTable.getRowCount());
    }

    function extra(table_) {
    console.log("In extra function: " + table_.getRowCount());
    }









    share|improve this question



























      2












      2








      2








      I am trying to load a simple CSV file in my sketch through the drop() function in p5. I can successfully 'get' the file and call loadTable(), however I would like to do something with the loaded table automatically, and for some reason it seems like I have to let the drop() function completely finish before being able to access the table.



      My little test sketch 'gets' a file that is dragged onto the canvas, loads it into a table, and attempts to print out the getRowCount() immediately after loading. This returns 0.... so I also set up a function to run getRowCount() when the mouse is clicked, and this works as expected.



      My test CSV file: https://drive.google.com/open?id=1NOluhKiqMxZy10s3dAFLsHLLjoAtV6GT



      I only partially understand why this is happening, and I definitely don't know how to get around it. I've been teaching myself Javascript and p5, so I don't know the terms that I need to search to understand what is happening here...



      var myTable;

      function setup() {
      var canvas = createCanvas(400, 400);
      canvas.drop(getFile);
      }

      function draw() {
      background(220);
      }

      function getFile(file) {
      myTable = loadTable(file.data);

      // Do something with the table *when* I drop it...
      console.log("In getFile function: " + myTable.getRowCount());

      // Doesn't work either...
      extra(myTable);
      }

      function mouseClicked() {
      console.log("On mouse click " + myTable.getRowCount());
      }

      function extra(table_) {
      console.log("In extra function: " + table_.getRowCount());
      }









      share|improve this question
















      I am trying to load a simple CSV file in my sketch through the drop() function in p5. I can successfully 'get' the file and call loadTable(), however I would like to do something with the loaded table automatically, and for some reason it seems like I have to let the drop() function completely finish before being able to access the table.



      My little test sketch 'gets' a file that is dragged onto the canvas, loads it into a table, and attempts to print out the getRowCount() immediately after loading. This returns 0.... so I also set up a function to run getRowCount() when the mouse is clicked, and this works as expected.



      My test CSV file: https://drive.google.com/open?id=1NOluhKiqMxZy10s3dAFLsHLLjoAtV6GT



      I only partially understand why this is happening, and I definitely don't know how to get around it. I've been teaching myself Javascript and p5, so I don't know the terms that I need to search to understand what is happening here...



      var myTable;

      function setup() {
      var canvas = createCanvas(400, 400);
      canvas.drop(getFile);
      }

      function draw() {
      background(220);
      }

      function getFile(file) {
      myTable = loadTable(file.data);

      // Do something with the table *when* I drop it...
      console.log("In getFile function: " + myTable.getRowCount());

      // Doesn't work either...
      extra(myTable);
      }

      function mouseClicked() {
      console.log("On mouse click " + myTable.getRowCount());
      }

      function extra(table_) {
      console.log("In extra function: " + table_.getRowCount());
      }






      javascript p5.js import-from-csv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 15:51









      Brian Tompsett - 汤莱恩

      4,2321338102




      4,2321338102










      asked Nov 20 '18 at 14:44









      markersniffenmarkersniffen

      133




      133
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I'd recommend always looking in the reference when you have questions about how P5.js works.



          Here is the reference for the loadTable() function. It says:




          This method is asynchronous, meaning it may not finish before the next line in your sketch is executed. Calling loadTable() inside preload() guarantees to complete the operation before setup() and draw() are called.



          Outside of preload(), you may supply a callback function to handle the object:



          Syntax



          loadTable(filename, options, [callback], [errorCallback])
          loadTable(filename, [callback], [errorCallback])



          You would need to provide a callback function that is triggered when the loadTable() function is finished asynchronously loading the table.






          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',
            autoActivateHeartbeat: false,
            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%2f53395505%2faccessing-a-loaded-table-in-drop%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









            0














            I'd recommend always looking in the reference when you have questions about how P5.js works.



            Here is the reference for the loadTable() function. It says:




            This method is asynchronous, meaning it may not finish before the next line in your sketch is executed. Calling loadTable() inside preload() guarantees to complete the operation before setup() and draw() are called.



            Outside of preload(), you may supply a callback function to handle the object:



            Syntax



            loadTable(filename, options, [callback], [errorCallback])
            loadTable(filename, [callback], [errorCallback])



            You would need to provide a callback function that is triggered when the loadTable() function is finished asynchronously loading the table.






            share|improve this answer




























              0














              I'd recommend always looking in the reference when you have questions about how P5.js works.



              Here is the reference for the loadTable() function. It says:




              This method is asynchronous, meaning it may not finish before the next line in your sketch is executed. Calling loadTable() inside preload() guarantees to complete the operation before setup() and draw() are called.



              Outside of preload(), you may supply a callback function to handle the object:



              Syntax



              loadTable(filename, options, [callback], [errorCallback])
              loadTable(filename, [callback], [errorCallback])



              You would need to provide a callback function that is triggered when the loadTable() function is finished asynchronously loading the table.






              share|improve this answer


























                0












                0








                0







                I'd recommend always looking in the reference when you have questions about how P5.js works.



                Here is the reference for the loadTable() function. It says:




                This method is asynchronous, meaning it may not finish before the next line in your sketch is executed. Calling loadTable() inside preload() guarantees to complete the operation before setup() and draw() are called.



                Outside of preload(), you may supply a callback function to handle the object:



                Syntax



                loadTable(filename, options, [callback], [errorCallback])
                loadTable(filename, [callback], [errorCallback])



                You would need to provide a callback function that is triggered when the loadTable() function is finished asynchronously loading the table.






                share|improve this answer













                I'd recommend always looking in the reference when you have questions about how P5.js works.



                Here is the reference for the loadTable() function. It says:




                This method is asynchronous, meaning it may not finish before the next line in your sketch is executed. Calling loadTable() inside preload() guarantees to complete the operation before setup() and draw() are called.



                Outside of preload(), you may supply a callback function to handle the object:



                Syntax



                loadTable(filename, options, [callback], [errorCallback])
                loadTable(filename, [callback], [errorCallback])



                You would need to provide a callback function that is triggered when the loadTable() function is finished asynchronously loading the table.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 17:41









                Kevin WorkmanKevin Workman

                34.1k54172




                34.1k54172
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395505%2faccessing-a-loaded-table-in-drop%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







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)