{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "BK-O7y-k-lC0" }, "source": [ "# **Jupyter Widgets**\n", "\n", "## This is what I learned from SciPy 2022 Conference held in Austin during July. I met Itay Dafna from Bloomberg who showed how to intergrate Jupyter Widgets in presenting market data.\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", "source": [ "!pip install ipywidgets" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uXKUYP98-oYN", "outputId": "1c2a67a0-f178-4839-c771-a6a0d8074b4f" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Requirement already satisfied: ipywidgets in /usr/local/lib/python3.7/dist-packages (7.7.1)\n", "Requirement already satisfied: widgetsnbextension~=3.6.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (3.6.1)\n", "Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (1.1.1)\n", "Requirement already satisfied: ipython>=4.0.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (5.5.0)\n", "Requirement already satisfied: traitlets>=4.3.1 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (5.1.1)\n", "Requirement already satisfied: ipykernel>=4.5.1 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (4.10.1)\n", "Requirement already satisfied: ipython-genutils~=0.2.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets) (0.2.0)\n", "Requirement already satisfied: tornado>=4.0 in /usr/local/lib/python3.7/dist-packages (from ipykernel>=4.5.1->ipywidgets) (5.1.1)\n", "Requirement already satisfied: jupyter-client in /usr/local/lib/python3.7/dist-packages (from ipykernel>=4.5.1->ipywidgets) (5.3.5)\n", "Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.4 in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (1.0.18)\n", "Requirement already satisfied: pygments in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (2.6.1)\n", "Requirement already satisfied: pickleshare in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (0.7.5)\n", "Requirement already satisfied: pexpect in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (4.8.0)\n", "Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (57.4.0)\n", "Requirement already satisfied: decorator in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (4.4.2)\n", "Requirement already satisfied: simplegeneric>0.8 in /usr/local/lib/python3.7/dist-packages (from ipython>=4.0.0->ipywidgets) (0.8.1)\n", "Requirement already satisfied: wcwidth in /usr/local/lib/python3.7/dist-packages (from prompt-toolkit<2.0.0,>=1.0.4->ipython>=4.0.0->ipywidgets) (0.2.5)\n", "Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.7/dist-packages (from prompt-toolkit<2.0.0,>=1.0.4->ipython>=4.0.0->ipywidgets) (1.15.0)\n", "Requirement already satisfied: notebook>=4.4.1 in /usr/local/lib/python3.7/dist-packages (from widgetsnbextension~=3.6.0->ipywidgets) (5.3.1)\n", "Requirement already satisfied: jinja2 in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (2.11.3)\n", "Requirement already satisfied: jupyter-core>=4.4.0 in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (4.11.1)\n", "Requirement already satisfied: terminado>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.13.3)\n", "Requirement already satisfied: Send2Trash in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (1.8.0)\n", "Requirement already satisfied: nbformat in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (5.4.0)\n", "Requirement already satisfied: nbconvert in /usr/local/lib/python3.7/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (5.6.1)\n", "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from jupyter-client->ipykernel>=4.5.1->ipywidgets) (2.8.2)\n", "Requirement already satisfied: pyzmq>=13 in /usr/local/lib/python3.7/dist-packages (from jupyter-client->ipykernel>=4.5.1->ipywidgets) (23.2.0)\n", "Requirement already satisfied: ptyprocess in /usr/local/lib/python3.7/dist-packages (from terminado>=0.8.1->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.7.0)\n", "Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from jinja2->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (2.0.1)\n", "Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.4)\n", "Requirement already satisfied: defusedxml in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.7.1)\n", "Requirement already satisfied: testpath in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.6.0)\n", "Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (1.5.0)\n", "Requirement already satisfied: mistune<2,>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.8.4)\n", "Requirement already satisfied: bleach in /usr/local/lib/python3.7/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (5.0.1)\n", "Requirement already satisfied: jsonschema>=2.6 in /usr/local/lib/python3.7/dist-packages (from nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (4.3.3)\n", "Requirement already satisfied: fastjsonschema in /usr/local/lib/python3.7/dist-packages (from nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (2.16.1)\n", "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.18.1)\n", "Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (4.12.0)\n", "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (4.1.1)\n", "Requirement already satisfied: importlib-resources>=1.4.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (5.9.0)\n", "Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (22.1.0)\n", "Requirement already satisfied: zipp>=3.1.0 in /usr/local/lib/python3.7/dist-packages (from importlib-resources>=1.4.0->jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (3.8.1)\n", "Requirement already satisfied: webencodings in /usr/local/lib/python3.7/dist-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.5.1)\n" ] } ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ePP_gI0a-lC6" }, "outputs": [], "source": [ "import ipywidgets as widgets" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 49, "referenced_widgets": [ "d4ac185b1c0c48bbb8818f15de94dbbb", "59435ea7b6664dbe9dff7fb71eb0e88c", "09a3f3920b304077a48b258ec732e958" ] }, "id": "tBl1Nze7-lC7", "outputId": "a87b3bf6-52ca-49e6-e8da-ba420022beab" }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "FloatSlider(value=8.0, description='Input:', max=10.0, min=5.0)" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "d4ac185b1c0c48bbb8818f15de94dbbb" } }, "metadata": {} } ], "source": [ "slider = widgets.FloatSlider(\n", " value=8.0,\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": { "id": "0FowCaEm-lC-" }, "outputs": [], "source": [ "import ipywidgets as ipw" ] }, { "cell_type": "code", "source": [ "def f(x):\n", " print(x * x)" ], "metadata": { "id": "UqXLBiAu_M6A" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 67, "referenced_widgets": [ "8df35378a8184679859c34a17344787a", "49677f28b8e54d20a4b12b4afd383440", "c501fd9d596b4c3d8de3782a54b5bce8", "0864fd7625e542c5aeab6c93c0d36c6b", "91eb40cb7a8d4ff2a566605c160637f7", "61e20787b8e74c23a0c7eaa58df79415", "0e353e924e3845619d8ae14f55c48800" ] }, "id": "slBUihn4-lC_", "outputId": "dec25497-27a4-4fad-8923-c94f694cb718" }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "interactive(children=(IntSlider(value=50, description='x'), Output()), _dom_classes=('widget-interact',))" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "8df35378a8184679859c34a17344787a" } }, "metadata": {} } ], "source": [ "widgets.interact(f, x=(0, 100, 1));" ] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "zLXbZhw4pHv7" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "pip install quandl" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "oxTbNBXCpHt8", "outputId": "85c04249-c360-46b5-f0ad-9138530cee62" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Requirement already satisfied: quandl in /usr/local/lib/python3.7/dist-packages (3.7.0)\n", "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from quandl) (1.15.0)\n", "Requirement already satisfied: python-dateutil in /usr/local/lib/python3.7/dist-packages (from quandl) (2.8.2)\n", "Requirement already satisfied: more-itertools in /usr/local/lib/python3.7/dist-packages (from quandl) (8.13.0)\n", "Requirement already satisfied: requests>=2.7.0 in /usr/local/lib/python3.7/dist-packages (from quandl) (2.23.0)\n", "Requirement already satisfied: pandas>=0.14 in /usr/local/lib/python3.7/dist-packages (from quandl) (1.3.5)\n", "Requirement already satisfied: numpy>=1.8 in /usr/local/lib/python3.7/dist-packages (from quandl) (1.21.6)\n", "Requirement already satisfied: inflection>=0.3.1 in /usr/local/lib/python3.7/dist-packages (from quandl) (0.5.1)\n", "Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.14->quandl) (2022.1)\n", "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.0->quandl) (2.10)\n", "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.0->quandl) (3.0.4)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.0->quandl) (1.24.3)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.0->quandl) (2022.6.15)\n" ] } ] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "8c96W00RpHrZ" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "\n", "import quandl as ql\n", "%matplotlib inline\n", "\n", "yield_ = ql.get(\"USTREASURY/YIELD\")\n", "yield_" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 455 }, "id": "NQbSklVOhf9x", "outputId": "5861eb49-47c6-423f-9e84-42e6196a2af0" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " 1 MO 2 MO 3 MO 6 MO 1 YR 2 YR 3 YR 5 YR 7 YR 10 YR \\\n", "Date \n", "1990-01-02 NaN NaN 7.83 7.89 7.81 7.87 7.90 7.87 7.98 7.94 \n", "1990-01-03 NaN NaN 7.89 7.94 7.85 7.94 7.96 7.92 8.04 7.99 \n", "1990-01-04 NaN NaN 7.84 7.90 7.82 7.92 7.93 7.91 8.02 7.98 \n", "1990-01-05 NaN NaN 7.79 7.85 7.79 7.90 7.94 7.92 8.03 7.99 \n", "1990-01-08 NaN NaN 7.79 7.88 7.81 7.90 7.95 7.92 8.05 8.02 \n", "... ... ... ... ... ... ... ... ... ... ... \n", "2022-01-31 0.03 0.13 0.22 0.49 0.78 1.18 1.39 1.62 1.75 1.79 \n", "2022-02-01 0.04 0.10 0.19 0.48 0.78 1.18 1.39 1.63 1.76 1.81 \n", "2022-02-02 0.04 0.10 0.19 0.45 0.76 1.16 1.38 1.60 1.74 1.78 \n", "2022-02-03 0.03 0.15 0.20 0.48 0.78 1.19 1.42 1.66 1.78 1.82 \n", "2022-02-04 0.05 0.12 0.23 0.56 0.89 1.31 1.55 1.78 1.90 1.93 \n", "\n", " 20 YR 30 YR \n", "Date \n", "1990-01-02 NaN 8.00 \n", "1990-01-03 NaN 8.04 \n", "1990-01-04 NaN 8.04 \n", "1990-01-05 NaN 8.06 \n", "1990-01-08 NaN 8.09 \n", "... ... ... \n", "2022-01-31 2.17 2.11 \n", "2022-02-01 2.19 2.12 \n", "2022-02-02 2.17 2.11 \n", "2022-02-03 2.20 2.14 \n", "2022-02-04 2.29 2.23 \n", "\n", "[8032 rows x 12 columns]" ], "text/html": [ "\n", "
\n", " | 1 MO | \n", "2 MO | \n", "3 MO | \n", "6 MO | \n", "1 YR | \n", "2 YR | \n", "3 YR | \n", "5 YR | \n", "7 YR | \n", "10 YR | \n", "20 YR | \n", "30 YR | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
Date | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
1990-01-02 | \n", "NaN | \n", "NaN | \n", "7.83 | \n", "7.89 | \n", "7.81 | \n", "7.87 | \n", "7.90 | \n", "7.87 | \n", "7.98 | \n", "7.94 | \n", "NaN | \n", "8.00 | \n", "
1990-01-03 | \n", "NaN | \n", "NaN | \n", "7.89 | \n", "7.94 | \n", "7.85 | \n", "7.94 | \n", "7.96 | \n", "7.92 | \n", "8.04 | \n", "7.99 | \n", "NaN | \n", "8.04 | \n", "
1990-01-04 | \n", "NaN | \n", "NaN | \n", "7.84 | \n", "7.90 | \n", "7.82 | \n", "7.92 | \n", "7.93 | \n", "7.91 | \n", "8.02 | \n", "7.98 | \n", "NaN | \n", "8.04 | \n", "
1990-01-05 | \n", "NaN | \n", "NaN | \n", "7.79 | \n", "7.85 | \n", "7.79 | \n", "7.90 | \n", "7.94 | \n", "7.92 | \n", "8.03 | \n", "7.99 | \n", "NaN | \n", "8.06 | \n", "
1990-01-08 | \n", "NaN | \n", "NaN | \n", "7.79 | \n", "7.88 | \n", "7.81 | \n", "7.90 | \n", "7.95 | \n", "7.92 | \n", "8.05 | \n", "8.02 | \n", "NaN | \n", "8.09 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
2022-01-31 | \n", "0.03 | \n", "0.13 | \n", "0.22 | \n", "0.49 | \n", "0.78 | \n", "1.18 | \n", "1.39 | \n", "1.62 | \n", "1.75 | \n", "1.79 | \n", "2.17 | \n", "2.11 | \n", "
2022-02-01 | \n", "0.04 | \n", "0.10 | \n", "0.19 | \n", "0.48 | \n", "0.78 | \n", "1.18 | \n", "1.39 | \n", "1.63 | \n", "1.76 | \n", "1.81 | \n", "2.19 | \n", "2.12 | \n", "
2022-02-02 | \n", "0.04 | \n", "0.10 | \n", "0.19 | \n", "0.45 | \n", "0.76 | \n", "1.16 | \n", "1.38 | \n", "1.60 | \n", "1.74 | \n", "1.78 | \n", "2.17 | \n", "2.11 | \n", "
2022-02-03 | \n", "0.03 | \n", "0.15 | \n", "0.20 | \n", "0.48 | \n", "0.78 | \n", "1.19 | \n", "1.42 | \n", "1.66 | \n", "1.78 | \n", "1.82 | \n", "2.20 | \n", "2.14 | \n", "
2022-02-04 | \n", "0.05 | \n", "0.12 | \n", "0.23 | \n", "0.56 | \n", "0.89 | \n", "1.31 | \n", "1.55 | \n", "1.78 | \n", "1.90 | \n", "1.93 | \n", "2.29 | \n", "2.23 | \n", "
8032 rows × 12 columns
\n", "