gpt4 book ai didi

widget - 如何使用 ipython 中的按钮获取小部件值

转载 作者:行者123 更新时间:2023-12-03 19:10:18 25 4
gpt4 key购买 nike

我有一个函数 createWidgets,其目的是获取一个字符串列表并为每个字符串创建一个容器列表 -> 1 个容器 = 一个文本框和一个复选框。然后将每个容器放入一个大容器中。

我想做的是在容器上附加一个按钮,on_click 获取所有“True”并将所有修改后的字符串放入数据框中

    widgelist = e.options
txtBox_type = 'text_widget' # Define if Area box o regular txtbox
bigContainer = createWidgets(widgelist, txtBox_type)

Function
def createWidgets(widgelist, txtBox_type):

#containerList = []
i = 0

for k in widgelist:
## Build Container widgets

chBox_Widget = widgets.CheckboxWidget(description = str(i),value = False,)
if txtBox_type == 'textA_widget': # Check wether txtBox should be an area txt box or not.
txt_Widget = widgets.TextareaWidget( description = str(i), value = k)
else:
txt_Widget = widgets.TextWidget( description = str(i), value = k)

container = widgets.ContainerWidget()
container.children = [chBox_Widget, txt_Widget]
containerList.append(container)

i+= 1

button = widgets.ButtonWidget(description = 'Add')
bigContainer = widgets.ContainerWidget()
bigContainer.children = containerList

return bigContainer

我访问了很多网站并花了很多天的时间在这个帮助上非常感谢

最佳答案

就我所能解释的问题而言,下面的代码应该提供答案:

import IPython.html.widgets as widgets
from IPython.display import display, clear_output
import pandas as pd

df = pd.DataFrame(columns=['Thing'])

def createWidgets(widgelist):

## Each CheckboxWidget and TextWidget are enclosed in a subwidget. We use a
## list comprehension to construct a list of these subwidgets.
containerList = [
widgets.ContainerWidget(children=(widgets.CheckboxWidget(description=k)
widgets.TextWidget(value=k)))
for k in widgelist]
bigContainer = widgets.ContainerWidget(children=containerList)

## To arrange the CheckboxWidget in a row with the TextWidget, we have to
## first display them, then remove_class('vbox') and add_class('hbox'). This
## bit of awkwardness in the IPython version 2.x notebook will hopefully
## be fixed in version 3.x. Displaying bigContainer also displays it's children.
display(bigContainer)
for c in containerList:
c.remove_class('vbox')
c.add_class('hbox')

return bigContainer

widgelist = ['ThingA', 'ThingB', 'ThingC', 'ThingD']
bigContainer = createWidgets(widgelist, txtBox_type)

## Callback for button.on_click.
def add_to_dataframe(a):
# The children of bigContainer are also containers,
# each with first child a CheckboxWidget and second
# child a TextWidget. We iterate through them and
# if checked, add the text to the dataframe df as
# an additional row.
for c in bigContainer.children:
if c.children[0].value:
df.loc[len(df)+1] = (c.children[1].value,)
display(df)

clear_output()
display(df)

button = widgets.ButtonWidget(description = 'Add')
button.on_click(add_to_dataframe)

display(button)

这是向数据框添加几行后小部件区域和输出的屏幕截图。

Here is a screen clip of the widget area and output after adding a few rows to the dataframe.

我会设计代码以稍微不同的方式来执行此操作,但我试图保持靠近您的代码组织。

关于widget - 如何使用 ipython 中的按钮获取小部件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26352555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com