【发布时间】:2018-07-05 22:07:25
【问题描述】:
我正在尝试在 Python 上使用 Bokeh 实现交互式 (HTML) 地图,以便可以在我的个人 GitHub 上发布它。我意识到如果我不想使用 Bokeh Server,我就不能在 Python 中使用回调。在我意识到这个问题之前,我在 Python 上编写了回调或更新函数,现在我在 JS 上重写它时遇到了麻烦。
在从滑块上选择的值中分割出 pandas 数据框后,该函数会更新地图上活动字形的数据源。
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)
那么更新的函数是:
def update(attr, old, new):
# make a new def
new_src = make_data(zone=schoolzone_dd.value,
sibling=sibling_dd.value)
# update data on map
psource2.data.update(new_src.data)
其中sibling_dd 和schoolzone_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 调用中使用两个并行滑块的值以类似的方式对数据帧进行切片?
提前致谢。
【问题讨论】: