{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Some examples usign `ipywidgets`\n", "\n", "Install ipywidgets with:\n", "\n", "`pip install ipywidgets`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact, interactive, fixed, interact_manual" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here some standard python packages to show the usage:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Automatic (and simple) implementation using `interact`\n", "\n", "The simplest possibility is to use the `interact()`-function, with a passed function. Arguments are automatically adjusted to interaction widgets:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def sum_func(a=1.,b=3.):\n", " return a+b" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "63d3ee87532d410e9a856395e8092a28", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=1.0, description='a', max=3.0, min=-1.0), FloatSlider(value=3.0, descr…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "interact(sum_func)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To define the ranges:\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "88f9480a4feb417c8f20dce568c8bf26", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=1.0, description='a', max=5.0, min=1.0), FloatSlider(value=20.0, descr…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "interact(sum_func, a=(1,5,0.1), b=(20,30,.05))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Booleans\n", "\n", "Booleans are transfered to check boxes:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def plot_data(show_points=False, show_line=False):\n", " xvals = np.arange(-5,5.01,.5)\n", " yvals = xvals ** 2\n", " \n", " plt.figure()\n", " plt.xlim([-5,5])\n", " plt.ylim([0,25])\n", " \n", " if show_line:\n", " plt.plot(xvals, yvals)\n", " \n", " if show_points:\n", " plt.plot(xvals, yvals, 'r.')\n", " \n", "\n", " plt.grid()\n", " plt.show()\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "18fc59c27e024ec7ae4a62e93a08fd8c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Checkbox(value=False, description='show_points'), Checkbox(value=False, description='sho…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "interact(plot_data)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import HTML\n", "\n", "HTML('''\n", "
''')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.9.7" } }, "nbformat": 4, "nbformat_minor": 4 }