diff --git a/Pyvis/Pyvis_Create_a_network_visualization.ipynb b/Pyvis/Pyvis_Create_a_network_visualization.ipynb index d5fc61bc02..405fe1adfb 100644 --- a/Pyvis/Pyvis_Create_a_network_visualization.ipynb +++ b/Pyvis/Pyvis_Create_a_network_visualization.ipynb @@ -38,7 +38,7 @@ "tags": [] }, "source": [ - "**Tags:** #python #naas #scheduler #network #snippet #analytics" + "**Tags:** #python #naas #asset #network #analytics" ] }, { @@ -60,7 +60,7 @@ "tags": [] }, "source": [ - "**Last update:** 2023-04-12 (Created: 2022-07-27)" + "**Last update:** 2023-12-01 (Created: 2022-07-27)" ] }, { @@ -82,7 +82,8 @@ "tags": [] }, "source": [ - "**References:** https://pyvis.readthedocs.io/en/latest/tutorial.html" + "**References:** \n", + "- [Pyvis documentation](https://pyvis.readthedocs.io/en/latest/tutorial.html)" ] }, { @@ -98,83 +99,98 @@ }, { "cell_type": "markdown", - "id": "87f5d344-ecdd-4054-8696-34ed7f96548c", + "id": "numeric-mediterranean", "metadata": { "papermill": {}, "tags": [] }, "source": [ - "### Import packages" + "### Import libraries" ] }, { "cell_type": "code", - "execution_count": 1, - "id": "6b708479-188a-41ca-9180-46b6328b6621", + "execution_count": null, + "id": "potential-surfing", "metadata": { - "jupyter": { - "source_hidden": true - }, "papermill": {}, "tags": [] }, "outputs": [], "source": [ - "pip install pyvis" + "import naas\n", + "try:\n", + " from pyvis.network import Network\n", + "except:\n", + " !pip install pyvis --upgrade\n", + " from pyvis.network import Network" ] }, { "cell_type": "markdown", - "id": "numeric-mediterranean", + "id": "aggressive-trustee", "metadata": { "papermill": {}, "tags": [] }, "source": [ - "### Import library" + "### Setup variables\n", + "- `network_name`: Network name\n", + "- `height`: Network height\n", + "- `width`: Network width\n", + "- `bgcolor`: Network background color\n", + "- `font_color`: Network font color" ] }, { "cell_type": "code", - "execution_count": 2, - "id": "potential-surfing", + "execution_count": null, + "id": "continuous-melbourne", "metadata": { - "jupyter": { - "source_hidden": true - }, "papermill": {}, "tags": [] }, "outputs": [], "source": [ - "import naas\n", - "from pyvis.network import Network" + "network_name = \"pyvis_example\"\n", + "height = \"750px\"\n", + "width = \"100%\"\n", + "bgcolor = \"#222222\"\n", + "font_color = \"white\"" ] }, { "cell_type": "markdown", - "id": "aggressive-trustee", + "id": "registered-showcase", "metadata": { "papermill": {}, "tags": [] }, "source": [ - "### Setup the global variables for the network" + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "ed90822e-8456-47c8-937a-7d207d96993d", + "metadata": {}, + "source": [ + "### Initializing the Pyvis network" ] }, { "cell_type": "code", - "execution_count": 3, - "id": "continuous-melbourne", - "metadata": { - "papermill": {}, - "tags": [] - }, + "execution_count": null, + "id": "14656059-6704-41fb-b8c4-315881efca42", + "metadata": {}, "outputs": [], "source": [ - "network_name = \"pyvis_example\"\n", "net = Network(\n", - " notebook=True, height=\"750px\", width=\"100%\", bgcolor=\"#222222\", font_color=\"white\"\n", + " notebook=True,\n", + " height=height,\n", + " width=width,\n", + " bgcolor=bgcolor,\n", + " font_color=font_color\n", ")" ] }, @@ -191,7 +207,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "c11aae54-33b4-4f21-889a-988ddf28a079", "metadata": { "papermill": {}, @@ -232,7 +248,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "ede1c7f7-cd29-49e2-be8a-db9484848959", "metadata": { "papermill": {}, @@ -271,17 +287,6 @@ ")" ] }, - { - "cell_type": "markdown", - "id": "registered-showcase", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "## Model" - ] - }, { "cell_type": "markdown", "id": "tested-astrology", @@ -295,7 +300,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "crude-louisville", "metadata": { "papermill": {}, @@ -319,7 +324,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "9cca845f-3a67-4e5f-9721-903970bee206", "metadata": { "papermill": {}, @@ -388,7 +393,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "9c4e3b7b-6440-4844-8054-265f1aec65eb", "metadata": { "papermill": {}, @@ -413,7 +418,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "db205965-a2dc-4541-ba8e-db3250d1cff0", "metadata": { "papermill": {}, @@ -466,4 +471,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} diff --git a/Pyvis/Pyvis_Visualize_awesome_notebooks_network.ipynb b/Pyvis/Pyvis_Visualize_awesome_notebooks_network.ipynb new file mode 100644 index 0000000000..815feb59e7 --- /dev/null +++ b/Pyvis/Pyvis_Visualize_awesome_notebooks_network.ipynb @@ -0,0 +1,407 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "latin-packing", + "metadata": { + "execution": { + "iopub.execute_input": "2021-02-23T14:22:16.610471Z", + "iopub.status.busy": "2021-02-23T14:22:16.610129Z", + "iopub.status.idle": "2021-02-23T14:22:16.627784Z", + "shell.execute_reply": "2021-02-23T14:22:16.626866Z", + "shell.execute_reply.started": "2021-02-23T14:22:16.610384Z" + }, + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Pyvis.png\"" + ] + }, + { + "cell_type": "markdown", + "id": "compressed-wilson", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Pyvis - Visualize awesome notebooks graph network\n", + "

Give Feedback | Bug report" + ] + }, + { + "cell_type": "markdown", + "id": "religious-programmer", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #python #naas #scheduler #network #snippet #analytics" + ] + }, + { + "cell_type": "markdown", + "id": "1fe9f56e-561c-4f52-aef8-b861c9462107", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Jaime Dols Duxans](https://www.linkedin.com/in/duxans/)" + ] + }, + { + "cell_type": "markdown", + "id": "9f4457c5-1377-4167-8528-fdb7449f2d2d", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-12-01 (Created: 2023-11-30)" + ] + }, + { + "cell_type": "markdown", + "id": "31ea7cdb-e10d-43fc-b026-f69249a59736", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** With this notebook you can visualize all the awesome-notebooks templates as a graph network." + ] + }, + { + "cell_type": "markdown", + "id": "162268ac-ed10-4f32-af82-ab5bd5b2bb9e", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:** \n", + "- [Pyvis documentation](https://pyvis.readthedocs.io/en/latest/tutorial.html)" + ] + }, + { + "cell_type": "markdown", + "id": "distinguished-truth", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "numeric-mediterranean", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "potential-surfing", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "import naas\n", + "try:\n", + " from pyvis.network import Network\n", + "except:\n", + " !pip install pyvis --upgrade\n", + " from pyvis.network import Network\n", + "import pandas as pd\n", + "import requests" + ] + }, + { + "cell_type": "markdown", + "id": "09c7ee40-504b-4661-a7c6-013eee0e71af", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "- `github_url`: Stores the URL to a JSON file hosted on GitHub.\n", + "- `solver`: The gravity model used in the visualization (choose and test your favourite)\n", + "- `network_name`: Network name\n", + "- `height`: Network height\n", + "- `width`: Network width\n", + "- `bgcolor`: Network background color\n", + "- `font_color`: Network font color" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e346d915-94dc-45a8-b9c2-6929dd4d998d", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "github_url = \"https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/templates.json\"\n", + "solver = 'force_atlas_2based'\n", + "network_name = \"pyvis_awesome-notebooks\"\n", + "height = \"1400px\"\n", + "width = \"100%\"\n", + "bgcolor = \"#222222\"\n", + "font_color = \"white\"" + ] + }, + { + "cell_type": "markdown", + "id": "25015cf0-4ee4-418a-8371-a163d179d9e8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "d5697de1-307e-4beb-b99c-fc4af92d7fc8", + "metadata": {}, + "source": [ + "### Get templates from JSON" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f46c9e67-d9c5-4d5c-9e8a-e4beaaa895fb", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def get_templates(url):\n", + " res = requests.get(url)\n", + " df = pd.DataFrame(res.json())\n", + " return df\n", + "\n", + "df_temp = get_templates(github_url)\n", + "print(\"Templates:\", len(df_temp))\n", + "df_temp.head(1)" + ] + }, + { + "cell_type": "markdown", + "id": "f5556230-702d-41df-a3b9-921a1e1cd766", + "metadata": {}, + "source": [ + "### Initializing the Pyvis network" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2d22f8ce-f051-410c-ba35-cff5509a2aa2", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "net = Network(\n", + " notebook=True,\n", + " height=height,\n", + " width=width,\n", + " bgcolor=bgcolor,\n", + " font_color=font_color\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "527710df-4fa5-44de-aa20-ddcdd5846a1e", + "metadata": {}, + "source": [ + "### Create Pyvis network" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c11aae54-33b4-4f21-889a-988ddf28a079", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "# Physics solver. Choose one from Pyvis documentation \n", + "net.force_atlas_2based()\n", + "\n", + "# Pull data\n", + "tools = list(df_temp['tool'])\n", + "images = list(df_temp['image_url'])\n", + "notebooks = list(df_temp['notebook'])\n", + "\n", + "# Create a dictionary of tools and images\n", + "tool_img = dict(zip(tools, images))\n", + "\n", + "# Add tool nodes\n", + "for tool in tools:\n", + " if tool == 'Naas':\n", + " net.add_node(tool, title=tool, image=tool_img[tool], shape='image', mass=30, size=150, level=1, fixed=True, x=0, y=0,physics=False)\n", + " if tool == 'OpenAI':\n", + " #this large node was bouncing around very fast, so I fixed it\n", + " net.add_node(tool, title=tool, image=tool_img[tool], shape='image', size=60, level=1, fixed=True, x=-1000, y=2000, physics=False)\n", + " else:\n", + " net.add_node(tool, title=tool, image=tool_img[tool], shape='image',size=60,level=1, physics=False)\n", + "\n", + "# Add notebook nodes\n", + "for notebook in notebooks:\n", + " net.add_node(notebook, title=notebook, size=30,level=2,color='#4d94db')\n", + "\n", + "# Add edges\n", + "for _, node_e in df_temp.iterrows():\n", + " net.add_edge(node_e['tool'], node_e['notebook'], title=node_e['action'])\n", + " \n", + "level_1_count = 0\n", + "level_2_count = 0\n", + "for node in net.nodes:\n", + " if node['level'] == 1:\n", + " level_1_count += 1\n", + " else:\n", + " level_2_count += 1\n", + "\n", + "print(\"Number of tool nodes: \", level_1_count)\n", + "print(\"Number of notebook nodes: \", level_2_count)\n", + "print(\"Total number of nodes: \", len(net.nodes) , \"\\n\")\n", + "print(\"Total number of edges: \", len(net.edges), \"\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "lonely-pacific", + "metadata": { + "execution": { + "iopub.execute_input": "2021-07-02T23:32:10.789097Z", + "iopub.status.busy": "2021-07-02T23:32:10.788829Z", + "iopub.status.idle": "2021-07-02T23:32:10.796900Z", + "shell.execute_reply": "2021-07-02T23:32:10.796358Z", + "shell.execute_reply.started": "2021-07-02T23:32:10.789033Z" + }, + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "890f7c86-b7bb-4f5d-9a1b-e492dd9580fd", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Show results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9c4e3b7b-6440-4844-8054-265f1aec65eb", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "# Saving the network to a HTML file in your workdir (optional)\n", + "#net.write_html(f\"{network_name}.html\")\n", + "\n", + "network = net.show(f\"{network_name}.html\")\n", + "network" + ] + }, + { + "cell_type": "markdown", + "id": "a28950bb-1d7d-4835-a83a-faac837ebc7c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Share your output" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39bcdd24-a9f7-4522-acc8-cbe22dfad9d8", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "naas.asset.add(f\"{network_name}.html\", {\"inline\": True})\n", + "\n", + "# -> Uncomment the line below to remove your asset\n", + "# naas.asset.delete()" + ] + } + ], + "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.9.6" + }, + "naas": { + "notebook_id": "2df55afca82f21837a817a57c842dcf4460d0908883b76b3c64576ef8f09f864", + "notebook_path": "Pyvis/Pyvis_Create_a_network_visualization.ipynb" + }, + "papermill": { + "default_parameters": {}, + "environment_variables": {}, + "parameters": {}, + "version": "2.3.3" + }, + "toc-autonumbering": false, + "toc-showmarkdowntxt": false, + "toc-showtags": false, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}