如何从函数中获取数组?

How do I get an array out of a function?

我在 python 中使用 eel 到 运行 一个 html 前端,并希望显示包含来自 python 的数据的图表。为此,我需要获取我已经在 python 中为 js 格式化的数据。 (我对js的经验不多)

我想不出一种方法来全局定义 chart0,然后在函数中对其进行操作并保留更改。 我不能使用 return 因为我用数据从 python 调用这个函数,我想在 js 中结束。

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

您必须 return 函数中的值,这样您就可以调用您的函数,它会 return 值。

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

或者在你的情况下你可以像这样写得更短:

function get_chart0(ch0){
   return ch0;
}

console.log(eel.expose(get_chart0));

好的,我只需要异步函数。
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
}

希望这对像我今天早上一样绝望的初学者有所帮助。