{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to use ipywidgets to make your Jupyter notebook interactive\n", "Have you ever created a Python-based Jupyter notebook and analyzed data that you want to explore in a number of different ways? For example, you may want to look at a plot of data, but filter it ten different ways. What are your options to view these ten different results?\n", "\n", "1. Copy and paste a cell, changing the filter for each cell, then executing the cell. You will end up with ten different cells with ten different values.\n", "1. Modify the same cell, execute it and view the results, then modify it again, ten times.\n", "1. Parameterize the notebook (using something like [Papermill](https://papermill.readthedocs.io/en/latest/) and execute the notebook with ten different sets of parameters.\n", "1. Some combination of the above.\n", "\n", "These all are non-ideal if we want quick interaction and the ability to explore the data. They are also prone to typing errors. They may work great for the original developer of a notebook, but allowing a user who doesn't undestand Python syntax to modify variables and re-execute cells may not be the best option. What if you could just give the user a simple form, with a button, and they could modify the form and see the results they want?\n", "\n", "It turns out you can do this pretty easily right in Jupyter, without creating a full webapp. This is possible with ```ipywidgets```, also known just as widgets. I'll show you the basics in this article of building a few simple forms to view and analyze some data.\n", "\n", "## What are widgets?\n", "Jupyter widgets are special bits of code that will embed JavaScript and html in your notebook and present a visual representation in your brower when executed in a notebook. These components allow a user to interact with the widgets. The widgets can be configured to execute code on certain actions, allowing you to update cells without a user having to re-execute them or even modify any code.\n", "\n", "## Getting started\n", "First, you need to make sure that ```ipywidgets``` is installed in your environment. This will depend a bit on which Jupyter environment you are using. For older Jupyter and JupyterLab installs, make sure to check the details in [the docs](https://ipywidgets.readthedocs.io/en/latest/user_install.html). But for a basic install, just use pip\n", "\n", "```\n", "pip install ipywidgets\n", "```\n", "\n", "or for conda\n", "\n", "```\n", "conda install -c conda-forge ipywidgets\n", "```\n", "\n", "This should be all that you need to do in most situations to get things running. \n", "\n", "## Example\n", "Instead of going through all the widgets and getting into details right away, let's grab some interesting data and explore it manually. Then we'll use widgets to make a more interactive version of some of this data exploration. Let's grab some data from the [Chicago Data Portal](https://data.cityofchicago.org/Community-Economic-Development/Business-Licenses-Current-Active/uupf-x98q) - specifically their dataset of current active business licenses. Note that if you just run the code as below, you'll only get 1000 rows of data. Check the documentation on how to to grab all the data. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "df = pd.read_csv('https://data.cityofchicago.org/resource/uupf-x98q.csv')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | LEGAL NAME | \n", "ZIP CODE | \n", "BUSINESS ACTIVITY | \n", "
---|---|---|---|
0 | \n", "DE LA TORRE AUTO SALES INC. | \n", "60621 | \n", "Motor Vehicle Repair - Engine and Transmissio... | \n", "
1 | \n", "SITEL ARM CORP. | \n", "33131 | \n", "Debt Collecting - Administrative Commercial Of... | \n", "
2 | \n", "SEVEN NINE ELEVEN FOOD MART, INC. | \n", "60632 | \n", "Retail Sale of Tobacco | \n", "
3 | \n", "WARM BELLY BAKERY, LLC | \n", "60607 | \n", "Sale of Food Prepared Onsite With Dining Area | \n", "
4 | \n", "VICKIE, INC. | \n", "60639 | \n", "Tavern - Consumption of Liquor on Premise | \n", "
\n", " | LEGAL NAME | \n", "ADDRESS | \n", "LICENSE TERM EXPIRATION DATE | \n", "
---|---|---|---|
8 | \n", "CENVEO WORLDWIDE LIMITED | \n", "3001 N ROCKWELL ST | \n", "12/15/2022 | \n", "
580 | \n", "SUPAROSSA ON WESTERN INC. | \n", "3737 N WESTERN AVE | \n", "12/15/2022 | \n", "
643 | \n", "ERRO INC | \n", "2933 W MONTROSE AVE 1 # | \n", "12/15/2022 | \n", "
640 | \n", "JAMES INSTRUMENTS INC | \n", "3727 N KEDZIE AVE 1ST | \n", "12/15/2022 | \n", "
609 | \n", "MANJU J SUTHAR | \n", "3011 W IRVING PARK RD 1ST | \n", "12/15/2022 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "
542 | \n", "DANIEL J ACOSTA | \n", "2827 N MILWAUKEE AVE # 1ST | \n", "07/15/2021 | \n", "
329 | \n", "GRANITE STYLE DESIGN COMPANY | \n", "3111 N ROCKWELL ST | \n", "07/15/2021 | \n", "
405 | \n", "IAN HEPBURN | \n", "[REDACTED FOR PRIVACY] | \n", "07/15/2021 | \n", "
181 | \n", "LOGAN-AVONDALE VFW # 2978 | \n", "3007 N KEDZIE AVE 1ST | \n", "03/15/2022 | \n", "
786 | \n", "CUTTING EDGES BEAUTY STUDIO, INC. | \n", "2941 W IRVING PARK RD 1 | \n", "01/15/2023 | \n", "
78 rows × 3 columns
\n", "