{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Who's responsible?\n", "\n", "Archives New Zealand's [Archway](https://archway.archives.govt.nz/) database divides government activities up into 303 functions. Over time, different agencies have been made responsible for these functions, and it can be interesting to track how these responsibilities have shifted. \n", "\n", "This notebook uses [data about functions harvested from Archway](harvesting_functions_from_archway.ipynb) to create a a simple visualisation of the agencies responsible for a selected function." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%%capture\n", "import ipywidgets as widgets\n", "from IPython.display import display, HTML\n", "import json\n", "import altair as alt\n", "import pandas as pd\n", "\n", "alt.renderers.enable('notebook')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Load the harvested functions data from a JSON file\n", "with open('data/functions.json', 'r') as json_file:\n", " data = json.load(json_file)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Create a dictionary to use in the dropdown\n", "options = {f['term']: f for f in data}" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "af2caec1437c47dfb1dab3f2f3101730", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(Label(value='Select a function:'), Dropdown(options={'accident insurance': {'id': 'F0188', 'ter…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9c39697b6cf7405db24e71c6fd72636a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def make_chart(change):\n", " # Clear current output\n", " out.clear_output(wait=True)\n", " # Get the currently selected term from the dropdown\n", " term = change['new']\n", " # Get the agencies responsible for the selected function\n", " agencies = term['agencies_responsible']\n", " if agencies:\n", " # Convert to a dataframe\n", " df = pd.DataFrame(agencies)\n", " # Set some defualts for missing dates\n", " df['start_date'] = df['start_date'].replace('', '1853')\n", " df['end_date'] = df['end_date'].replace('', '2019')\n", " # Create a Gannt style chart\n", " chart = alt.Chart(df).mark_bar(size=20).encode(\n", " x=alt.X('start_date:T', axis=alt.Axis(format='%Y', title='Dates agency was responsible for function'), scale=alt.Scale(nice=True)),\n", " x2=alt.X('end_date:T'),\n", " y=alt.Y('entity', scale=alt.Scale(rangeStep=30), title='Agency'),\n", " color=alt.Color('entity', legend=None),\n", " tooltip=[alt.Tooltip('entity', title='Agency'), alt.Tooltip('start_date', title='From', timeUnit='year'), alt.Tooltip('end_date', title='To', timeUnit='year')]\n", " ).properties(\n", " width=700\n", " )\n", " with out:\n", " display(HTML('

Agencies responsible for ‘{}’

'.format(term['term'])))\n", " display(chart)\n", " else:\n", " with out:\n", " display(HTML('

No agencies responsible for ‘{}’

'.format(term['term'])))\n", "\n", "# This is where the chart will be displayed\n", "out = widgets.Output()\n", "\n", "# Create the dropdown\n", "term = widgets.Dropdown(\n", " options=options,\n", " value=None,\n", " disabled=False,\n", ")\n", "\n", "# Making a selection from the dropdown will automatically run 'make_chart'\n", "term.observe(make_chart, names='value')\n", "\n", "display(widgets.HBox([widgets.Label('Select a function:'), term]))\n", "display(out)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }