Created
July 18, 2018 21:57
-
-
Save awade/c17159d730a342339ed17c83a7669509 to your computer and use it in GitHub Desktop.
Notebook showing basics of widget slider usage. Includes example of passing object through fixed widget method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "run_control": { | |
| "frozen": false, | |
| "read_only": false | |
| } | |
| }, | |
| "source": [ | |
| "# Demo most basic widget usage" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Import widget tools\n", | |
| "from ipywidgets import interact, interactive, fixed, interact_manual\n", | |
| "import ipywidgets as widgets\n", | |
| "\n", | |
| "import numpy as np\n", | |
| "import matplotlib.pyplot as plt \n", | |
| "\n", | |
| "%matplotlib inline" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# easiest widgets use the interact decorator directly on a function\n", | |
| "@interact(k=widgets.FloatSlider(value=4.0e0,\n", | |
| " min=0.,\n", | |
| " max=15.,\n", | |
| " step=0.1,\n", | |
| " description=\"k\",\n", | |
| " continuous_update=False))\n", | |
| "def sinPlotx(k):\n", | |
| " x = np.linspace(0., 10., 1000)\n", | |
| " y = np.sin(k * x)\n", | |
| " plt.plot(x, y)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# basic example parsing an external class/variable/object as fixed object\n", | |
| "class testClass():\n", | |
| " def __init__(self, testVar):\n", | |
| " self.internalVar = testVar\n", | |
| "\n", | |
| "\n", | |
| "def sinPlotter(k, testClass):\n", | |
| " x = np.linspace(0., 10., 1000)\n", | |
| " y = np.sin(k * x)\n", | |
| " plt.plot(x, y)\n", | |
| " \n", | |
| "# make the widget objects\n", | |
| "kSlider=widgets.FloatSlider(value=4.0e0,\n", | |
| " min=0.,\n", | |
| " max=15.,\n", | |
| " step=0.1,\n", | |
| " description=\"k\",\n", | |
| " continuous_update=False)\n", | |
| "\n", | |
| "fixedClass = widgets.fixed(testClass) # fixed widget for passing into function without slider\n", | |
| " \n", | |
| "# Put together the UI\n", | |
| "ui = widgets.HBox([kSlider]) # make UI \n", | |
| "out = widgets.interactive_output(sinPlotter, {'k': kSlider, 'testClass': fixedClass})\n", | |
| "display(ui, out)" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python2 (pyenv)", | |
| "language": "python", | |
| "name": "pyenv" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.14" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment