2

I have a function createWidgets whose purpose is to take a list of strings and create a list of containers for each string -> 1 container = a textbox and checkbox. Each container is then put into a large container.

What I am trying to do is append a button to the container that on_click takes all the "True"s and puts all the modified strings and puts them in a dataframe

    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

I have gone to many websites and spent many days on this help is very much appreciated

2 Answers 2

8

As near as I can interpret the question, the code below should provide an answer:

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.

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

I would have designed the code to do this somewhat differently, but I tried to stay close to your code organization.

2
  • Yes it does and more! Adding checkbox was a real bonus. I settled for much less. I guess the part I do not understand is how does the Callback for button.on_click. inherit bigContainer?
    – itay livni
    Oct 16, 2014 at 7:26
  • Yes, that could be confusing. Inside the callback function add_to_dataframe(...) the variable bigContainer is actually a global variable - you can see it defined three lines above. But inside the function createWidgets(...) there is also a local variable definition of bigContainer. A search for "python variable scope" lead me to this explanation but you will find many more expositions of of this. Oct 16, 2014 at 10:58
0

This is updated version for Ipython3 on jupyternotebooks 4

Just rename:

  • widgets.ContainerWidget ->widgets.Box
  • widgets.CheckboxWidget -> widgets.Checkbox
  • widgets.TextWidget -> widgets.Text

Reference: [https://ipython.org/ipython-doc/3/whatsnew/version3_widget_migration.html]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.