我想使用ipywidget以交互方式显示熊猫数据帧。到目前为止,代码获取一些选择,然后进行一些计算。对于此示例情况,它实际上并不使用输入标签。但是,我的问题是,当我想显示熊猫数据帧时,它不会被视为小部件。但是,我如何使用小部件很好地显示熊猫数据帧?最后,我想在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)
阿波罗的战车
相关分类