在update()回调函数中为layout.children分配元素会弄乱一个孩子的标题和另一个孩子的位置

时间:2017-04-26 11:42:21

标签: bokeh

我有一个小的散景应用程序,包括输入,条形图和表格。当用户从输入中选择一个值时,根据新数据和layout.children相应地替换图表和表格。问题是,当页面更新时,条形图标题会混乱,表格的位置也是如此。

更新时,我会打印第一行中孩子的长度。每次都是两次,所以当我在update()函数中替换它们时,它不会添加更多的子项。

以下是相关代码:

income = Select(title="Income Tier", options=sorted(income_map.keys()), 
value="All")
age = Select(title="Age", options=sorted(age_map.keys()), value="All")
gender = Select(title="Gender", options=sorted(gender_map.keys()), 
value="All")
relationship = Select(title="Relationship", 
options=sorted(relationship_map.keys()), value="All")
education = Select(title="Education", 
options=sorted(education_map.keys()), value="All")
user_interests = Select(title="User Interests", 
options=sorted(user_interests_map.keys()), value="All")

def update():
    update_p1()
    update_p2()

def update_p1():
    global awareness_data_copy
    global income
    global age
    global gender
    global relationship
    global education
    global graph_data
    global layout
    awareness_data_copy = awareness_data
    if income.value != 'All':
        awareness_data_copy = 
        awareness_data_copy.loc[(awareness_data_copy['income_tier'] == 
    income.value)]
    if age.value != 'All':
        awareness_data_copy = 
        awareness_data_copy.loc[(awareness_data_copy['age'] == 
        age.value)]
    if gender.value != 'All':
        awareness_data_copy = 
        awareness_data_copy.loc[(awareness_data_copy['gender'] == 
        gender.value)]
    if relationship.value != 'All':
        awareness_data_copy = 
        awareness_data_copy.loc[(awareness_data_copy 
        ['relationship_label'] == relationship.value)]
    if education.value != 'All':
        awareness_data_copy = 
        awareness_data_copy.loc[(awareness_data_copy['education_label'] 
        == education.value)]
    if user_interests.value != 'All':
        awareness_data_copy = 
        awareness_data_copy.loc[(awareness_data_copy[education.value] 
        != None)]
    for key, value in graph_data['values'].items():
        column_awareness_data_copy = awareness_data_copy
        if awareness_data_copy['product'].count() > 0:
            column_awareness_data_copy = 
            column_awareness_data_copy.loc[awareness_data_copy 
            [answer_field] == key]
            graph_data['values'][key] = 
            column_awareness_data_copy['product'].count()
        else:
            graph_data['values'][key] = 0
            df = pd.DataFrame(graph_data)
    df['label'] = df.index
    new_p = Bar(df, values='values', label='label', 
        legend=False, title='Sources Of Awareness', xlabel="", 
        ylabel="")
    layout.children[0].children[1] = new_p

def update_p2():
    global table_data_copy
    table_data_copy = table_data
    if income.value != 'All':
        table_data_copy = table_data_copy.loc[(table_data_copy['income_tier'] == income.value)]
    if age.value != 'All':
        table_data_copy = table_data_copy.loc[(table_data_copy['age'] == age.value)]
    if gender.value != 'All':
        table_data_copy = table_data_copy.loc[(table_data_copy['gender'] == gender.value)]
    if relationship.value != 'All':
        table_data_copy = table_data_copy.loc[(table_data_copy['relationship_label'] == relationship.value)]
    if education.value != 'All':
        table_data_copy = table_data_copy.loc[(table_data_copy['education_label'] == education.value)]
    if user_interests.value != 'All':
        table_data_copy = table_data_copy.loc[(table_data_copy[education.value] != None)]
    layout.children[1].children[0] = build_cross_table(question_1, question_2)

sizing_mode = 'fixed'
controls = [income, age, gender, relationship, education, user_interests]
for control in controls:
    control.on_change('value', lambda attr, old, new: update())
inputs = widgetbox(*controls, sizing_mode=sizing_mode)

p1 = initialize_graph(graph_data)
add_support_columns(question_1, question_2)
p2 = build_cross_table(question_1, question_2)
layout = layout([inputs, p1], [p2])
curdoc().add_root(layout)
curdoc().title = "hi-chew"
output_file("bar.html")

show(layout)

以下是首次加载时页面的外观:

enter image description here

以下是我更改Select:

的值时的外观

enter image description here

非常感谢任何帮助。

0 个答案:

没有答案