<jupyter_start><jupyter_text>UNCLASSIFIED~~//FOR OFFICIAL USE ONLY~~Transcribed from FOIA Doc ID: 6689695https://archive.org/details/comp3321 **Note:** The overall classification of this lesson is marked as indicated above, however the material doesn't have portion markings after the second paragraph below. Also, looking through the material it's clear there isn't anything about it that warants an FOUO categorization so it appears this lesson was overprotected.  (U) ipywidgets (U) ipywidgets is used for making interactive widgets inside your jupyter notebook (U) The most basic way to get user input is to use the python built in `input` function. For more complicated types of interaction, you can use ipywidgets<jupyter_code># input example (not using ipywidgets) 
a = input("Give me your input: ")
print("Your input was: " + a)
import ipywidgets
from ipywidgets import *<jupyter_output><empty_output><jupyter_text>`interact` is the easiest way to get started with ipywidgets by creating a user interface and automatically calling the specified function.<jupyter_code>def f(x):
    return x*2

interact(f,x=10)
def g(check,y): 
    print ("{} {}".format(check,y))
interact(g,check=True,y="Hi there! ")<jupyter_output><empty_output><jupyter_text>But, if you need more flexibility, you can start from scratch by picking a widget and then calling the functionality you want. Hint: you get more widget choices this way.<jupyter_code>IntSlider()
w = IntSlider()
w<jupyter_output><empty_output><jupyter_text>You can explicitly display using IPython's display module. Note what happens when you display the same widget more than once!<jupyter_code>from IPython.display import display 
display(w)
w.value<jupyter_output><empty_output><jupyter_text>Now we have a value from our slider we can use in code. But what other attributes or "keys" does our slider widget have?<jupyter_code>w.max
new_w = IntSlider(max=200)
display(new_w)<jupyter_output><empty_output><jupyter_text>You can also close your widget<jupyter_code>w.close()
new_w.close()<jupyter_output><empty_output><jupyter_text>Here are all the available widgets:<jupyter_code>list(Widget.widget_types.items())<jupyter_output><empty_output><jupyter_text>Categories of widgets**Numeric:** IntSlider, FloatSlider, IntRangeSlider, FloatRangeSlider, IntProgress, FloatProgress, BoundedlntText, BoundedFloatText, IntText, FloatText **Boolean:** ToggleButton, Checkbox, Valid **Selection:** Dropdown, RadioButtons, Select, ToggleButtons, SelectMultiple **String Widgets:** Text, Textarea **Other common:** Button, ColorPicker, HTML, Image<jupyter_code>Dropdown(options=["1", "2", "3", "cat"])
bt = Button(description="Click me!") 
display(bt)<jupyter_output><empty_output><jupyter_text>Buttons don't do much on their own, so we have to use some event handling. We can define a function with the desired behavior and call it with the button's `on_click` method.<jupyter_code>def clicker(b):
    print("Hello World!!!!")

bt.on_click(clicker)
def f(change):
    print(change['new'])

w = IntSlider()
display(w)
w.observe(f, names='value')<jupyter_output><empty_output><jupyter_text>Wrapping Multiple Widgets in Boxes When working with multiple input widgets, it's often nice to wrap it all in a nice little box. ipywidgets provides a few options for this -- we'll cover HBox (horizontal box) and VBox (vertical box).  HBox This will display the widgets horizontally<jupyter_code>fruit_list = Dropdown(
    options = ['apple', 'cherry', 'orange', 'plum', 'pear']
)
fruit_label = HTML(
    value = 'Select a fruit from the list:&nbsp;&nbsp;'
)
fruit_box = HBox(children=[fruit_label, fruit_list])
fruit_box<jupyter_output><empty_output><jupyter_text>VBox This will display the widgets (or boxes) vertically<jupyter_code>num_label = HTML(
    value = 'Choose the number of fruits:&nbsp;&nbsp;'
)

num_options = IntSlider(min=1, max=20) 

num_box = HBox(children=(num_label, num_options))

type_label = HTML(
    value = 'Select the type of fruit:&nbsp;&nbsp;'
)

type_options = RadioButtons(
    options=('Under-ripe', 'Ripe', 'Rotten')
)

type_box = HBox(children=(type_label, type_options))

fruit_vbox = VBox(children=(fruit_box, num_box, type_box))
fruit_vbox<jupyter_output><empty_output><jupyter_text>Retrieving Values from a Box<jupyter_code>box_values = {}
# the elements in a box can be accessed using the children attribute
for index, box in enumerate(fruit_vbox.children):
    for child in box.children:
        if type(child) != ipywidgets.widgets.widget_string.HTML:
            if index == 0:
                print("The selected fruit is:", child.value)
                box_values['fruit'] = child.value
            elif index == 1:
                print("The select number of fruits is: ", str (child.value))
                box_values['count'] = child.value
            elif index == 2:
                print("The selected type of fruit is: ", str(child.value))
                box_values['type'] = child.value
box_values<jupyter_output><empty_output><jupyter_text>Specify Layout of the Widgets/Boxes<jupyter_code>form_item_layout = Layout(
    display = 'flex',
    flex_flow = 'row',
    justify_content = 'space-between',
    width = '70%',
    align_items = 'initial',
)

veggie_label = HTML(
    value='Select a vegetable from the list:&nbsp;&nbsp;',
    layout=Layout(width='20%', height='65px', border='solid 1px')
) 

veggie_options = Dropdown(
    options=['corn', 'lettuce', 'tomato', 'potato', 'spinach'], 
    layout=Layout(width='30%', height='65px')
) 

veggie_box = HBox(
    children=(veggie_label, veggie_options),
    layout=Layout(width='100%', border='solid 2px', height='100px'))

veggie_box<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11222163773452569, 'fraction_numerical': 0.007715237594248641, 'mean_word_length': 5.437923250564334, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '473911', 'license': 'Unlicense', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1791, 'n_words': 541, 'path': 'Module - Interactive User Input with ipywidgets.ipynb', 'repo_name': 'thundercats-py/python_training', 'size': '5703'}