我将在选择小部件中选择“日期”,然后将显示另一个选择小部件,具体取决于我选择的先前值。如果我再次选择之前的“日期”选择窗口小部件,则会再次出现一个新的“选择”窗口小部件,但之前的“选择”窗口小部件仍然存在。当我更改日期选择小部件中的值时,必须删除此项。有没有解决方法怎么做?
我尝试了curdoc()。清除但它清除了变化中的所有内容。只需要清除特定的小部件。
from bokeh.models.widgets import Div
import numpy as np
import pandas as pd
from bokeh.models.widgets import Tabs,Panel
welcome_message = 'Operator selected: (none)'
text_banner = Paragraph(text=welcome_message, width=200, height=100)
def callback_print(text_banner=text_banner):
user_input = str(cb_obj.value)
welcome_message = 'Operator selected: ' + user_input
text_banner.text = welcome_message
tabs.reset.emit()
#reaction_time.reset.emit()
def my_text_input_handler(attr, old, new):
print("Previous label: " + old)
print("Updated label: " + new)
root= join(dirname(__file__),str(new))
dirlist = [ item for item in os.listdir(root) if
os.path.isdir(os.path.join(root, item)) ]
available_date = dirlist
available_date += ['None']
def update(attr, old, new1):
root1= join(dirname(__file__),str(new),str(new1))
dirlist1 = [ item for item in os.listdir(root1) if
os.path.isdir(os.path.join(root1, item)) ]
available_level = dirlist1
available_level += ['None']
def update(attr, old, new2):
user_data = pd.read_csv(join(dirname(__file__),str(new),str(new1),str(new2),'test1.txt'), sep=",",na_filter =None)
data = user_data.copy()
#############################################
########PYTHON CODE ##########################
##############################################
##############################################
# Create each of the tabs
### ALL Tabs
# Put all the tabs into one application
tabs = Tabs(tabs = [tab1,tab2,tab3,tab4,tab5,tab6,tab7])
curdoc().clear()
layout = column(title,tabs,sizing_mode='scale_width')
curdoc().add_root(layout)
level_selection = Select(title="Select a Level : " ,value = 'None', options=available_level)
level_selection.on_change('value', update1)
curdoc().add_root(widgetbox(level_selection))
date_selection = Select(title="Select a date : " ,value = 'None', options=available_date)
date_selection.on_change('value', update)
layout1 = widgetbox(date_selection)
curdoc().add_root(layout1)
# Put the tabs in the current document for display
text_input = TextInput( title="Enter operator
Name:",callback=CustomJS.from_py_func(callback_print),css_classes=
['customTextInput'])
text_input.on_change('value', my_text_input_handler)
curdoc().add_root(widgetbox(text_input))
基于您添加的代码和评论讨论,我认为这应该适合您:
from bokeh.models import Div, Paragraph, Select, TextInput, CustomJS, WidgetBox
from bokeh.plotting import curdoc
import numpy as np
import pandas as pd
import os
from bokeh.models.widgets import Tabs, Panel
welcome_message = 'Operator selected: (none)'
text_banner = Paragraph(text = welcome_message, width = 200, height = 100)
current_date = None
def callback_print(text_banner = text_banner):
user_input = str(cb_obj.value)
welcome_message = 'Operator selected: ' + user_input
text_banner.text = welcome_message
tabs.reset.emit()
# reaction_time.reset.emit()
def my_text_input_handler(attr, old, new):
print("Previous label: " + old)
print("Updated label: " + new)
root = os.path.join(dirname(__file__), str(new))
dirlist = [ item for item in os.listdir(root) if
os.path.isdir(os.path.join(root, item)) ]
available_date = dirlist
available_date += ['None']
def update(attr, old, new1):
root1 = os.path.join(dirname(__file__), str(new), str(new1))
dirlist1 = [ item for item in os.listdir(root1) if
os.path.isdir(os.path.join(root1, item)) ]
available_level = dirlist1
available_level += ['None']
global current_date
if current_date is not None:
layout1.children.remove(layout1.children[0])
def update(attr, old, new2):
user_data = pd.read_csv(join(dirname(__file__), str(new), str(new1), str(new2), 'test1.txt'), sep = ",", na_filter = None)
data = user_data.copy()
#############################################
########PYTHON CODE ##########################
##############################################
##############################################
# Create each of the tabs
# ## ALL Tabs
# Put all the tabs into one application
tabs = Tabs(tabs = [tab1, tab2, tab3, tab4, tab5, tab6, tab7])
curdoc().clear()
layout = column(title, tabs, sizing_mode = 'scale_width')
curdoc().add_root(layout)
level_selection = Select(title = "Select a Level : " , value = 'None', options = available_level)
level_selection.on_change('value', update1)
curdoc().add_root(WidgetBox(level_selection))
current_date = new1
date_selection = Select(title = "Select a date : " , value = 'None', options = available_date)
date_selection.on_change('value', update)
layout1 = WidgetBox(date_selection)
curdoc().add_root(layout1)
# Put the tabs in the current document for display
text_input = TextInput(title = "Enter operator Name:", callback = CustomJS.from_py_func(callback_print), css_classes = ['customTextInput'])
text_input.on_change('value', my_text_input_handler)
curdoc().add_root(WidgetBox(text_input))
如果我理解正确,这就是你想要的。使用bokeh serve --show app.py
运行代码(使用Bokeh v1.0.4测试)
from bokeh.plotting import curdoc, show
from bokeh.models import Select, Column, Div
from datetime import datetime, timedelta
import numpy as np
day_start = datetime(2019, 3, 15)
day_step = timedelta(days = 1)
dates = [str(day_start + (i * day_step)) for i in range(0, 6)]
current_date = None
select_data = Select(title = 'Select a date', value = '', options = [''] + dates)
info = Div(text = 'Selected date: ')
layout = Column(info, select_data)
def update_info(attr, old, new):
prefix = info.text.split(', level:')
if len(prefix) > 1:
info.text = prefix[0] + ', level: {}'.format(new)
else:
info.text = info.text + ', level: {}'.format(new)
def update_layout(attr, old, new):
info.text = ('Selected date: {}'.format(new))
global current_date
if current_date is not None:
layout.children.remove(layout.children[2]) # index 0 == info, index 1 == select_data, so we need to remove index 1 = dynamic_select
levels = np.arange(np.random.randint(1, 4), np.random.randint(5, 7)) # replace this random levels with levels that you read from your csv file
dynamic_select = Select(title = 'Select a level for date: {}'.format(new), value = '', options = [''] + [str(value) for value in levels])
dynamic_select.on_change('value', update_info)
layout.children.append(dynamic_select)
current_date = new
select_data.on_change('value', update_layout)
curdoc().add_root(layout)
结果: