{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![The Deep Purple Network Notebook App](https://raw.githubusercontent.com/greggtdd/DeepPurpleNetwork/master/app_images/dpnetwork_graph_banner.png)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "######################################################################################################\n", "# Project: The Deep Purple Network. #\n", "# (CC) 2020 Made by Gregorio Tedde just for analysis purpose. #\n", "# #\n", "# #\n", "# PLEASE, DO NOT EDIT! #\n", "# #\n", "# #\n", "# Work License: Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License #\n", "# Libraries licenses in environment.yml #\n", "# Git Repo: https://github.com/greggtdd/DeepPurpleNetwork #\n", "# Docs for widgets handling: https://bit.ly/2W2JPGm #\n", "# Info: greggtedde@gmail.com #\n", "######################################################################################################" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "import requests\n", "import pandas as pd\n", "from PIL import Image\n", "from io import BytesIO\n", "import ipywidgets as widgets\n", "from ipywidgets import Layout\n", "from graph_bouncer import DataFramer, Networker\n", "from IPython.display import display, clear_output" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Data frame handling\n", "dp = DataFramer('https://raw.githubusercontent.com/greggtdd/DeepPurpleNetwork/master/dp_union_edges.csv')\n", "dp.upload_df()\n", "dp.density_sources()\n", "dp.artist_research()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Init state for the dropdown menu\n", "artist = \"Select an artist\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "def unique_artist_source(array):\n", " \"\"\"\n", " Returns the unique values for\n", " a specific source written as\n", " a query in the search box.\n", " \n", " Paramters\n", " ---------\n", " array: pandas.core.series.Series\n", " Desired column from the data frame.\n", " \n", " Returns\n", " -------\n", " list\n", " \"\"\"\n", " unique = array.unique().tolist()\n", " unique.sort()\n", " unique.insert(0, artist)\n", " return unique\n", "\n", "dropdown_source = widgets.Dropdown(options = unique_artist_source(dp.data_['Source']))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Widget - search output display\n", "output_source = widgets.Output()\n", "\n", "def dropdown_handler(change):\n", " output_source.clear_output()\n", " with output_source:\n", " output_text.value = dropdown_source.value\n", " display(dp.data_[dp.data_.Source == change.new])\n", " \n", "dropdown_source.observe(dropdown_handler, names='value')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Widget - search box\n", "output_text = widgets.Text(value='',\n", " placeholder='(e.g. Jon Lord)',\n", " description=\"Artist:\",\n", " disabled=False)\n", "\n", "def callback(widget):\n", " if output_text.value in dp.sour_:\n", " dropdown_source.value = output_text.value\n", " return widget.value\n", " \n", "output_text.on_submit(callback)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Request for error message image\n", "response = requests.get('https://raw.githubusercontent.com/greggtdd/DeepPurpleNetwork/master/app_images/ritchie_message.png')\n", "error_img = Image.open(BytesIO(response.content))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Widget - rendered network graph\n", "output_graph = widgets.Output()\n", "\n", "def create_graph(_):\n", " if output_text.value in dp.sour_:\n", " dp.sub_framer(output_text.value)\n", " edges = dp.get_edge_data(sources=dp.sources_,\n", " targets=dp.targets_,\n", " weights=dp.weights_)\n", " nw = Networker(data=edges)\n", " nw.init_graph()\n", " nw.add_elements()\n", " nw.get_neighbors()\n", " nw.get_info()\n", " display(nw.show_graph())\n", " else:\n", " display(error_img)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Widget - 'Create the graph' button\n", "graph_button = widgets.Button(description='Create the graph')\n", "output_start = widgets.Output()\n", "\n", "def on_button_clicked(_):\n", " with output_start:\n", " with output_graph:\n", " output_start.clear_output()\n", " create_graph(_)\n", " \n", "graph_button.on_click(on_button_clicked)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# Layout for the input widgets\n", "item_layout = widgets.Layout(margin='20px 0 30px 150px')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Tab for the output widgets\n", "tab = widgets.Tab([output_graph, output_source])\n", "tab.set_title(0, \"Network Graph\")\n", "tab.set_title(1, \"Subset Exploration\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Enter
on your keyboard.Create the graph
.Create the graph
.