Добавить функцию в Bokeh - Python - PullRequest
0 голосов
/ 28 сентября 2019

Набор данных:

enter image description here У меня есть функция, которая генерирует своего рода цветное облако слов:

def format_chars(chars,numbers):
    letter = lambda x: "<span style='color:{};'>{}</span>".format(x[1],x[0])
    text = " ".join(list(map(letter, zip(chars,vals))))
    text = "<div style='font-size:14pt;font-weight:bold;'>" + text + "</div>"
    col  = HTML(text)
    return col

chars = df_final['term']
vals = df_final['hex']
col = format_chars(chars,vals) 

enter image description here Я экспериментирую с Bokeh, чтобы получить два фильтра, которые фильтруют столбец ниже по номеру lan_name и страны и только значения для этой конкретной пары lan_name и country.

У меня есть код, который фильтрует таблицу:

import bokeh.embed
import bokeh.io
import bokeh.models
import bokeh.models.widgets
import bokeh.plotting
import pandas as pd
from pandas_datareader import wb

bokeh.plotting.output_notebook()

source = bokeh.models.ColumnDataSource(subset)
original_source = bokeh.models.ColumnDataSource(subset)
columns = [
    bokeh.models.widgets.TableColumn(field="country", title="country"),
    bokeh.models.widgets.TableColumn(field="lan_name", title="lan_name"),
    bokeh.models.widgets.TableColumn(field="term", title="term"),
    bokeh.models.widgets.TableColumn(field="hex", title="hex")
]
data_table = bokeh.models.widgets.DataTable(source=source, columns=columns)
# callback code to be used by all the filter widgets
combined_callback_code = """
var data = source.get('data');
var original_data = original_source.get('data');
var country = country_select_obj.get('value');
console.log("country: " + country);
var lan_name = lan_name.get('value');
console.log("lan_name: " + lan_name);
for (var key in original_data) {
    data[key] = [];
    for (var i = 0; i < original_data['country'].length; ++i) {
        if ((country === "ALL" || original_data['country'][i] === country) &&
            (lan_name === "ALL" || original_data['lan_name'][i] === lan_name)) {
            data[key].push(original_data[key][i]);
        }
    }
}
target_obj.trigger('change');
source.trigger('change');
"""

# define the filter widgets, without callbacks for now
country_list = ['ALL'] + df['country'].unique().tolist()
country_select = bokeh.models.widgets.Select(title="Country:", value=country_list[0], options=country_list)
lan_list = ['ALL'] + df['lan_name'].unique().tolist()
lan_select = bokeh.models.widgets.Select(title="lan_name:", value=lan_list[0], options=lan_list)

# now define the callback objects now that the filter widgets exist
generic_callback = bokeh.models.CustomJS(
    args=dict(source=source, 
              original_source=original_source, 
              country_select_obj=country_select, 
              lan_select_obj=lan_select, 
              target_obj=data_table),
    code=combined_callback_code
)

# finally, connect the callbacks to the filter widgets
country_select.callback = generic_callback
lan_select.callback = generic_callback

from bokeh.layouts import column
#p = bokeh.io.vplot(country_select, lan_select, data_table)
#bokeh.plotting.show(p)
show(column(country_select, lan_select, data_table))

Как добавить функцию в боке?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...