{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Exercises\n", "\n", "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.\n", "\n", "## We do not anticipate that you will have time to complete all of the exercises\n", "\n", "That is why we provide solutions!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "\n", "import numpy as np\n", "\n", "import ipywidgets as widgets\n", "from ipywidgets import interact, interactive " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## interact/interactive only" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reverse some text\n", "\n", "Write a function that takes text as an input and reverses the text, and uses `interact` or `interactive` to generate the input field. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %load solutions/interact-basic-list/reverse-text.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Make a plot\n", "\n", "Pick a function of interest to you that depends on a couple of parameters and use `interact` to generate controls for the parameters. \n", "\n", "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$.\n", "\n", "Use either the `interact` function or the `@interact` decorator." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %load solutions/interact-basic-list/plot-function.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A more extended example of interact\n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## interact/interactive and/or other widgets\n", "\n", "Some of the exercises below might be possible only with interact and interactive." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Choose two widgets and link their values\n", "\n", "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).\n", "\n", "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.\n", "\n", "Selection widgets:\n", "\n", "+ `Dropdown`\n", "+ `RadioButtons`\n", "+ `Select`\n", "+ `SelectionSlider`\n", "+ `SelectionRangeSlider`\n", "+ `ToggleButtons`\n", "+ `SelectMultiple`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %load solutions/interact-basic-list/sample-linked-widgets.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boxes\n", "\n", "Create any three widgets, place them in a `VBox`, and display it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %load solutions/interact-basic-list/widgets-in-a-box.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Blank canvas\n", "\n", "The cell below is for you to try out antyhing you would like. Be creative!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "widgets-tutorial", "language": "python", "name": "widgets-tutorial" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }