diff --git a/dashboard/.ipynb_checkpoints/Sugartrail-checkpoint.ipynb b/dashboard/.ipynb_checkpoints/Sugartrail-checkpoint.ipynb new file mode 100644 index 0000000..ea3b8e2 --- /dev/null +++ b/dashboard/.ipynb_checkpoints/Sugartrail-checkpoint.ipynb @@ -0,0 +1,315 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b6926e35", + "metadata": {}, + "source": [ + "# Sugartrail " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f17ebdd2", + "metadata": {}, + "outputs": [], + "source": [ + "from sugartrail import mapview, api, base\n", + "import ipywidgets as widgets\n", + "from IPython.display import display, HTML\n", + "import requests\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cbc5e202", + "metadata": {}, + "outputs": [], + "source": [ + "%%capture\n", + "network = base.Network()" + ] + }, + { + "cell_type": "markdown", + "id": "1704e377", + "metadata": {}, + "source": [ + "1. Insert your [Companies House API](https://developer.company-information.service.gov.uk/how-to-create-an-application) key:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0632780b", + "metadata": {}, + "outputs": [], + "source": [ + "API_input = widgets.Text(\n", + " value='',\n", + " placeholder='Insert API Key',\n", + " disabled=False\n", + ")\n", + "\n", + "company_text = widgets.Text(\n", + " value='',\n", + " placeholder='Insert Company ID',\n", + " disabled=True\n", + ")\n", + "\n", + "auth_status = widgets.HTML(\n", + " value=\"\",\n", + ")\n", + "\n", + "auth_button = widgets.Button(description='Authenticate',button_style='success')\n", + "auth_button.on_click(lambda bt: auth())\n", + "\n", + "init_button = widgets.Button(description='Initialise',button_style='success', disabled=True)\n", + "init_button.on_click(lambda bt: init_network()) \n", + "\n", + "def auth():\n", + " auth_button.disabled=True\n", + " API_input.disabled=True\n", + " api.basic_auth.username = API_input.value\n", + " if api.test():\n", + " auth_status.value = u'\\u2705: Login successful'\n", + " company_text.disabled = False\n", + " init_button.disabled = False\n", + " else:\n", + " auth_button.disabled=False\n", + " API_input.disabled=False\n", + " auth_status.value = u'\\u274c: Invalid API key'\n", + "\n", + "display(API_input, auth_button, auth_status)" + ] + }, + { + "cell_type": "markdown", + "id": "2bd8c5be", + "metadata": {}, + "source": [ + "2. Insert the unique company registration number (CRN) for a company you would like to investigate:" + ] + }, + { + "cell_type": "markdown", + "id": "d5f9b6ad", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "128106c5", + "metadata": {}, + "outputs": [], + "source": [ + "init_status = widgets.HTML(\n", + " value=\"\",\n", + ") \n", + "\n", + "depth_selector = widgets.BoundedIntText(\n", + " value=1,\n", + " min=1,\n", + " max=5,\n", + " step=1,\n", + " disabled=True\n", + ")\n", + "\n", + "generate_network_button = widgets.Button(description='Build Network',button_style='success', disabled=True)\n", + "generate_network_button.on_click(lambda bt: generate_network()) \n", + "\n", + "def init_network():\n", + " init_button.disabled=True\n", + " company_text.disabled=True\n", + " api.basic_auth.username = API_input.value\n", + " response = api.get_company(str(company_text.value))\n", + " if response:\n", + " network.company_id = str(company_text.value)\n", + " init_status.value = u'\\u2705: Initialisation successful for ' + str(response['company_name']) \n", + " depth_selector.disabled = False\n", + " generate_network_button.disabled = False\n", + " else:\n", + " init_button.disabled=False\n", + " company_text.disabled=False\n", + "# auth_button.disabled=False\n", + "# API_input.disabled=False\n", + " init_status.value = u'\\u274c: Initialisation Failed. No records for company: ' + str(company_text.value) + ' found.'\n", + "\n", + "display(company_text, init_button, init_status)" + ] + }, + { + "cell_type": "markdown", + "id": "addafb36", + "metadata": {}, + "source": [ + "3. Select the depth of the network you would like to build:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ea0e8392", + "metadata": {}, + "outputs": [], + "source": [ + "build_status = widgets.HTML(\n", + " value=\"\",\n", + ")\n", + "\n", + "build_map_button=widgets.Button(description='Build Map',button_style='success', disabled=True)\n", + "build_map_button.on_click(lambda bt: generate_map()) \n", + "\n", + "def generate_network():\n", + " with output_box:\n", + " depth_selector.disabled = True\n", + " generate_network_button.disabled = True\n", + " network.perform_hop(depth_selector.value)\n", + " network.run_map_preprocessing()\n", + " build_map_button.disabled = False\n", + " \n", + " \n", + "output_box = widgets.Output()\n", + "display(depth_selector, generate_network_button, build_status, output_box)" + ] + }, + { + "cell_type": "markdown", + "id": "03ffce05", + "metadata": {}, + "source": [ + "4. Visualise network on a map:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6449cd96", + "metadata": {}, + "outputs": [], + "source": [ + "map_container = widgets.HTML(\n", + " value=\"\",\n", + ")\n", + "\n", + "map_data,path_table = mapview.build_map(network, clear_widget=False) \n", + "hbox_map = widgets.HBox([path_table])\n", + "vbox_map = widgets.VBox([map_data, hbox_map])\n", + "\n", + "accordion_map = widgets.Accordion(children=[vbox_map])\n", + "accordion_map.set_title(0, 'Map')\n", + "\n", + "tabs = ['Companies', 'Addresses', 'Officers', 'Company Details']\n", + "children = [widgets.Output() for tab in tabs]\n", + "tab = widgets.Tab()\n", + "tab.children = children\n", + "for i,title in enumerate(tabs):\n", + " tab.set_title(i, title)\n", + "\n", + "accordion_data = widgets.Accordion(children=[tab])\n", + "accordion_data.set_title(0, 'Data')\n", + "\n", + "html_buttons = '''\n", + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "'''\n", + "\n", + "def generate_map():\n", + " map_data,path_table = mapview.build_map(network, clear_widget=False) \n", + " hbox_map = widgets.HBox([path_table])\n", + " vbox_map.children = [map_data, hbox_map]\n", + " accordion_map.selected_index=0\n", + " accordion_data.selected_index=0\n", + " build_map_button.disabled = True\n", + " with tab.children[0]:\n", + " display(pd.DataFrame(network.company_ids))\n", + " with tab.children[1]:\n", + " display(pd.DataFrame(network.addresses))\n", + " with tab.children[2]:\n", + " display(pd.DataFrame(network.officer_ids))\n", + " with tab.children[3]:\n", + " display(pd.DataFrame(network.companies))\n", + " file = str(company_text.value) + '.json'\n", + " network.save(file, \"./\")\n", + " html_button = html_buttons.format(filename=file)\n", + " with download_link:\n", + " display(HTML(html_button))\n", + "\n", + "download_link = widgets.Output()\n", + "display(build_map_button, map_container)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "684a116e", + "metadata": {}, + "outputs": [], + "source": [ + "accordion_map" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1e328d41", + "metadata": {}, + "outputs": [], + "source": [ + "accordion_data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9224dce5", + "metadata": {}, + "outputs": [], + "source": [ + "download_link" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c8201809", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/dashboard/Sugartrail.ipynb b/dashboard/Sugartrail.ipynb index 60b91f2..ea3b8e2 100644 --- a/dashboard/Sugartrail.ipynb +++ b/dashboard/Sugartrail.ipynb @@ -281,6 +281,14 @@ "source": [ "download_link" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c8201809", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {