如何将数据框表格编码为 JSON。

huangapple go评论57阅读模式
英文:

how to encode a dataframe table to json

问题

这是您要翻译的代码部分:

    def drawtieringchart(service='All'):
        fig = create_tiering_scatter(service)
        graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
        generallogger.info(graphJSON)
        return graphJSON

我想要这样的东西

    def drawtieringchart(service='All'):
        fig = create_tiering_scatter(service)
        graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
        df = somedata
        dfJson = json.dumps(df, cls=ENCODER HERE?
        final_JSON = COMBINE BOTH JSONS
        return final_JSON

不要翻译的部分已经去除,只提供翻译好的部分。

英文:

I have a flask function that I create a plotly chart then pass via plotly encoder.

Is there something similar for datatables library? I want to pass both the fig and a table object into my json for a javascript function to parse later.

Here is my current code:

def drawtieringchart(service='All'):
    fig = create_tiering_scatter(service)
    graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
    generallogger.info(graphJSON)
    return graphJSON

I want something like this:

def drawtieringchart(service='All'):
    fig = create_tiering_scatter(service)
    graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
    df = somedata
    dfJson = json.dumps(df, cls=ENCODER HERE?
    final_JSON = COMBINE BOTH JSONS
    return final_JSON

答案1

得分: 0

以下是您要求的代码的中文翻译部分:

这是我解决问题的方法

def drawtieringchart(service='All'):
    fig = create_tiering_scatter(service)
    graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
    df = tiering_table(service)
    dfJson = df.to_json(orient='records')
    merged_dict = {
        "graphJSON": json.loads(graphJSON),
        "dfJson": json.loads(dfJson)
    }
    final_JSON = json.dumps(merged_dict)
    return final_JSON
在我的jQuery `$.getJSON` 语句中我引用了每个对象列表 `result.graphJSON`  `result.dfJson`

<script>
    // 渲染图表的回调函数
    function cb(selection) {
        displayloadingcontainer("loaderContainer");
        $.getJSON({url: "/tieringcallback", data: { 'data': selection }})
            .done(result => {
            Plotly.react('chart', result.graphJSON, {responsive: true});
            $('#tiering_table').DataTable().clear().destroy();
            $('#tiering_table tbody').append(result.dfJson.map(item =>
                $('<tr>').append($('<td>').text(item.Name), $('<td>').text(item['Primary Service']), $('<td>').text(item.Value), $('<td>').text(item.Risk))
            ));
            $('#tiering_table').DataTable({});
            })
            .always(() => hideloadingcontainer("loaderContainer"));
    }
</script>
英文:

Here is how I solved it:

def drawtieringchart(service=&#39;All&#39;):
    fig = create_tiering_scatter(service)
    graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
    df=tiering_table(service)
    dfJson = df.to_json(orient=&#39;records&#39;)
    merged_dict = {
        &quot;graphJSON&quot;: json.loads(graphJSON),
        &quot;dfJson&quot;: json.loads(dfJson)
    }
    final_JSON = json.dumps(merged_dict)
    return final_JSON

In my jquery $.getJSON statement I reference each object list this result.graphJSON or result.dfJson:

&lt;script&gt;
    // callback function for rendering chart
    function cb(selection) {
        displayloadingcontainer(&quot;loaderContainer&quot;);
        $.getJSON({url: &quot;/tieringcallback&quot;, data: { &#39;data&#39;: selection }})
            .done(result =&gt; {
            Plotly.react(&#39;chart&#39;, result.graphJSON, {responsive: true});
            $(&#39;#tiering_table&#39;).DataTable().clear().destroy();
            $(&#39;#tiering_table tbody&#39;).append(result.dfJson.map(item =&gt;
                $(&#39;&lt;tr&gt;&#39;).append($(&#39;&lt;td&gt;&#39;).text(item.Name), $(&#39;&lt;td&gt;&#39;).text(item[&#39;Primary Service&#39;]), $(&#39;&lt;td&gt;&#39;).text(item.Value), $(&#39;&lt;td&gt;&#39;).text(item.Risk))
            ));
            $(&#39;#tiering_table&#39;).DataTable({});
            })
            .always(() =&gt; hideloadingcontainer(&quot;loaderContainer&quot;));
    }
  &lt;/script&gt;

huangapple
  • 本文由 发表于 2023年2月24日 09:00:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定