#!/usr/bin/env python # coding: utf-8 # # Exercises # # This collection of exercises can be done in any order, or even skipped entirely; none of the rest of the tutorial depends on it. Choose the ones that are of interest to you. Sample solutions are provided for all of them. # # ## We do not anticipate that you will have time to complete all of the exercises # # That is why we provide solutions! # In[ ]: get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt import numpy as np import ipywidgets as widgets from ipywidgets import interact, interactive # ## interact/interactive only # ### Reverse some text # # Write a function that takes text as an input and reverses the text, and uses `interact` or `interactive` to generate the input field. # In[ ]: # %load solutions/interact-basic-list/reverse-text.py # ### Make a plot # # Pick a function of interest to you that depends on a couple of parameters and use `interact` to generate controls for the parameters. # # If you don't have a favorite function, try graphing $f(x) = \sin(k x + p)$ over the $0 < x < 4\pi$, with $0.5 \le k \le 2$ and $0 \le p < 2\pi$. # # Use either the `interact` function or the `@interact` decorator. # In[ ]: # %load solutions/interact-basic-list/plot-function.py # ### A more extended example of interact # # A notebook I used in a cosmology course is [here](https://github.com/mwcraig/jupyter-notebook-intro/blob/master/supernova-data.ipynb). It used interact to allow students to fit cosmological models to high redshift supernova data. # ## interact/interactive and/or other widgets # # Some of the exercises below might be possible only with interact and interactive. # ### Choose two widgets and link their values # # Choose any two widgets from the widget list, link their values using `widgets.jslink`, and display both widgets (the `display` function can take more than one argument). # # Note: for all widgets except the section widgets below you should link the `value` of the widgets. For the selection widgets, link the `index` instead. # # Selection widgets: # # + `Dropdown` # + `RadioButtons` # + `Select` # + `SelectionSlider` # + `SelectionRangeSlider` # + `ToggleButtons` # + `SelectMultiple` # In[ ]: # %load solutions/interact-basic-list/sample-linked-widgets.py # ### Boxes # # Create any three widgets, place them in a `VBox`, and display it. # In[ ]: # %load solutions/interact-basic-list/widgets-in-a-box.py # ## Blank canvas # # The cell below is for you to try out antyhing you would like. Be creative! # In[ ]: