【问题标题】:Ordering widgets in IPython notebooks在 IPython 笔记本中订购小部件
【发布时间】:2015-05-28 17:57:29
【问题描述】:

我正在使用 IPython (IPython.html.widgets) 的模块小部件,创建一些小部件。

我正在做的是创建一堆小部件,比如说 4 个整数滑块:

w1 = widgets.IntSliderWidget(description='w1')
w2 = widgets.IntSliderWidget(description='w2')
w3 = widgets.IntSliderWidget(description='w3')
w4 = widgets.IntSliderWidget(description='w4')

我想在函数a_function() 上使用函数interact() 来玩小部件。但是我希望有一个代码,我没有在函数的参数中明确列出所有小部件(通用的东西)。比如:

def a_function(**kwargs):
    print kwargs
    # do other things...

kwargs = {}
kwargs['w1'] = w1
kwargs['w2'] = w2
kwargs['w3'] = w3
kwargs['w4'] = w4

# calling the function interact() to interact with the function a_function()
interact(a_function, **kwargs)

我的问题是 Python 中的字典未排序,但我想管理小部件在笔记本中出现的顺序。我想确保小部件w1 将出现在w2 之前,w2w3 之前,等等...

有解决这个问题的建议吗?

非常感谢您的帮助!

【问题讨论】:

    标签: python-2.7 widget ipython-notebook


    【解决方案1】:

    您可以尝试对键进行排序:

    def a_function(**kwargs):
        for k in sorted(kwargs.keys()):
            print k, kwargs[k]
        # do other things...
    

    【讨论】:

      【解决方案2】:

      您是否尝试过使用 collections.OrderedDict() 而不是 dict()?它会记住插入键的顺序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-03
        • 1970-01-01
        • 1970-01-01
        • 2016-10-14
        相关资源
        最近更新 更多