{ "cells": [ { "cell_type": "markdown", "id": "f91b696a", "metadata": {}, "source": [ "# The Knowledge Graph HTML generator (N1904-TF)" ] }, { "cell_type": "markdown", "id": "5ffbdfac-efa5-4bbf-bb02-6a791feb4f1c", "metadata": {}, "source": [ "## Table of content (ToC)\n", "* 1 - Introduction\n", "* 2 - Add x-y positons to the nodes\n", "* 3 - Generate the cytoscape based HTML file\n", "* 4 - Notebook version details" ] }, { "cell_type": "markdown", "id": "55fbc22b-683d-482d-a5ec-7cf3679b5061", "metadata": {}, "source": [ "# 1 - Introduction \n", "##### [Back to ToC](#TOC)\n", "\n", "This notebook imports the JSON file with the N1904 knowledge graph data (describing TF nodes and features) and converts it into a downloadable (semi stand-alone) HTML file." ] }, { "cell_type": "markdown", "id": "9606f8c6-f954-4b93-9d62-981a1b7fe2d8", "metadata": {}, "source": [ "# 2 - Add x-y positons to the nodes \n", "##### [Back to ToC](#TOC)" ] }, { "cell_type": "code", "execution_count": 1, "id": "a6fdbd68-8a22-4bee-98f8-11aba3a03885", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Updated elements JSON saved to 'n1904_elements.json'\n" ] } ], "source": [ "import json\n", "from pathlib import Path\n", "\n", "# Load the knowledge graph from JSON\n", "with open(\"n1904_knowledge_graph.json\", encoding=\"utf-8\") as f:\n", " kg = json.load(f)\n", "\n", "# Load the node positions from JSON\n", "with open(\"node_positions.json\", encoding=\"utf-8\") as f:\n", " node_positions = json.load(f)\n", "\n", "nodes = []\n", "for nodeId, nodeData in kg[\"nodes\"].items():\n", " # We'll just keep the nodeData['description'] as is if it exists.\n", " # If there's no description, we can add a fallback or leave it blank.\n", " if \"description\" not in nodeData:\n", " nodeData[\"description\"] = \"\"\n", " # Also, you might want to store a \"label\" that strips the \"feature::\"/\"otype::\" prefix.\n", " # But that's optional. We'll do it here for clarity:\n", " labelStr = nodeId.replace(\"feature::\", \"\").replace(\"otype::\", \"\")\n", "\n", " # Get the position from the node_positions dictionary\n", " position = node_positions.get(nodeId, {})\n", " # If the position is empty, use default values (0, 0)\n", " x = position.get(\"x\", 0)\n", " y = position.get(\"y\", 0)\n", "\n", " # Create the node element with data\n", " nodeElem = {\n", " \"data\": {\n", " \"id\": nodeId,\n", " \"label\": labelStr,\n", " # Merge all the fields from nodeData into \"data\"\n", " **nodeData,\n", " \"x\": x,\n", " \"y\": y\n", " }\n", " }\n", " nodes.append(nodeElem)\n", "\n", "edges = []\n", "for e in kg[\"edges\"]:\n", " # We also include freqDetail, if present\n", " edgeData = {\n", " \"source\": e[\"from\"],\n", " \"target\": e[\"to\"],\n", " \"label\": e[\"relation\"],\n", " }\n", " # If there's a freqDetail, add it\n", " if \"freqDetail\" in e:\n", " edgeData[\"freqDetail\"] = e[\"freqDetail\"]\n", "\n", " edgeElem = {\"data\": edgeData}\n", " edges.append(edgeElem)\n", "\n", "elements = {\"nodes\": nodes, \"edges\": edges}\n", "elements_json = json.dumps(elements, indent=2)\n", "\n", "# Write out to a file or use directly\n", "Path(\"n1904_elements.json\").write_text(elements_json, encoding=\"utf-8\")\n", "print(\"Updated elements JSON saved to 'n1904_elements.json'\")" ] }, { "cell_type": "markdown", "id": "432bb5b6-ddc9-498f-a292-e9cc376bd0f9", "metadata": {}, "source": [ "# 3 - Generate the cytoscape based HTML file \n", "##### [Back to ToC](#TOC)" ] }, { "cell_type": "code", "execution_count": 2, "id": "482b5da4-bc97-4516-a6d0-d7e5b2da7ef6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HTML successfully written to 'n1904_graph_contextmenu.html' -- ready to open in a browser!\n" ] } ], "source": [ "import json\n", "from pathlib import Path\n", "\n", "html_template = \"\"\"\n", "\n", "\n", " \n", " N1904 Graph\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", "
\n", "
\n", "\n", " \n", " \n", "\n", " \n", "\n", "\n", "\"\"\"\n", "\n", "# Load your JSON data (which should have \"nodes\" and \"edges\" keys) from file.\n", "DATA_FILE = \"n1904_elements.json\" # Adjust this filename as needed.\n", "with open(DATA_FILE, encoding=\"utf-8\") as f:\n", " elements_data = json.load(f)\n", "\n", "# Dump the JSON data as a pretty-printed string.\n", "elements_json = json.dumps(elements_data, indent=2)\n", "\n", "# Insert the JSON data into the HTML template.\n", "final_html = html_template.replace(\"REPLACE_JSON_HERE\", elements_json)\n", "\n", "# Write the final HTML to file.\n", "OUTPUT_HTML = \"n1904_graph_contextmenu.html\"\n", "Path(OUTPUT_HTML).write_text(final_html, encoding=\"utf-8\")\n", "print(f\"HTML successfully written to '{OUTPUT_HTML}' -- ready to open in a browser!\")\n" ] }, { "cell_type": "markdown", "id": "523d86de-f4c8-4e6c-8377-f68ea7accc0b", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "# 4 - Notebook version details\n", "##### [Back to ToC](#TOC)\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AuthorTony Jurg
Version1.1
Date3 April 2025
\n", "
" ] } ], "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.12.7" } }, "nbformat": 4, "nbformat_minor": 5 }