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

时间:2018-07-05 22:07:25

标签: python bokeh

我正在尝试在Python上使用Bokeh来实现交互式(HTML)地图,以便可以将其发布在我的个人GitHub上。我意识到,如果我不想使用Bokeh服务器,则无法在Python中使用回调。在意识到此问题之前,我在Python上编写了回调或更新函数,而现在在用JS重写它时遇到了麻烦。

此功能是在从滑块上选择的值上切片熊猫数据框之后,更新地图上活动字形的数据源。

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_ddschoolzone_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调用上以类似的方式对数据帧进行切片?

谢谢。

1 个答案:

答案 0 :(得分:0)

显然,有一种更简便的方法(使用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)

定义您的回调:

def callback(source=source_bok, original_source=original_source,
             sibling_source=sibling_dd, zone_source=schoolzone_dd):

    # initialize data
    data = original_source.data
    s_data = source.data
    zone = zone_source.value
    sibling = sibling_source.value


    # fetch unique schools
    schools = s_data['school']

    # fetch columns from original data
    orig_schools = data['school']
    orig_nhoods = data['nhood']
    orig_sibling = data['sibling']
    orig_year = data['year']
    orig_market = data['market']
    orig_prio_mode = data['prio_mode']
    orig_ratex = original_source.data['ratex']
    orig_ratex_str = data['ratex_str']
    orig_priority = data['priority']

    no_prio_ratex = []
    no_prio_ratex_str = []
    no_prio_priority = []
    for row in range(len(orig_schools)):
        for j in range(len(schools)):
            if ((orig_nhoods[row] == 'None')
                & (orig_sibling[row] == 'None')
                & (orig_schools[row] == schools[j])
                & (orig_year[row] == 1718)
                & (orig_market[row] == 'dayof')
                & (orig_prio_mode[row] == 'priority_b')):
                    no_prio_ratex.append(orig_ratex[row])
                    no_prio_ratex_str.append(orig_ratex_str[row])
                    no_prio_priority.append(orig_priority[row])

        #return (no_prio_ratex, no_prio_ratex_str, no_prio_priority)
    if (zone == 'None') & (sibling == 'None'):
        s_data['ratex_str'] = no_prio_ratex_str
        s_data['ratex'] = no_prio_ratex
        s_data['priority'] = no_prio_priority

    if (zone == 'None') & (sibling != 'None'):
        s_prio_ratex = []
        s_prio_ratex_str = []
        s_prio_priority = []
        for row in range(len(orig_schools)):
            for j in range(len(schools)):
                if ((orig_nhoods[row] == 'None')
                    & (orig_sibling[row] == sibling)
                    & (orig_schools[row] == schools[j])
                    & (orig_year[row] == 1718)
                    & (orig_market[row] == 'dayof')
                    & (orig_prio_mode[row] == 'priority_b')):
                        s_prio_ratex.append(orig_ratex[row])
                        s_prio_ratex_str.append(orig_ratex_str[row])
                        s_prio_priority.append(orig_priority[row])

        s_data['ratex'] = [s_prio_ratex[0] if s_data['school'][i] == sibling else s_data['ratex'][i] for i in range(len(s_data['school']))]
        s_data['ratex_str'] = [s_prio_ratex_str[0] if s_data['school'][i] == sibling else s_data['ratex_str'][i] for i in range(len(s_data['school']))]
        s_data['priority'] = [s_prio_priority[0] if s_data['school'][i] == sibling else s_data['priority'][i] for i in range(len(s_data['school']))]

    if (zone != 'None') & (sibling == 'None'):
        n_prio_ratex = []
        n_prio_ratex_str = []
        n_prio_priority = []
        for row in range(len(orig_schools)):
            for j in range(len(schools)):
                if ((orig_nhoods[row] == zone)
                    & (orig_sibling[row] == 'None')
                    & (orig_schools[row] == schools[j])
                    & (orig_year[row] == 1718)
                    & (orig_market[row] == 'dayof')
                    & (orig_prio_mode[row] == 'priority_b')):
                        n_prio_ratex.append(orig_ratex[row])
                        n_prio_ratex_str.append(orig_ratex_str[row])
                        n_prio_priority.append(orig_priority[row])

        s_data['ratex'] = [n_prio_ratex[0] if s_data['school'][i] == zone else s_data['ratex'][i] for i in range(len(s_data['school']))]
        s_data['ratex_str'] = [n_prio_ratex_str[0] if s_data['school'][i] == zone else s_data['ratex_str'][i] for i in range(len(s_data['school']))]
        s_data['priority'] = [n_prio_priority[0] if s_data['school'][i] == zone else s_data['priority'][i] for i in range(len(s_data['school']))]

    if (zone != 'None') & (sibling != 'None'):
        ns_prio_ratex = []
        ns_prio_ratex_str = []
        ns_prio_priority = []
        for row in range(len(orig_schools)):
            for j in range(len(schools)):
                if ((orig_nhoods[row] == zone)
                    & (orig_sibling[row] == sibling)
                    & (orig_schools[row] == schools[j])
                    & (orig_year[row] == 1718)
                    & (orig_market[row] == 'dayof')
                    & (orig_prio_mode[row] == 'priority_b')):
                        ns_prio_ratex.append(orig_ratex[row])
                        ns_prio_ratex_str.append(orig_ratex_str[row])
                        ns_prio_priority.append(orig_priority[row])

        s_data['ratex'] = [ns_prio_ratex[0] if (s_data['school'][i] == zone) & (s_data['school'][i] == sibling) else s_data['ratex'][i] for i in range(len(s_data['school']))]
        s_data['ratex_str'] = [ns_prio_ratex_str[0] if (s_data['school'][i] == zone) & (s_data['school'][i] == sibling) else s_data['ratex_str'][i] for i in range(len(s_data['school']))]
        s_data['priority'] = [ns_prio_priority[0] if (s_data['school'][i] == zone) & (s_data['school'][i] == sibling) else s_data['priority'][i] for i in range(len(s_data['school']))]

    if zone == 'None':
        s_data['nhood'] = ['None' for school in s_data['school']]
    else:
        s_data['nhood'] = [zone if school == zone else 'None' for school in s_data['school']]

    if sibling == 'None':
        s_data['sibling'] = ['None' for school in s_data['school']]
    else:
        s_data['sibling'] = [sibling if school == sibling else 'None' for school in s_data['school']]

    source.change.emit()def callback(source=source_bok, original_source=original_source,
             sibling_source=sibling_dd, zone_source=schoolzone_dd):

    # initialize data
    data = original_source.data
    s_data = source.data
    zone = zone_source.value
    sibling = sibling_source.value


    # fetch unique schools
    schools = s_data['school']

    # fetch columns from original data
    orig_schools = data['school']
    orig_nhoods = data['nhood']
    orig_sibling = data['sibling']
    orig_year = data['year']
    orig_market = data['market']
    orig_prio_mode = data['prio_mode']
    orig_ratex = original_source.data['ratex']
    orig_ratex_str = data['ratex_str']
    orig_priority = data['priority']

    no_prio_ratex = []
    no_prio_ratex_str = []
    no_prio_priority = []
    for row in range(len(orig_schools)):
        for j in range(len(schools)):
            if ((orig_nhoods[row] == 'None')
                & (orig_sibling[row] == 'None')
                & (orig_schools[row] == schools[j])
                & (orig_year[row] == 1718)
                & (orig_market[row] == 'dayof')
                & (orig_prio_mode[row] == 'priority_b')):
                    no_prio_ratex.append(orig_ratex[row])
                    no_prio_ratex_str.append(orig_ratex_str[row])
                    no_prio_priority.append(orig_priority[row])

        #return (no_prio_ratex, no_prio_ratex_str, no_prio_priority)
    if (zone == 'None') & (sibling == 'None'):
        s_data['ratex_str'] = no_prio_ratex_str
        s_data['ratex'] = no_prio_ratex
        s_data['priority'] = no_prio_priority

    if (zone == 'None') & (sibling != 'None'):
        s_prio_ratex = []
        s_prio_ratex_str = []
        s_prio_priority = []
        for row in range(len(orig_schools)):
            for j in range(len(schools)):
                if ((orig_nhoods[row] == 'None')
                    & (orig_sibling[row] == sibling)
                    & (orig_schools[row] == schools[j])
                    & (orig_year[row] == 1718)
                    & (orig_market[row] == 'dayof')
                    & (orig_prio_mode[row] == 'priority_b')):
                        s_prio_ratex.append(orig_ratex[row])
                        s_prio_ratex_str.append(orig_ratex_str[row])
                        s_prio_priority.append(orig_priority[row])

        s_data['ratex'] = [s_prio_ratex[0] if s_data['school'][i] == sibling else s_data['ratex'][i] for i in range(len(s_data['school']))]
        s_data['ratex_str'] = [s_prio_ratex_str[0] if s_data['school'][i] == sibling else s_data['ratex_str'][i] for i in range(len(s_data['school']))]
        s_data['priority'] = [s_prio_priority[0] if s_data['school'][i] == sibling else s_data['priority'][i] for i in range(len(s_data['school']))]

    if (zone != 'None') & (sibling == 'None'):
        n_prio_ratex = []
        n_prio_ratex_str = []
        n_prio_priority = []
        for row in range(len(orig_schools)):
            for j in range(len(schools)):
                if ((orig_nhoods[row] == zone)
                    & (orig_sibling[row] == 'None')
                    & (orig_schools[row] == schools[j])
                    & (orig_year[row] == 1718)
                    & (orig_market[row] == 'dayof')
                    & (orig_prio_mode[row] == 'priority_b')):
                        n_prio_ratex.append(orig_ratex[row])
                        n_prio_ratex_str.append(orig_ratex_str[row])
                        n_prio_priority.append(orig_priority[row])

        s_data['ratex'] = [n_prio_ratex[0] if s_data['school'][i] == zone else s_data['ratex'][i] for i in range(len(s_data['school']))]
        s_data['ratex_str'] = [n_prio_ratex_str[0] if s_data['school'][i] == zone else s_data['ratex_str'][i] for i in range(len(s_data['school']))]
        s_data['priority'] = [n_prio_priority[0] if s_data['school'][i] == zone else s_data['priority'][i] for i in range(len(s_data['school']))]

    if (zone != 'None') & (sibling != 'None'):
        ns_prio_ratex = []
        ns_prio_ratex_str = []
        ns_prio_priority = []
        for row in range(len(orig_schools)):
            for j in range(len(schools)):
                if ((orig_nhoods[row] == zone)
                    & (orig_sibling[row] == sibling)
                    & (orig_schools[row] == schools[j])
                    & (orig_year[row] == 1718)
                    & (orig_market[row] == 'dayof')
                    & (orig_prio_mode[row] == 'priority_b')):
                        ns_prio_ratex.append(orig_ratex[row])
                        ns_prio_ratex_str.append(orig_ratex_str[row])
                        ns_prio_priority.append(orig_priority[row])

        s_data['ratex'] = [ns_prio_ratex[0] if (s_data['school'][i] == zone) & (s_data['school'][i] == sibling) else s_data['ratex'][i] for i in range(len(s_data['school']))]
        s_data['ratex_str'] = [ns_prio_ratex_str[0] if (s_data['school'][i] == zone) & (s_data['school'][i] == sibling) else s_data['ratex_str'][i] for i in range(len(s_data['school']))]
        s_data['priority'] = [ns_prio_priority[0] if (s_data['school'][i] == zone) & (s_data['school'][i] == sibling) else s_data['priority'][i] for i in range(len(s_data['school']))]

    if zone == 'None':
        s_data['nhood'] = ['None' for school in s_data['school']]
    else:
        s_data['nhood'] = [zone if school == zone else 'None' for school in s_data['school']]

    if sibling == 'None':
        s_data['sibling'] = ['None' for school in s_data['school']]
    else:
        s_data['sibling'] = [sibling if school == sibling else 'None' for school in s_data['school']]

    source.change.emit()

source.change.emit()是关键部分(这对我没有真正深入了解文档是不利的)

最后,在保存或渲染之前,请致电:

sibling_dd.callback = CustomJS.from_py_func(callback)

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

PS-@Matt的答案和随后的解决方案非常有用。