将pandas数据框作为列表数组传递给javascript

时间:2020-01-06 07:56:58

标签: javascript php python pandas bokeh

这可能是一个非常简单的问题,但是我对javascript完全陌生,无法解决。

我正在创建一个散景图,该色图在单击时更新ColumnDataSource。 在javascript回调内部,我使用的是ajax调用,然后使用php从服务器获取更新的pandasdf,到目前为止,大部分工作正常且正常。

尝试将熊猫数据框作为json传递,但我一直失败。 我尝试过这里发布的几个答案,但是它们都以某种方式将数组作为字符串传递。

这是问题的简化再现。

submit.html 提交表单,然后从response.php中获取数组

<!DOCTYPE html>
<link rel="shortcut icon" href="favicon.ico">
<html>
   <body>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript">
$("document").ready(function(){
  $(".js-ajax-php-json").submit(function(){
    var data1 = {
      "action": "test"
    };
    data1 = $(this).serialize() + "&" + $.param(data1);
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "response.php",
      data: data1,
      success: function(data) {
        console.table(data)
        $(".the-return").html(
        "return: " + data["index"]
        );

        alert("Form submitted successfully.\nReturned json: " + data["index"]);
      }
    });
    return false;
  });
});
</script>


    <form action="return.php" class="js-ajax-php-json" method="post" accept-charset="utf-8">
      <input type="text" name="st_numbers" value="" placeholder="write something" />
      </select>
      <input type="submit" name="submit" value="Submit form"  />
    </form>

    <div class="the-return">
      [HTML is replaced when successful.]
    </div>

</body>
</html>

response.php

<?php
\\ a call to server-side python code to calculate and print( df.reset_index().to_dict('list'))
\\ $query = $_SERVER['QUERY_STRING'];
\\ exec($python." server_side_calc.py ".$query, $return)

\\ what it is doing is essentially the same as;
echo json_encode(["{'index': [0, 1, 2, 3], 'p': [10, 20, 30, 50], 'init': [0.5178, 0.11115, 0.1903, 0.026], '24': [0.385, 0.028, 0.104, -0.0052]}"]);
?>

这些程序将熊猫数据帧读取为字符串"{'index': [0, 1, 2, 3], 'p': [10, 20, 30, 50], 'init': [0.5178, 0.11115, 0.1903, 0.026], '24': [0.385, 0.028, 0.104, -0.0052]}" 我不知道最外层的[]放在哪里以及为什么不能将其识别为数组。

欢迎提出任何建议。

谢谢您的帮助。

Yui

0 个答案:

没有答案
相关问题