<jupyter_start><jupyter_text>Jupyter for Analysts - Notebook 3  Welcome to Jupyter for Analysts Notebook 3!!        So by now you have queried nbgallery for notebooks, interacted with code in notebooks, found a notebook on nbgallery, and ran at least  2 notebooks!           This is seriously AMAZING progress and you should be incredibly proud. Being able to use Jupyter is a huge advantage for you! We promise, there will be notebooks out there that you will want to use for your work! :)         This notebook is going to dive in to more interaction with the code you are running. We will be querying ---service --- using 'widgets' . You have actually already seen a widget but probably didn't notice. That is perfectly OK! We are going to show you a couple more widgets you might see in notebooks.                             Let's start by looking at some basic widgets. Run the new few cells below to see widgets in action! Ok. First step, let's import the widgets in to this notebook.<jupyter_code>from IPython import display as disp
from IPython.display import display, HTML
from ipywidgets import widgets, Layout
better_layout =  {'height' : '100px', 'width':'75%' }<jupyter_output><empty_output><jupyter_text>Now start running the code below to play with widgets!The first one you will see is the string widget. Notice that there is a textbox for you to type in. Go ahead type something (anything you want) in the box and watch the text JUST below the box change! :)  Don't worry about the actual output here.<jupyter_code>from ipywidgets import *
def f(x):
    print(x)
interact(f, x="Type stuff here")<jupyter_output><empty_output><jupyter_text>Thank u, next. Ok, run the code below to see a 'progress bar' widget in action. This particular progress bar is just counting up to a number, but in other notebooks this could be used to show you how much longer you have until your results are ready! Notice how it takes until the progress bar is done for the asterisk to turn into a number!<jupyter_code>import time
progress_bar = widgets.FloatProgress(min=0.0, max=2.0)
display(progress_bar)
x = 0
while x != 2:
    x+=.25
    progress_bar.value = x
    time.sleep(1)
print("Complete!")<jupyter_output><empty_output><jupyter_text>As I'm writing this notebook, there is a big Oreo cookie discussion going on in the office. (How much cream is there in the regular, double, mega, and most stufed? Is there a ratio?) Sorry - I sidetracked. But now I am hungry for cookies! How many should I eat?  Run the code below and let me know using the slider widget!<jupyter_code>widgets.FloatSlider(min=0, max=10, step=0.5,description='Number of Cookies' )<jupyter_output><empty_output><jupyter_text>Okay... that's fine, but what if you let me choose a range of the number of cookies I could eat? (Run the next cell!)<jupyter_code>widgets.FloatRangeSlider(min=0, max=10, step=0.5, description='Cookie Range')<jupyter_output><empty_output><jupyter_text>Do you think I *actually* ate within the range of cookies you gave me? Run the next cell, and check True or False.  Note: You do not have to run this cell again once you click in the check box. Just run the cell after that to see my response!<jupyter_code>f = widgets.Checkbox(description='False?:', value=False)
t = widgets.Checkbox(description='True?:', value=True)
display(f, t)
if f.value==True:
    print("Are you sure?")
else:
    print("Yeah.... probably :)")<jupyter_output><empty_output><jupyter_text>Exercise 4: Who's in space right now? Querying an API Take a stab at running the code below, which is broken up in to a few cells. This code goes to ---service--- and outputs the information you want (using widgets!)Okay... see how far you make it on your own.  YOU CAN DO IT! Note: Not every cell will return output. Wait for the asterisk to go away, then run the next cell!<jupyter_code>#imports a library and requests access to the Open Notify API from NASA
import requests
people = requests.get('http://api.open-notify.org/astros.json')
people = people.json()
#creates widgets to prompt user for information they want from the Open Notify API
name = widgets.Checkbox(description='Full Name', value=True)
space_craft = widgets.Checkbox(description='Space Craft', value=True)
total_people = widgets.Checkbox(description='Total Number of People in Space', value=True)

#displays the widgets
display(name, space_craft, total_people)
#checks to see if astronaut name was picked. if it was picked, then it tells you name of astronauts currently in space
if name.value==True:
    for person in people['people']:
        print("Astronaut:",person['name'])

#prints a blank line
print()

#checks to see if space craft name was picked. if it was picked, then it tells you name of 
#space craft currently in space
if space_craft.value == True:
    for person in people['people']:
        print("Space Craft for", person['name'], ":", person['craft'])
        
#prints a blank line
print()

#checks to see if total number of people in space was picked. if it was picked, then it tells you the total number of 
#astronauts currently in space
if total_people.value == True:
    print("Total Number of People in Space:", people['number'])

#checks to see if no value was selected. if no value selected, an error message is printed 
#to ask user to select a value
if total_people.value==False and space_craft.value==False and name.value==False:
    print("Please check an item in the widget box above before running this code cell.")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08041386821564712, 'fraction_numerical': 0.005082592121982211, 'mean_word_length': 4.515515515515515, 'pattern_counts': {'":': 1, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '541579', 'license': 'MIT', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1544, 'n_words': 762, 'path': 'J4A Notebook 3 - Widgets & Querying an API.ipynb', 'repo_name': 'nbgallery/Jupyter4Analysts', 'size': '5509'}