Search code examples
javascriptpythonflaskapexcharts

Passing a python list to Javascript list for Apexcharts


Im passing my python list to a html element and reading it with JS, but i get this error below.

    var test = document.querySelector('#my_variable').value
    document.write(test)

    var options = {
      series: [{
        name: "Desktops",
        data: test

enter image description here

enter image description here

enter image description here


Solution

  • When you move data between different programming languages, you use some kind of intermediary. When working with JavaScript, it's almost impossible to avoid using JSON, as that's it's native intermediary. You can use it in many more places, but that's beside the point.

    Python also natively supports JSON with it's json module. You should convert any data you have to a Python dictionary or list, which you can then turn into a JSON string using the module.

    You can then pass that string to the frontend, from where you can parse it with JavaScript's JSON, which should solve your problem, or at the very least get you closer to a solution.