将值从Jupyter小部件映射到字典的更好方法

时间:2018-06-12 11:18:16

标签: python widget jupyter

我确实有一个类似于这个的jupyter小部件:

def run():
    style = {'description_width': 'initial'}
    A = widgets.Dropdown(options={'A': 'B'},
                                   description='Some selector:',
                                   disabled=False,
                                   continuous_update=False,
                                   orientation='horizontal',
                                   readout=True,
                                   style=style)

    # slider for setting C
    B = widgets.FloatSlider(value=50,
                           min=10,
                           max=200,
                           step=1,
                           description='Variable C in some units',
                           disabled=False,
                           continuous_update=False,
                           orientation='horizontal',
                           readout=True,
                           readout_format='.1f',
                           style=style)

    # slider for setting C
    C = widgets.FloatSlider(value=50,
                           min=10,
                           max=200,
                           step=1,
                           description='Variable C in some units',
                           disabled=False,
                           continuous_update=False,
                           orientation='horizontal',
                           readout=True,
                           readout_format='.1f',
                           style=style)

    # slider for setting D
    D = widgets.FloatSlider(value=50,
                           min=10,
                           max=200,
                           step=1,
                           description='Variable D in some units',
                           disabled=False,
                           continuous_update=False,
                           orientation='horizontal',
                           readout=True,
                           readout_format='.1f',
                           style=style)

    # slider for setting E
    E = widgets.FloatSlider(value=50,
                           min=10,
                           max=200,
                           step=1,
                           description='Variable E in some units',
                           disabled=False,
                           continuous_update=False,
                           orientation='horizontal',
                           readout=True,
                           readout_format='.1f',
                           style=style)

    opt_list = [A, B]
    req_list = [C, D, E]

    req = widgets.VBox(req_list)
    opt = widgets.VBox(opt_list)

    accordion = widgets.Accordion(children=[opt, req])
    accordion.set_title(0, 'Optional')
    accordion.set_title(1, 'Required')
    #accordion.set_title(2, 'PSD conversions')
    #accordion.set_title(3, 'Get design space')

    return accordion

如果我运行它:

w = run()
w

我可以设置个别值,这些值应作为我设置的模拟的输入。模拟期望输入以所需的格式,我有一个辅助类来做到这一点。我想将变量提供给辅助类,并执行类似的操作:

d = {}
for child in w.children:
    try:
        for sub_widget in child.children:
            try:
                name = sub_widget.description
                val = sub_widget.value
                print(name)
                print(val)
                d[name] = val
                print(10*'-')
            except AttributeError:
                continue
    except AttributeError:
        continue

dmap = {'Some selector:': 'state_1', 
        'Variable B in some units': 'state_b',
       'Variable C in some units': 'state_c',
       'Variable D in some units': 'state_d',
       'Variable E in some units': 'state_b'}

{dmap[k]:v for k, v in d.items()}

产生:

{'state_1': 'B', 'state_b': 50.0, 'state_c': 50.0, 'state_d': 50.0}

是否有更好的方法来获取变量(使用正确的名称)?

0 个答案:

没有答案