Как использовать «тап» в боке, чтобы произвести изменения на другом графике (или в таблице)? - PullRequest
0 голосов
/ 05 апреля 2019

Я пытаюсь создать панель инструментов bokeh (используя только python, а не JS!), В которой нажатие на глиф в одном графике влияет на вывод таблицы в той же панели. По какой-то причине я могу нажать и выбрать точку, но я не вижу изменений в таблице. (Также по какой-то причине он не выводится на консоль, поэтому мне сложно отлаживать (?)) Я пробовал много разных вещей, но безуспешно. Если бы кто-нибудь мог предоставить какой-либо вклад, я был бы очень благодарен Кодекс следует. Код ниже существовал под именем "main.py" и находился в папке с именем "select_exp". Сервер bokeh был запущен с: боке подача select_exp. Еще раз спасибо заранее за любую помощь! Gino

`

from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.models.widgets import DataTable, TableColumn

# Create data for plot
x = [0, 1]
y = [0, 1]
table_index = [0, 1]

# Create the plot (this will be clicked on)
plot = figure(height = 400, width = 600,
              title='Select a point', tools='tap')
plot_source = ColumnDataSource(data = dict(x=x, y=y))
renderer = plot.circle('x',  'y', source=plot_source, size=30)


# Create two sets of data for the tablet
master_data = {}
master_data[0] = {'animals': ['dog', 'cat', 'cow', 'mouse'],
                   'plants': ['carrot', 'catnip', 'grass', 'cheese']}
master_data[1] = {'animals': ['elephant', 'lion', 'monkey', 'emu'],
                  'plants': ['grass', 'turnips', 'banana', 'petunias']}

# Create a table
data = master_data[0]
table_source = ColumnDataSource(data)
columns = [  TableColumn(field='animals', title = 'Animal'),
             TableColumn(field='plants',  title = 'Plant')   ]
data_table = DataTable(source=table_source, columns=columns,
                       width=400, height=600)

# Here the reactions of the server are defined
def my_tap_handler(attr, old, new):
    index = source.selected.indices
    print(index)
    data_table.source = ColumnDataSource(master_data[index])

renderer.data_source.on_change("selected", my_tap_handler)

# Collect it all together iin the current doc
curdoc().add_root(column(plot, data_table))
curdoc().title = 'Select experiment'

`

1 Ответ

0 голосов
/ 05 апреля 2019

В Bokeh v1.0.4 вам необходимо применить обратный вызов к свойству selected атрибута data_source и к атрибуту indices.Запустите код с помощью bokeh serve --show app.py

from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.models.widgets import DataTable, TableColumn

# Create data for plot
x = [0, 1]
y = [0, 1]
table_index = [0, 1]

# Create the plot (this will be clicked on)
plot = figure(height = 400, width = 600,
              title = 'Select a point', tools = 'tap')
plot_source = ColumnDataSource(data = dict(x = x, y = y))
renderer = plot.circle('x', 'y', source = plot_source, size = 30)

# Create two sets of data for the tablet
master_data = {}
master_data[0] = {'animals': ['dog', 'cat', 'cow', 'mouse'],
                   'plants': ['carrot', 'catnip', 'grass', 'cheese']}
master_data[1] = {'animals': ['elephant', 'lion', 'monkey', 'emu'],
                  'plants': ['grass', 'turnips', 'banana', 'petunias']}

# Create a table
data = master_data[0]
table_source = ColumnDataSource(data)
columns = [  TableColumn(field = 'animals', title = 'Animal'),
             TableColumn(field = 'plants', title = 'Plant')   ]
data_table = DataTable(source = table_source, columns = columns,
                       width = 400, height = 600)

# Here the reactions of the server are defined
def my_tap_handler(attr, old, new):
    index = new[0]
    data_table.source = ColumnDataSource(master_data[index])

plot_source.selected.on_change("indices", my_tap_handler)

# Collect it all together in the current doc
curdoc().add_root(column(plot, data_table))
curdoc().title = 'Select experiment'

Результат:

enter image description here

...