0

[What I want to do]

  1. use danfo.js to read a csv file from a url
  2. assign the danfo.js DataFrame (similar to that in pandas) as created above to a global variable, so that I can use it somewhere else for other purpose, such as plotting charts.

[What I did] I created an async function to read the csv file:

const url =
    "https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv"; 

async function readCSV(url) {
    try {
        const df = await dfd.readCSV(url);

        console.log("df describe: ");
        df.describe().print(); // print df describe as a table

    } catch (error) {
        console.error(error);
    }
}

readCSV(url)

[What's the issue] It seems that the DataFrame created from reading the csv file can only be used inside this async function.

[My question] How to get the DataFrame out of the async function and assign it to a global variable?

[What I tried] I also tried the following by adding a return in the readCSV function. But this seems cumbersome because every time I want to use the DataFrame, I have to call the function to read the csv again.

async function readCSV(url) {
    try {
        const df = await dfd.readCSV(url);
        return df;
    } catch (error) {
        console.error(error);
    }
}

readCSV(url).then((df) => {
    console.log("df head: ");
    df.head().print(); 
});

[reference I read] How to return values from async functions using async-await from function? How do I return the response from an asynchronous call?

oat
  • 1
  • "*How to assign the DataFrame to a global variable outside of this async function?*" - that's trivial, just assign it to a variable declared in the global scope. "*…so that I can use it somewhere else for other purpose, such as plotting charts.*" - that's less easy, since you cannot use it *immediately*, you'd have to until the variable has been assigned. Solution: store the promise itself in the global variable. – Bergi Nov 27 '22 at 13:11
  • "*every time I want to use the DataFrame, I have to call the function to read the csv again.*" - but why do you want to print the dataframe multiple times? Even if you need multiple charts, what's wrong with creating them all at once inside the single `.then()` handler? – Bergi Nov 27 '22 at 13:13
  • @Bergi, those .print() are just to check the description and head of the DataFrame. You can treat them as place holders in the code posted above. As you suggested, I figured out that I can save the `promise` returned from the async function of reading csv in a variable, and use `.then()` to process this `promiss` whenever I want and access the DataFrame inside it. But I'd like to know how to save this DataFrame inside this `promise` as a danfo.js DataFrame object without using `.then()`. – oat Nov 27 '22 at 14:27
  • You cannot do it without `.then()`. The two canonical questions you read should have made that clear. What is wrong with using it? It would help if you would show your actual code not just placeholders, at least wrt how/where/when you are using the `df` (not what you're doing with it) and how often. – Bergi Nov 27 '22 at 14:32
  • @Bergi Noted with thanks. I'll go back to read the canonical questions on this topic. The CodeSandBox link here (I'm unable to get it work on CodePen) shows my test code in which I'm using `.then()` as suggested to call the `promise` returned from read CSV file, check the dataframe inside, and create a line chart using one of the columns: https://codesandbox.io/s/test-danfojs-hy5xuv – oat Nov 28 '22 at 08:15
  • Yeah, both approaches from that sandbox work – Bergi Nov 28 '22 at 13:31

0 Answers0