首页 > 解决方案 > 如何从多个小部件 ipywidgets 中获取观察值

问题描述

我正在尝试使用来自 ipywidgets 的交互式小部件创建自己的交互式路径选择器,应该有三个选择框,第二个观察第一个的值,第三个观察前一个的选择值,这就是我的问题,我不知道如何添加第三个小部件。

这是我的功能代码:

import os
import getpass
import glob
import ipywidgets as widgets
from ipywidgets import VBox, HBox, Label, interactive, Box

def return_paths(raw_path):
    """Create a list of folders in a given path
    skipping those with begin with '.' and are empty

    """
    paths = [folder for folder in os.listdir(raw_path) 
             if os.path.isdir(os.path.join(raw_path, folder)) and not folder.startswith('.') 
             and len(os.listdir(os.path.join(raw_path, folder))) != 0
    ]
    paths.sort()
    return paths

def get_select2(*args):
    options = return_paths(os.path.join(raw_path, select_1.value))  
    select_2.options = options

def process(Feature,ID):
    print(Feature,ID)

def run():
    w = interactive(process, Feature=select_1, ID=select_2)
    display(HBox([Label('Select the folder you want process:'), w.children[0], w.children[1]]))
    display(w.children[2])

user = getpass.getuser()
raw_path = f'/home/{user}/downloads/0_raw/'
raw_folders = []

select_1 = widgets.Select(
    options=return_paths(raw_path),
)

select_2 = widgets.Select()
select_3 = widgets.Select()
select_1.observe(get_select2)    

run()

此刻正在使用两个选定的框,如何添加第三个?任何想法?

标签: python-3.xjupyter-notebookjupyterinteractiveipywidgets

解决方案


推荐阅读