带有Bokeh帮助的CustomJS回调(不知道任何JS!)

2024-09-30 12:21:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在Python上使用Bokeh实现一个交互式(HTML)映射,以便可以在我的个人GitHub上发布它。我意识到如果我不想使用Bokeh服务器,就不能在Python中使用回调。在意识到这个问题之前,我在Python上编写了callback或update函数,现在在JS上重写它遇到了困难。在

在从滑块上选择的值切片pandas数据帧后,该函数将更新地图上活动glyph的数据源。在

def make_data(year=1718, market='dayof', mode='priority_b',
          zone='None', sibling='None'):
"""
This functions subsets the ratexs_dfs by year, market, mode and sibling
parameters refer to the following:
    year =      school year, 1718 or 1819
    market =    time at which cutoffs were calculated:
        any =       anyday, this includes waitlisted applicants and such
        dayof =     these cutoffs were calculated at the day of the lottery
                    #   it should be noted that only the shp_data from SY1718
                        has shp_data from both dayof and any markets.
                        any or dayof markets for SY1819 are identical
                        since the shp_data was obtained before waitlist placements
    mode =      priority system.
        priority =      this system is the native method of the yearself.
                        SY1718 uses the 2 digit priority system,
                        SY1819 uses the 3 digit priority system.
        priority_b =    this is the secondary system. For SY1718, priority
                        and priority_b are identical. However for SY1819,
                        priority is the 3 digit priority system, while
                        priority_b is the SY1819 priorities adapted for the
                        SY1718 priority system.
    sibling =   school where applicant would get sibling priority
"""

school_ratex = ratexs_df[(ratexs_df['year'] == year)
                         & (ratexs_df['market'] == market)
                         & (ratexs_df['prio_mode'] == mode)
                         & ((ratexs_df['nhood'] == zone)
                            | (ratexs_df['nhood'] == 'None'))
                         & ((ratexs_df['sibling'] == sibling)
                            | (ratexs_df['sibling'] == 'None'))]

school_ratex = school_ratex.drop_duplicates(subset='school', keep='first')
school_ratex['ratex'] = (school_ratex['ratex'] * 100).round(2)
school_ratex['ratex_str'] = school_ratex['ratex'].astype(str) + ' %'
school_ratex = school_ratex.sort_values(by='priority')

return ColumnDataSource(school_ratex)

更新时的功能是:

^{pr2}$

其中sibling_dd和{}是滑块:

# define dropdown widgets
# school lists
schools = ['None']
schools.extend(sorted(src['school'].values.tolist(), reverse=False))
sibling_dd = Select(title='Sibling School',
                    value=schools[0],
                    options=schools)

# zones lists
zones = ['None']
zones.extend(sorted(shp_source['app_name'].values.tolist(), reverse=False))
zones.insert(1, schools[1])
schoolzone_dd = Select(title='School Zone',
                       value=zones[0],
                       options=zones)

有没有一种简单的方法可以在CustomJS调用中以类似的方式切片dataframe,同时使用两个滑块的值?在

提前谢谢。在


Tags: thenonedfdatamodeyearsystemmarket
1条回答
网友
1楼 · 发布于 2024-09-30 12:21:44

显然有一种更简单的方法(使用Python)使用PScript一个编译Python int Javascript的包。在

解决方案是在没有任何特殊参数的情况下调用小部件,例如:

# school lists
schools = ['None']
schools.extend(sorted(source['school'].values.tolist(), reverse=False))
sibling_dd = Select(title='Sibling School',
                    value=schools[1],
                    options=schools)

定义回调:

^{pr2}$

source.change.emit()是一个关键的部分(我的缺点是没有真正地挖掘文档)

最后,在保存或渲染之前,调用:

sibling_dd.callback = CustomJS.from_py_func(callback)

确保回调中的所有python都符合PScript!在

PS-非常有帮助的是@Matt答案和随后的解决方案。在

相关问题 更多 >

    热门问题