我想使用ipywidgets以交互方式显示熊猫数据框。到目前为止,代码得到了一些选择,然后进行了一些计算。对于这种示例,它并不是真正使用输入标签。但是,我的问题是当我想显示pandas数据框时,没有将其视为小部件。但是如何使用小部件很好地显示熊猫数据框?最后,我想在main_box
这是一个代码示例,可以在任何jupyter笔记本中使用
import pandas as pd
import ipywidgets as widgets
def button_run_on_click(_):
status_label.value = "running...."
df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]])
status_label.value = ""
result_box = setup_ui(df)
main_box.children = [selection, button_run, status_label, result_box]
def setup_ui(df):
return widgets.VBox([df])
selection_box = widgets.Box()
selection_toggles = []
selected_labels = {}
default_labels = ['test1', "test2"]
labels = {"test1": "test1", "test2": "test2", "test3": "test3"}
def update_selection(change):
owner = change['owner']
name = owner.description
if change['new']:
owner.icon = 'check'
selected_labels[name] = labels[name]
else:
owner.icon = ""
selected_labels.pop(name)
for k in sorted(labels):
o = widgets.ToggleButton(description=k)
o.observe(update_selection, 'value')
o.value = k in default_labels
selection_toggles.append(o)
selection_box.children = selection_toggles
status_label = widgets.Label()
status_label.layout.width = '300px'
button_run = widgets.Button(description="Run")
main_box = widgets.VBox([selection_box, button_run, status_label])
button_run.on_click(button_run_on_click)
display(main_box)
答案 0 :(得分:2)
def setup_ui(df):
out = widgets.Output()
with out:
display(df)
return out
如果将setup_ui
函数更改为此,则可以返回带有数据框的Output小部件。
但是,在您的button_run_on_click
函数中,似乎selection
未定义。还应该这样吗?