【发布时间】:2021-10-11 10:55:21
【问题描述】:
我正在使用 ipython 小部件开发数据可视化仪表板,并且我正在尝试建立一个连续的流程。我的问题是,一旦我为某些widget 调用widget.close(),调用display(widget) 就不再有任何作用。
一旦点击clearOutputButton,我的计划就会重新开始。有人知道这是什么原因吗?
在我的代码使用数据库调用时减少它的版本:
import ipywidgets as widgets
from IPython.display import display
orgs_list = ["org_1", "org_2"]
selectOrg = widgets.Dropdown(options = orgs_list)
selectDays = widgets.IntSlider(min = 7, max = 90, value = 30, step = 1)
submitOrgButton = widgets.Button(description = 'Submit', button_style='info', tooltip = "Click here to submit")
submitUserButton = widgets.Button(description = 'Submit', button_style='info', tooltip = "Click here to submit")
submitDaysButton = widgets.Button(description = 'Submit', button_style='info', tooltip = "Click here to submit")
clearOutputButton = widgets.Button(description='New Query', button_style='info', tooltip='Make another query')
def submitOrgHandler(obj):
print(f"Selected {selectOrg.label.upper()}")
selectOrg.close()
submitOrgButton.close()
org_id = selectOrg.value
user_emails = ["user_email_1", "user_email_2"]
user_ids = ["user_id_1", "user_id_2"]
user_tuples = [(email, _id) for email, _id in zip(user_emails, user_ids)]
global selectUser
selectUser = widgets.Dropdown(options = user_tuples)
print("Please select a user:")
display(selectUser)
display(submitUserButton)
def submitUserHandler(obj):
print(f"Selected {selectUser.label}")
selectUser.close()
submitUserButton.close()
print("Please select a timespan:")
display(selectDays)
display(submitDaysButton)
def submitMetricHandler(obj):
print(f"Selected timespan: {selectDays.value} days \n")
n_days = selectDays.value
selectDays.close()
submitDaysButton.close()
print(f'Displaying information in the last {selectDays.value} days for {selectUser.label}...')
user_email = selectUser.label
# Do some plotting here
print("{Plot goes here!}")
display(clearOutputButton)
def clearOutputHandler(obj):
clear_output()
print("Please select an organisation, a user and a timespan:")
display(selectOrg)
display(submitOrgButton)
print("Please select an organisation, a user and a timespan:")
display(selectOrg)
display(submitOrgButton)
submitOrgButton.on_click(submitOrgHandler)
submitUserButton.on_click(submitUserHandler)
submitDaysButton.on_click(submitMetricHandler)
clearOutputButton.on_click(clearOutputHandler)
【问题讨论】:
标签: python jupyter-notebook ipywidgets