Search code examples
javascriptpythoneel

How do I get an array out of a function?


I am using eel in python to run a html frontend and want to display a graph with data from python. For this I need to get my data, which I already formated for js in python. (I don't have a lot of experience with js)

I can't figure out a way to define chart0 globably and then manipulate it in a function and keep the changes. I can't use return because I call this function from python with the data, that I want to end up in js.

let chart0 = [];
eel.expose(get_chart0);
function get_chart0(ch0){
  chart0 = ch0;
  console.log(chart0); // --> correct output
}
console.log(chart0); // --> empty

Solution

  • Ok so all I needed are async functions.
    python:

    eel.init('web')
    
    @eel.expose()
    def get_chart0():
        return chart0
    
    eel.start('index.html')
    

    js:

    async_chart0();
    async function async_chart0(){
      let a = await eel.get_chart0()();
      console.log(a); 
    
      //whatever you need a for
    }
    

    Hope this helps some hopeless beginner like I was this morning.