{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dashboard to explore data with ipywidget controls\n", "\n", "\n", "### Set up requirements, import packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload\n", "\n", "import os\n", "if not os.path.basename(os.getcwd()) == \"datenguide-python\":\n", " os.chdir(\"..\")\n", " \n", " \n", "from datenguidepy.query_helper import get_regions, get_statistics\n", "from datenguidepy import Query\n", "import pandas as pd\n", "import ipywidgets as widgets\n", "from ipywidgets import interact, interactive\n", "%matplotlib inline\n", "\n", "pd.set_option('display.max_colwidth', 150)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create dashboard\n", "\n", "Using ipywidgets you can browse data types here. Opening the list of regions will take a few seconds.\n", "\n", "If the result remains empty the selected region have no corresponding data for the selected statistic.\n", "\n", "First lets select the NUTS region level. Based on this value the region selector will list the available regions.\n", "\n", "\n", "#### Choosing region and statistic\n", "\n", "After selecting NUTS level choose from the list of regions and statistics." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "all_statistics = get_statistics().reset_index().loc[:,[\"short_description\", \"statistic\"]]\n", "all_statistics = sorted([tuple(x) for x in all_statistics.to_numpy()])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "aa9ac813a2684499bd0f67de74ab4415", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='nuts_level', options=(('NUTS 1', \"'nuts1'\"), ('NUTS 2', \"'nuts2'\")…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def nuts_selector(nuts_level):\n", " \n", " regions = get_regions().query(f\"level == {nuts_level}\").reset_index()[[\"name\", \"region_id\"]]\n", " regions = sorted([tuple(x) for x in regions.to_numpy()])\n", "\n", " @interact\n", " def get_statistics_dashboard(region=regions, statistic=all_statistics):\n", "\n", " q = Query.region(region)\n", "\n", " try:\n", " field = q.add_field(statistic)\n", " except KeyError as e:\n", " return(f\"Statistic not available: {e}\")\n", " \n", " return q.results()\n", "\n", "interact(nuts_selector, nuts_level=[('NUTS 1', \"'nuts1'\"), ('NUTS 2', \"'nuts2'\"), ('NUTS 3', \"'nuts3'\")]);" ] } ], "metadata": { "kernelspec": { "display_name": "datenguide", "language": "python", "name": "datenguide" }, "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.4+" } }, "nbformat": 4, "nbformat_minor": 4 }