{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b4499af9-f198-4e4b-8c21-fdd8920b4bc9", "metadata": { "execution": { "iopub.execute_input": "2024-03-27T19:24:50.755645Z", "iopub.status.busy": "2024-03-27T19:24:50.755170Z", "iopub.status.idle": "2024-03-27T19:24:51.824852Z", "shell.execute_reply": "2024-03-27T19:24:51.824598Z", "shell.execute_reply.started": "2024-03-27T19:24:50.755615Z" }, "tags": [] }, "outputs": [], "source": [ "from graph_fmt import *\n", "\n", "# Connect to Aerospike Graph\n", "g = traversal().withRemote(DriverRemoteConnection('ws://0.0.0.0:8182/gremlin','g'))" ] }, { "cell_type": "code", "execution_count": 2, "id": "317486e7-2b30-433b-8b55-79dbb2806680", "metadata": { "execution": { "iopub.execute_input": "2024-03-27T19:24:53.999130Z", "iopub.status.busy": "2024-03-27T19:24:53.998364Z", "iopub.status.idle": "2024-03-27T19:24:59.994749Z", "shell.execute_reply": "2024-03-27T19:24:59.993801Z", "shell.execute_reply.started": "2024-03-27T19:24:53.999107Z" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Success'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g.V().drop().iterate()\n", "g.with_(\"evaluationTimeout\", 86400000)\\\n", " .call(\"bulk-load\")\\\n", " .with_(\"aerospike.graphloader.vertices\", \"data/fraud/vertices\")\\\n", " .with_(\"aerospike.graphloader.edges\", \"data/fraud/edges\")\\\n", " .next()" ] }, { "cell_type": "code", "execution_count": 3, "id": "24cc5c6a-5e2d-4723-995e-5074de99439e", "metadata": { "execution": { "iopub.execute_input": "2024-03-27T19:25:12.320534Z", "iopub.status.busy": "2024-03-27T19:25:12.320012Z", "iopub.status.idle": "2024-03-27T19:25:12.549908Z", "shell.execute_reply": "2024-03-27T19:25:12.549399Z", "shell.execute_reply.started": "2024-03-27T19:25:12.320503Z" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "edad3e975f2145c9a3330f566c805e36", "version_major": 2, "version_minor": 0 }, "text/plain": [ "CytoscapeWidget(cytoscape_layout={'name': 'cola'}, cytoscape_style=[{'selector': 'any', 'style': {'content': '…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
vertex_count_per_labeledge_properties_per_labeledge_countvertex_countvertex_properties_per_labeledge_count_per_label
0[{'Email': 30, 'Device': 30, 'IP': 30, 'BrowserAgent': 30, 'MailingAddress': 30, 'Member': 30, 'Toolbar': 30}][{'used_mailing_address': {'weight'}, 'used_email_for_account': {'weight'}, 'used_toolbar': {'weight'}, 'used_browser_agent': {'weight'}, 'invited_email': {'weight'}, 'used_device': {'weight'}, 'referred_by': {'weight'}, 'used_ip': {'weight'}}][329][210][{'Email': {'email_valid', 'handle_suspicious', 'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'created_ts', 'graph_object_modified_by', 'region_id'}, 'Device': {'region_id', 'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'created_ts', 'graph_object_modified_by', 'device_type'}, 'IP': {'graph_object_status', 'ip_likely_city', 'graph_object_modified_ts', 'graph_object_created_by', 'ip_likely_country', 'created_ts', 'graph_object_modified_by', 'region_id'}, 'BrowserAgent': {'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'created_ts', 'graph_object_modified_by', 'browser_family', 'region_id'}, 'MailingAddress': {'address1', 'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'address2', 'country', 'zipcode', 'city', 'created_ts', 'graph_object_modified_by', 'avs_state', 'region_id', 'state'}, 'Member': {'first_name', 'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'signup_date', 'over3_fraud_score', 'last_name', 'account_modified_ts', 'graph_object_modified_by', 'member_status', 'region_id'}, 'Toolbar': {'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'created_ts', 'graph_object_modified_by', 'region_id'}}][{'used_mailing_address': 32, 'used_email_for_account': 30, 'used_toolbar': 30, 'used_browser_agent': 29, 'invited_email': 65, 'used_device': 33, 'referred_by': 74, 'used_ip': 36}]
\n", "
" ], "text/plain": [ " vertex_count_per_label \\\n", "0 [{'Email': 30, 'Device': 30, 'IP': 30, 'BrowserAgent': 30, 'MailingAddress': 30, 'Member': 30, 'Toolbar': 30}] \n", "\n", " edge_properties_per_label \\\n", "0 [{'used_mailing_address': {'weight'}, 'used_email_for_account': {'weight'}, 'used_toolbar': {'weight'}, 'used_browser_agent': {'weight'}, 'invited_email': {'weight'}, 'used_device': {'weight'}, 'referred_by': {'weight'}, 'used_ip': {'weight'}}] \n", "\n", " edge_count vertex_count \\\n", "0 [329] [210] \n", "\n", " vertex_properties_per_label \\\n", "0 [{'Email': {'email_valid', 'handle_suspicious', 'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'created_ts', 'graph_object_modified_by', 'region_id'}, 'Device': {'region_id', 'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'created_ts', 'graph_object_modified_by', 'device_type'}, 'IP': {'graph_object_status', 'ip_likely_city', 'graph_object_modified_ts', 'graph_object_created_by', 'ip_likely_country', 'created_ts', 'graph_object_modified_by', 'region_id'}, 'BrowserAgent': {'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'created_ts', 'graph_object_modified_by', 'browser_family', 'region_id'}, 'MailingAddress': {'address1', 'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'address2', 'country', 'zipcode', 'city', 'created_ts', 'graph_object_modified_by', 'avs_state', 'region_id', 'state'}, 'Member': {'first_name', 'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'signup_date', 'over3_fraud_score', 'last_name', 'account_modified_ts', 'graph_object_modified_by', 'member_status', 'region_id'}, 'Toolbar': {'graph_object_status', 'graph_object_modified_ts', 'graph_object_created_by', 'created_ts', 'graph_object_modified_by', 'region_id'}}] \n", "\n", " edge_count_per_label \n", "0 [{'used_mailing_address': 32, 'used_email_for_account': 30, 'used_toolbar': 30, 'used_browser_agent': 29, 'invited_email': 65, 'used_device': 33, 'referred_by': 74, 'used_ip': 36}] " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "graph_stats = pd.DataFrame(g.V(\"~graph_summary\").valueMap().toList())\n", "display(dgraph_sch(graph_stats, g))\n", "graph_stats" ] }, { "cell_type": "code", "execution_count": 4, "id": "cbafaa25-b17d-4662-97b6-f2e76d989429", "metadata": { "execution": { "iopub.execute_input": "2024-03-27T19:25:18.953873Z", "iopub.status.busy": "2024-03-27T19:25:18.953566Z", "iopub.status.idle": "2024-03-27T19:25:18.991730Z", "shell.execute_reply": "2024-03-27T19:25:18.991268Z", "shell.execute_reply.started": "2024-03-27T19:25:18.953849Z" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[v[ME-526967],\n", " v[ME-603335],\n", " v[ME-351584],\n", " v[ME-548238],\n", " v[ME-076805],\n", " v[ME-094592]]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Given the member vertex id or member_id,\n", "\n", "# 1. find all the member vertices that shared the same IP, Device and MailingAddress in the past with the\n", "# given member (through used_ip, used_device, used_mailing_addrress)\n", "\n", "g.V(\"ME-518945\").as_(\"m1\")\\\n", " .out(\"used_ip\", \"used_device\", \"used_mailing_address\", \"referred_by\")\\\n", " .in_(\"used_ip\", \"used_device\", \"used_mailing_address\", \"referred_by\")\\\n", " .where(P.neq(\"m1\")).dedup().toList()" ] }, { "cell_type": "code", "execution_count": 6, "id": "29b90f81-dfe1-4a5c-837f-3414780ae31e", "metadata": { "execution": { "iopub.execute_input": "2024-03-27T19:28:11.611994Z", "iopub.status.busy": "2024-03-27T19:28:11.611460Z", "iopub.status.idle": "2024-03-27T19:28:11.709539Z", "shell.execute_reply": "2024-03-27T19:28:11.709018Z", "shell.execute_reply.started": "2024-03-27T19:28:11.611956Z" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[path[v[ME-518945], v[ME-498888], v[ME-627893], v[ME-465364]],\n", " path[v[ME-518945], v[ME-498888], v[ME-627893], v[ME-477057]],\n", " path[v[ME-518945], v[ME-498888], v[ME-477057], v[ME-627893]],\n", " path[v[ME-518945], v[ME-498888], v[ME-477057], v[ME-465364]],\n", " path[v[ME-518945], v[ME-526967], v[ME-477057], v[ME-627893]],\n", " path[v[ME-518945], v[ME-526967], v[ME-477057], v[ME-465364]],\n", " path[v[ME-518945], v[ME-526967], v[ME-627893], v[ME-465364]],\n", " path[v[ME-518945], v[ME-526967], v[ME-627893], v[ME-477057]],\n", " path[v[ME-518945], v[ME-763327], v[ME-465364], v[ME-627893]],\n", " path[v[ME-518945], v[ME-763327], v[ME-465364], v[ME-477057]],\n", " path[v[ME-518945], v[ME-763327], v[ME-627893], v[ME-465364]],\n", " path[v[ME-518945], v[ME-763327], v[ME-627893], v[ME-477057]],\n", " path[v[ME-518945], v[ME-627893], v[ME-465364], v[ME-627893]],\n", " path[v[ME-518945], v[ME-627893], v[ME-465364], v[ME-477057]],\n", " path[v[ME-518945], v[ME-627893], v[ME-477057], v[ME-627893]],\n", " path[v[ME-518945], v[ME-627893], v[ME-477057], v[ME-465364]],\n", " path[v[ME-518945], v[ME-199470], v[ME-627893], v[ME-465364]],\n", " path[v[ME-518945], v[ME-199470], v[ME-627893], v[ME-477057]],\n", " path[v[ME-518945], v[ME-477057], v[ME-627893], v[ME-465364]],\n", " path[v[ME-518945], v[ME-477057], v[ME-627893], v[ME-477057]],\n", " path[v[ME-518945], v[ME-477057], v[ME-465364], v[ME-627893]],\n", " path[v[ME-518945], v[ME-477057], v[ME-465364], v[ME-477057]]]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 2. print the possible referral tree among the members generated above (including the given member)\n", "\n", "# Comment: This makes sense. Path seems required here. They can tune number of 'times'.\n", "\n", "g.V(\"ME-518945\").repeat(in_(\"referred_by\")).times(3).path().dedup().toList()" ] }, { "cell_type": "code", "execution_count": 7, "id": "fca2b02e-16ed-40dc-83d7-fd0bd0590d58", "metadata": { "execution": { "iopub.execute_input": "2024-03-27T19:30:54.488993Z", "iopub.status.busy": "2024-03-27T19:30:54.488492Z", "iopub.status.idle": "2024-03-27T19:30:54.532826Z", "shell.execute_reply": "2024-03-27T19:30:54.532325Z", "shell.execute_reply.started": "2024-03-27T19:30:54.488970Z" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[v[ME-526967], v[ME-094592]]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 3. Find all the groups of member vertices within the graph that shared the same IP, Device and MailingAddress\n", "# (at least once, all the three components) in the past (through used_ip, used_device, used_mailing_address)\n", "\n", "# Comment: Again lets just return vertices instead path unless required. Also edit to using logical and on lists of vertices.\n", "g.V(\"194.91.138.20\").in_().as_(\"ip_attached\")\\\n", " .V(\"B9-2E-D5-A6-84-C0\").in_().as_(\"device_attached\")\\\n", " .V(\"ADDRS-042305\").in_().as_(\"mailing_address_attached\")\\\n", " .and_(select(\"ip_attached\"),select(\"device_attached\"),select(\"mailing_address_attached\"))\\\n", " .dedup().toList()" ] }, { "cell_type": "code", "execution_count": 10, "id": "bcdd4046-2a1c-4f5b-b3ef-2c0edea13e31", "metadata": { "execution": { "iopub.execute_input": "2024-03-27T19:32:54.242759Z", "iopub.status.busy": "2024-03-27T19:32:54.242021Z", "iopub.status.idle": "2024-03-27T19:32:54.274575Z", "shell.execute_reply": "2024-03-27T19:32:54.274077Z", "shell.execute_reply.started": "2024-03-27T19:32:54.242709Z" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0.6]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Given the member_id and a score threshold calculate a score based on the IP, Device, MailingAddress or BrowserAgent that this member shared\n", "# and used with the other members in the past (through used_ip, used_device, used_mailing_address, used_browser_agent)\n", "# and output the score between each pair if the total score between them is greater than the given threshold.\n", "\n", "#\"ME-799370\", \"ME-094592\"\n", "g.V(\"ME-799370\")\\\n", " .outE(\"used_ip\",\"used_device\",\"used_mailing_address\",\"used_browser_agent\").as_('e1')\\\n", " .select('e1').values().sum_().is_(P.lt(2.0).or_(eq(2.0))).toList()\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "f2cd9076-a523-449d-8de4-53251aa6dc31", "metadata": { "execution": { "iopub.execute_input": "2024-03-27T19:44:26.985191Z", "iopub.status.busy": "2024-03-27T19:44:26.984440Z", "iopub.status.idle": "2024-03-27T19:44:27.011394Z", "shell.execute_reply": "2024-03-27T19:44:27.010876Z", "shell.execute_reply.started": "2024-03-27T19:44:26.985136Z" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0.6]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g.V(\"ME-799370\")\\\n", " .outE(\"used_ip\",\"used_device\",\"used_mailing_address\",\"used_browser_agent\")\\\n", " .values('weight').sum_().is_(P.lt(2.0).or_(eq(2.0))).toList()" ] }, { "cell_type": "code", "execution_count": null, "id": "41fab2cd-76ce-452a-bfac-bbd64a6dc5e0", "metadata": { "tags": [] }, "outputs": [], "source": [ "#\"ME-799370\", \"ME-094592\"\n", "g.V(\"ME-094592\")\\\n", " .outE(\"used_ip\",\"used_device\",\"used_mailing_address\",\"used_browser_agent\").as_('e1')\\\n", " .select('e1').values().sum_().is_(P.lt(2.0).or_(eq(2.0))).toList()" ] }, { "cell_type": "markdown", "id": "92c6d020-f583-49c1-a2e9-e9d1ae6510f5", "metadata": {}, "source": [ "\n", "\n", "# END OF DEMO\n", "---" ] }, { "cell_type": "markdown", "id": "74675767-f3ec-4400-b634-aee96b2bf85e", "metadata": {}, "source": [ " // For the next query I need help to finish writing it. I do not know how to add a weight to an edge if it\n", " // exists and display the result ONLY IN the sum of the edges weights is greater than a given threshold.\n", " //\n", " // Given the member_id and a score threshold\n", " //\n", " // calculate a score based on the IP, Device, MailingAddress or BrowserAgent that this member shared\n", " // and used with the other members in the past (through used_ip, used_device, used_mailing_address, used_browser_agent)\n", " // and output the score between each pair if the total score between them is greater than the given threshold.\n", " //\n", " // Comment: I editted the input to the first query's input since the other member vertices that were provided\n", " // as input did not share any ip's, device's, mailing addrs, or browser agents with other members.\n", " // Side note: I ran the following to add scores randomly to edges:\n", " // List edgeids = g.E().id().toList();\n", " // for (Object edgeid : edgeids) {\n", " // final Random random = new Random();\n", " // g.E(edgeid).property(\"score\", random.nextDouble()).iterate();\n", " // }\n", " \n", " List> query4Output =\n", " g.V(\"ME-3518945\").\n", " as(\"m1\").\n", " outE(\"used_ip\",\"used_device\",\"used_mailing_address\",\"used_browser_agent\").\n", " as(\"e1\").\n", " properties(\"score\").value().as(\"score1\").\n", " select(\"e1\").\n", " otherV().\n", " inE(\"used_ip\",\"used_device\",\"used_mailing_address\",\"used_browser_agent\").\n", " as(\"e2\").\n", " properties(\"score\").value().as(\"score2\").\n", " select(\"e2\").\n", " otherV().\n", " where(P.neq(\"m1\")).\n", " as(\"m2\").\n", " project(\"member1\", \"member2\", \"score\").\n", " by(__.select(\"m1\")).\n", " by(__.select(\"m2\")).\n", " by(__.math(\"score1 + score2\")).\n", " toList();\n", " for (Map path : query4Output) {\n", " Double score = (Double) path.get(\"score\");\n", " if (score > 0.2) {\n", " System.out.println(\n", " \"Member 1: \" + path.get(\"member1\") +\n", " \" Member 2: \" + path.get(\"member2\") +\n", " \" Score: \" + score);\n", " }\n", " }" ] }, { "cell_type": "code", "execution_count": null, "id": "7cf3c4c1-00c8-437b-a811-6299c2f9e1f5", "metadata": { "tags": [] }, "outputs": [], "source": [ "g.E().id_().toList()" ] }, { "cell_type": "code", "execution_count": null, "id": "3e029314-3fd1-4b50-84f0-4f71e8238305", "metadata": { "tags": [] }, "outputs": [], "source": [ "import pandas as pd\n", "from ipywidgets import widgets, interact, interactive, fixed, interact_manual, Layout, AppLayout, HBox, VBox\n", "from IPython.display import display\n", "\n", "df = pd.DataFrame([1,2,3])\n", "\n", "def f(x, df):\n", " df\n", " df['Kosten A'] = x\n", " y = x*x\n", " print(df, y)\n", "\n", "interact(f, x=(10,50,5), df = fixed(df))" ] }, { "cell_type": "code", "execution_count": null, "id": "05e450c9-9658-445f-98db-b50034ab6aa8", "metadata": { "tags": [] }, "outputs": [], "source": [ "al = widgets.IntText(\n", " value=0,\n", " description='AVG Latency (ms):',\n", " style={'description_width': 'initial'},\n", " disabled=False,\n", " layout=Layout(width='20%')\n", ")\n", "\n", "tp = widgets.IntText(\n", " value=0,\n", " description='Trafic Proportion',\n", " style={'description_width': 'initial'},\n", " disabled=False,\n", " layout=Layout(width='20%')\n", ")\n", "\n", "qps = widgets.IntText(\n", " value=0,\n", " description='Trafic Proportion',\n", " style={'description_width': 'initial'},\n", " disabled=False,\n", " layout=Layout(width='20%')\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "093af182-8396-407d-814d-7220838cbed0", "metadata": { "tags": [] }, "outputs": [], "source": [ "HBox(al, tp, qps)" ] }, { "cell_type": "code", "execution_count": null, "id": "d41d3fc8-0457-4ea8-866d-40b4903e37db", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ipywidgets import interact, HBox, VBox, Output, HTML, Dropdown, Button, Layout, Label\n", "from IPython.display import display, clear_output\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import datetime as dt\n", "class demo():\n", " def __init__(self):\n", " self.df = pd.DataFrame({'A':[23,45,26,43,67], 'B':[25,65,85,74,56], 'C':[23,65,87,89,65]})\n", " \n", " style=\"\"\"\n", " \n", " \"\"\"\n", " display(HTML(style))\n", " self.o1 = Output(layout=Layout(width='400px'))\n", " \n", " self.o2 = Output() \n", " self.o2.add_class('o2')\n", " self.o3 = Output()\n", " self.o3.add_class('o3')\n", " self.o4 = Output()\n", " self.o4.add_class('o4')\n", " self.o5 = Output()\n", " self.o5.add_class('o5')\n", " # create a scene for displaying the outputs, \n", " # Output1 on the top row, 2,3, and 4 stacked horizontally in the second row\n", " scene = VBox([self.o1,\n", " HBox([self.o2, self.o3, self.o4]),\n", " self.o5\n", " ])\n", " display(scene)\n", " \n", " with self.o1:\n", " display(HTML('

Demo

'))\n", " \n", " with self.o2:\n", " self.dd_filter = Dropdown(description='Select Filter', options=['', 'A','B','C'])\n", " self.dd_filter.observe(self.fill_values)\n", " \n", " self.dd_values = Dropdown(description='Select Value')\n", " \n", " self.btn = Button(description='Run')\n", " self.btn.on_click(self.display_results)\n", " \n", " display(self.dd_filter, self.dd_values, self.btn)\n", " \n", " \n", " def fill_values(self,x):\n", " if x['type'] == 'change' and x['name'] == 'value':\n", " filter_by = x['owner'].value\n", " \n", " if filter_by=='':\n", " filter_values = []\n", " else:\n", " filter_values = self.df[filter_by].values\n", " self.dd_values.options = filter_values\n", " \n", " def display_results(self, x):\n", " filter_by = self.dd_filter.value\n", " filter_val = self.dd_values.value\n", " \n", " if filter_by=='':\n", " with self.o3:\n", " clear_output()\n", " print('Please select filter')\n", " return\n", " \n", " df_filtered = self.df[self.df[filter_by]>=filter_val]\n", " \n", " with self.o3:\n", " clear_output()\n", " lbl = Label(value=f'Filter by {filter_by}, with {filter_val}, found {df_filtered.shape[0]} observation(s)')\n", " lbl.add_class(f'style_{filter_by}')\n", " display(lbl)\n", " display(df_filtered)\n", " \n", " with self.o4:\n", " clear_output()\n", " df_filtered.plot(kind='bar')\n", " plt.show()\n", " \n", " with self.o5:\n", " clear_output()\n", " display(Label(value= f'Code last run {dt.datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")}' ))\n", " \n", " \n", "d = demo()" ] }, { "cell_type": "code", "execution_count": null, "id": "5c557f4e-2612-4803-bf94-cb405dfbfa62", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ipywidgets import interact, HBox, VBox, Output\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "@interact\n", "def chart_and_table(filterby=['A','B','C']):\n", " df = pd.DataFrame({'A':[23,45,26,43,67], 'B':[25,65,85,74,56], 'C':[23,65,87,89,65]})\n", " \n", " o1, o2 = Output(), Output()\n", " scene = HBox([o1,o2])\n", " display(scene)\n", " \n", " with o1:\n", " df[[filterby]].plot(kind='bar')\n", " plt.show()\n", " with o2:\n", " display(df[[filterby]])" ] }, { "cell_type": "code", "execution_count": null, "id": "3817cc40-2b83-40ad-ad35-8254152fae8d", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ipywidgets import interact, HBox, VBox, Output, HTML, Layout\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "@interact\n", "def chart_and_table(filterby=['A','B','C']):\n", " df = pd.DataFrame({'A':[23,45,26,43,67], 'B':[25,65,85,74,56], 'C':[23,65,87,89,65]})\n", " \n", " style=\"\"\"\n", " \n", " \"\"\"\n", " display(HTML(style))\n", " \n", " o1 = Output(layout=Layout(width='400px'))\n", " o2 = Output(layout=Layout(width='200px')) \n", " o3 = Output()\n", " o4 = Output()\n", " scene = HBox([o1,\n", " o2,\n", " VBox([o3, o4])\n", " ])\n", " display(scene)\n", " \n", " with o1:\n", " df[[filterby]].plot(kind='bar')\n", " plt.show()\n", " with o2:\n", " display(df[[filterby]])\n", " with o3:\n", " if filterby=='A':\n", " o3.add_class('style_a')\n", " elif filterby=='B':\n", " o3.add_class('style_b')\n", " elif filterby=='C':\n", " o3.add_class('style_c')\n", " print('The selected filter is :', filterby)\n", " with o4:\n", " display(df)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "028302ca-afc5-4e5e-ab76-aef02f88485a", "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.11.7" } }, "nbformat": 4, "nbformat_minor": 5 }