英文:
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='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
In my jquery $.getJSON
statement I reference each object list this result.graphJSON
or result.dfJson
:
<script>
// callback function for rendering chart
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论