【问题标题】:Python onclick button widget return objectPython onclick 按钮小部件返回对象
【发布时间】:2019-02-21 17:35:26
【问题描述】:

我正在尝试使用 python 在 jupyter 笔记本中构建文件/数据选择器。这个想法是我使用 multipleSelect 小部件在文件中选择一些文件和数据通道,然后使用按钮返回一个数据帧。

如何访问 df_object?

#stack example
from ipywidgets import widgets
from IPython.display import display
from IPython.display import clear_output
import pandas as pd
import numpy as np
filenames = ["file1", "file2"]
file_dict = {
    "file1":pd.DataFrame(np.arange(5)),
    "file2":pd.DataFrame(np.arange(10,15))
}

def data_selection():
    sel_file = widgets.SelectMultiple(description="Files",
    options=filenames)
    display(sel_file)

    button = widgets.Button(description="OK")
    display(button)            

    def on_button_clicked(button):
        clear_output(wait=True) #clears the previous output
        display(sel_file) #displays new selection window
        display(button) #displays new button
        for f in sel_file.value:
            print (f)
            display (file_dict[f])
            #global df_object #would be a solution but not recommended for sure
            df_object = file_dict[f]
            return df_object #doesn't work
    button.on_click(on_button_clicked)

data_selection()    

【问题讨论】:

  • 你好像忘了问你的问题...
  • 你想把数据框返回到哪里?期望的行为是什么?

标签: python ipywidgets


【解决方案1】:

您确实应该为此使用一个类,然后将所有函数定义为作用于该类的实例。并非所有这些都需要公开访问。您还可以将df_objects 存储在字典等单独的属性中,并使用单独的函数访问字典。查看下面的代码:

class foo(object):

    def __init__(self, file1, file2):
        self.filenames = [file1, file2]
        self.file_dict = {
                file1:pd.DataFrame(np.arange(5)),
                file2:pd.DataFrame(np.arange(10,15))
            }

    def _create_widgets(self):
        self.sel_file = widgets.SelectMultiple(description='Files',
                                               options=self.filenames,
                                               value=[self.filenames[0]],
                                              )
        self.button = widgets.Button(description="OK")
        self.button.on_click(self._on_button_clicked)

    def _on_button_clicked(self, change):
        self.out.clear_output()
        self.df_objects = {}
        with self.out:
            for f in self.sel_file.value:
                print(f)
                display(self.file_dict[f])
                self.df_objects[f] = self.file_dict[f]

    def display_widgets(self):
        self._create_widgets()
        self.out = widgets.Output()  # this is the output widget in which the df is displayed
        display(widgets.VBox(
                            [
                                self.sel_file,
                                self.button,
                                self.out
                            ]
                        )
               )

    def get_df_objects(self):
        return self.df_objects

然后您可以创建实例并像这样显示小部件:

something = foo('a', 'b')
something.display_widgets()

something.get_df_objects() 将返回包含所需“file:dataframe_of_file”键值对的字典。

希望这会有所帮助:)

【讨论】:

猜你喜欢
  • 2018-06-27
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 2015-10-30
  • 1970-01-01
  • 2021-08-13
相关资源
最近更新 更多