将python回调函数转换为javascript

问题描述 投票:0回答:1

我想帮助将我在python中的回调函数从python转换为javascript。我的代码接下来显示(我删除了大部分代码,因为它对翻译并不重要。)

#returns a CDS of a dataframe based on one parameter
def create_df(option): 
    return ColumnDataSource(incident_df)

all_df = create_df('all')
critical_df = create_df('1')
high_df = create_df('2')
avg_low_df = create_df('3/4')
data_points = create_df('all')    
color_mapper=LinearColorMapper(palette=RdBu7,low=data_points.data['freq'].min(),high=data_points.data['freq'].max())

radio_button_group = RadioButtonGroup(
        labels=["All Priorities", "1 - Critical", "2 - High", "3/4 - Average/Low"], active=0)

#Define the callback function
def callback(attr, old, new):
    if radio_button_group.active == 0:
        new_cds = all_df
    if radio_button_group.active == 1:
        new_cds = critical_df 
    if radio_button_group.active == 2:
        new_cds = high_df
    if radio_button_group.active == 3:
        new_cds = avg_low_df
    data_points.data = new_cds.data
    color_mapper.low=min(data_points.data['freq'])
    color_mapper.high=max(data_points.data['freq'])

radio_button_group.on_change('active', callback)
javascript python bokeh
1个回答
1
投票

您的Python回调可以像这样转换为JS回调(假设它在Python中工作):

from bokeh.models import CustomJS

code = """
    if (radio_button_group.active == 0)
        new_cds = all_df
    if (radio_button_group.active == 1)
        new_cds = critical_df 
    if (radio_button_group.active == 2)
        new_cds = high_df
    if (radio_button_group.active == 3)
        new_cds = avg_low_df

    data_points.data = new_cds.data
    color_mapper.low = Math.min.apply(null, data_points.data['freq'])
    color_mapper.high = Math.max.apply(null, data_points.data['freq'])
 """
callback = CustomJS(args = dict(data_points = data_points,
                                color_mapper = color_mapper
                                radio_button_group = radio_button_group
                                all_df = all_df,
                                critical_df = critical_df,
                                high_df = high_df,
                                avg_low_df = avg_low_df), 
                    code = code)

radio_button_group.js_on_change('active', callback)
© www.soinside.com 2019 - 2024. All rights reserved.