Боке связывание / чистка на основе столбца вместо индексов строк / индекса с использованием инструментов выбора

Вы можете перебирать невидимые атрибуты класса с помощью for attr in (elem for elem in dir(Foo) if elem[:2] != '__').

Менее ужасный способ написания:

def class_iter(Class):
    return (elem for elem in dir(Class) if elem[:2] != '__')

, затем

for attr in class_iter(Foo):
    pass
0
задан D.Thomas 19 March 2019 в 08:27
поделиться

1 ответ

Основываясь на ответе Тони (используя BoxSelectTool для одиночного выбора), я придумал код для множественного выбора. (Обратите внимание, что я сделал несколько опечаток в моей первоначальной функции callback1)

from bokeh.plotting import figure, show
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, CDSView, BooleanFilter, CustomJS, BoxSelectTool
import pandas as pd
# output_notebook()
data = {'person': [1, 1, 1, 2, 2, 3, 3, 3, 3],
        'activities':['a', 'b', 'c', 'a', 'b', 'a', 'b', 'c', 'd'],
        'hours':[3, 4, 6, 2, 7, 5, 3, 2, 12],
        'foodeaten':[12, 14, 34, 45, 67, 5, -1, 3, 5]}
df = pd.DataFrame(data = data)
print(df)
source = ColumnDataSource(data = df)
views = [(df.activities == l) for l in ['a', 'b', 'c', 'd']]
filtered_views = [CDSView(source = source, filters = [BooleanFilter(view.values.tolist())]) for view in views]
plot_options = dict(plot_width = 250,
                    plot_height = 250,
                    tools = "box_select,tap,pan,wheel_zoom,reset,save",
                    tooltips = [("Person", "@person"), ("hours", "@hours")])
plots = [figure(title = 'activity {l}'.format(l = l), **plot_options) for l in ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
for plot, view, name in zip(plots, 2 * filtered_views, 4 * ['hours'] + 4 * ['foodeaten']):
    plot.circle('person', y = name, size = 15, view = view, source = source)


callback1 = CustomJS(args = dict(source = source, ), code = """
var selected_indices = source.selected.indices;
var data = source.data;
debugger;
console.log(selected_indices)
var all_selected = [];
var persons = [];

for (i in selected_indices){
    index = selected_indices[i];
    console.log(data['person'][index]);
    persons.push(data['person'][index]);
    }


for (i in data.index){
    index = data.index[i]
    for (j in persons){
        person = persons[j]
        if (data['person'][i] == person){
            all_selected.push(index);
            }
    }
    }
source.selected.indices=all_selected;
""")

# output_file('testSelect.html')
for plot in plots:
    plot.select(BoxSelectTool).callback = callback1
show(gridplot(children = [plot for plot in plots], ncols = 2))
0
ответ дан D.Thomas 19 March 2019 в 08:27
поделиться
Другие вопросы по тегам:

Похожие вопросы: