{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "\n", "The notebook comes alive with the interactive widgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Speeding up the bottleneck in the REPL\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "9*9" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def f(x):\n", " print(x * x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f(9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from ipywidgets import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interact(f, x=(0, 100));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interactive Jupyter widgets\n", "\n", "A Python widget is an object that represents a control on the front end, like a slider. A single control can be displayed multiple times - they all represent the same python object." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slider = FloatSlider(\n", " value=7.5,\n", " min=5.0,\n", " max=10.0,\n", " step=0.1,\n", " description='Input:',\n", ")\n", "\n", "slider" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slider" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The control attributes, like its value, are automatically synced between the frontend and the kernel." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slider.value" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "slider.value = 8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can trigger actions in the kernel when a control value changes by \"observing\" the value. Here we set a global variable when the slider value changes." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "scrolled": true }, "outputs": [], "source": [ "square = slider.value * slider.value\n", "def handle_change(change):\n", " global square\n", " square = change.new * change.new\n", "slider.observe(handle_change, 'value')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "square" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can link control attributes and lay them out together." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "text = FloatText(description='Value')\n", "link((slider, 'value'), (text, 'value'))\n", "VBox([slider, text])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Jupyter widgets as a framework\n", "\n", "Jupyter widgets forms a framework for representing python objects interactively. Some large open-source interactive controls based on Jupyter widgets include:\n", "\n", " - bqplot - 2d plotting library\n", " - pythreejs - low-level 3d graphics library\n", " - ipyvolume - 3d plotting and volume rendering\n", " - ipyleaflet - interactive maps\n", " - ipywebrtc - video streaming\n", " - ipysheet - interactive spreadsheets\n", " - ..." ] } ], "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.7.3" }, "widgets": { "state": { "0cb2d81d826b4e25aba3b1d39711ef3f": { "views": [ { "cell_index": 6 } ] }, "2cfe6521f9834ca69e54518716104cd2": { "views": [ { "cell_index": 8 }, { "cell_index": 9 }, { "cell_index": 12 } ] }, "889356d59ac543a49da33d134d791c3f": { "views": [ { "cell_index": 11 } ] } }, "version": "2.0.0-dev" } }, "nbformat": 4, "nbformat_minor": 1 }