简体   繁体   English

散景,回调中的更改数据不会更改原始python数据

[英]bokeh, change data in callback does not change original python data

I am working on a tool for manual classification which changes the property of certain dot(color in my case) chosen in a scatter plot by bokeh.我正在开发一种手动分类工具,该工具更改散景散点图中选择的某些点(在我的情况下为颜色)的属性。 I changed the source data in callback by s.data = d2 and s.change.emit() but both failed.我通过 s.data = d2 和 s.change.emit() 在回调中更改了源数据,但都失败了。 I thought such operation will change source.data, but when I print source.data, actually nothing happens.我以为这样的操作会改变source.data,但是当我打印source.data时,实际上什么也没发生。 The dots' color in the plot changes as expected though.不过,图中点的颜色会按预期发生变化。 Here is my related code:这是我的相关代码:

DF = pd.read_csv(csv_path)
s = ColumnDataSource(DF_file)
p = figure(plot_width=500, plot_height=500, tooltips=TOOLTIPS,tools="lasso_select, tap", title="manual classification")
circles = p.circle('x', 'y', color='color', size=10, source=s, line_alpha=0.6,fill_alpha=0.6)
s.callback = CustomJS(args=dict(s1=s), code="""
    var inds = cb_obj.selected.indices;
    var d1 = s1.data;

    for (var i = 0; i < inds.length; i++) 
    {d1['color'][inds[i]] = 'green';} 

    s1.change.emit();
""")

Both print(s.data) and the csv file saved from s.to_csv(xxx) shows no change to the original input data. print(s.data) 和从 s.to_csv(xxx) 保存的 csv 文件都显示原始输入数据没有变化。 Also, I wonder how does callback work to change the plot's data while leave the data in python unchanged when the data in python is the data passed to it in args=(s1=s) .另外,我想知道当 python 中的数据是args=(s1=s)传递给它的数据时,回调如何工作来更改绘图的数据,同时保持 python 中的数据不变。

I have searched for some possible methods and found this answer in https://discourse.bokeh.org/t/getting-selected-glyph-data-properties-via-customjs/4311/5?u=1113我搜索了一些可能的方法,并在https://discourse.bokeh.org/t/getting-selected-glyph-data-properties-via-customjs/4311/5?u=1113 中找到了这个答案

when the Bokeh JS object is instantiated it uses the Python objects as sources for data but then is essentially disconnected from them - so updates to the JS model are not propagated back to their Python parents.当 Bokeh JS 对象被实例化时,它使用 Python 对象作为数据源,但实际上与它们断开连接 - 因此对 JS 模型的更新不会传播回它们的 Python 父对象。

While the discussion in this webpage also proposed a workaround using IPython.notebook.kernel.execute to create or overwrite a variable in the Python.虽然此网页中的讨论还提出了使用 IPython.notebook.kernel.execute 在 Python 中创建或覆盖变量的解决方法。 It can only be used in a notebook frontend(I found this workaround only works when use output_notebook() in the code).它只能用于笔记本前端(我发现此解决方法仅在代码中使用 output_notebook() 时才有效)。

Then here is my new code to change the original data in python:然后这是我在python中更改原始数据的新代码:

s.callback = CustomJS(args=dict(s1=s1), code="""
    var inds = s1.selected.indices;
    var d1 = s1.data;

    for (var i = 0; i < inds.length; i++) 
    {
    d1['color'][inds[i]] = 'red';
    var index = inds[i];
    var command = "s1.data['color'][" + index + "] = red";
    var kernel = IPython.notebook.kernel;
    kernel.execute(command);
    } 

    s1.change.emit();;
""")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM