0

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

2 Answers2

0

You have to return the value in your function, that way you can call your function and it will return the value instead.

let chart0 = [];
eel.expose(get_chart0);
function get_chart0(ch0){
  chart0 = ch0;
  return chart0; // --> return chart0
}
console.log(eel.expose(get_chart0)); // --> directly call your get_chart0 function instead

Or in your case you could write it shorter like this:

function get_chart0(ch0){
   return ch0;
}

console.log(eel.expose(get_chart0));
Luïs
  • 2,671
  • 1
  • 20
  • 33
  • I call get_chart0(data) from pyhton with data generated by python, to get the data to js. I found that a problem is that I need the data in js before python can call the function. [image](https://imgur.com/a/NP3G3ak) – KingJacker Oct 31 '19 at 15:00
  • My guess is that `eel.expose()` here is an asynchronous function. So the value isn't available immediatly, hence why `a` in your second console.log isn't updated yet, but the one called in your function is. – Luïs Oct 31 '19 at 15:10
  • Funny thing, I copy pasted your code and ran it just to see if it works and it turns out `console.log(eel.expose(get_chart0));` completely breaks eel... – KingJacker Oct 31 '19 at 15:39
  • Hmm weird, this is outside of the scope of your question of using a variable outside a function though, but I’ll see if I can help. What happens if you simply do `console.log(eel.expose);`? – Luïs Oct 31 '19 at 16:28
0

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.