【问题标题】:How to display ipython widgets after calling .close() one them如何在调用 .close() 后显示 ipython 小部件
【发布时间】: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


    【解决方案1】:

    如果我理解您想要正确执行的操作,您可以做的一件事是在每个事件处理函数的开头调用 clear_output() 函数,而不是使用 widget.close() 关闭小部件。这样做可以让您根据需要随时打开和关闭小部件。

    代码如下:

    import ipywidgets as widgets
    from IPython.display import display, clear_output
    
    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):
        clear_output()
        print(f"Selected {selectOrg.label.upper()}")
        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):
        clear_output()
        print(f"Selected {selectUser.label}")
        print("Please select a timespan:")
        display(selectDays)
        display(submitDaysButton)
    
    def submitMetricHandler(obj):
        clear_output()
        print(f"Selected timespan: {selectDays.value} days \n")
        n_days = selectDays.value
        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)
    

    【讨论】:

    • 我也想过这个解决方案,但我的总体目标是能够根据需要选择性地关闭/显示小部件。在这种情况下,我希望小部件在每次按下按钮后消失,但打印的语句(“Selected {user_email}”)保留。我可以在每个 clear_output() 之后重复打印语句,但这会使代码更加混乱......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多