Created
January 2, 2024 13:15
-
-
Save BjornFJohansson/a60d754bf6330852c5b683e1dafbf2d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Minimal Networkx d3js JavaScript example\n", | |
| "\n", | |
| "Example of writing JSON format graph data and using the D3 JavaScript library\n", | |
| "to produce an HTML/JavaScript drawing.\n", | |
| "\n", | |
| "You will need to download the following directory:\n", | |
| "\n", | |
| "- https://github.com/networkx/networkx/tree/main/examples/external/force\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": false, | |
| "jupyter": { | |
| "outputs_hidden": false | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import json\n", | |
| "import networkx as nx\n", | |
| "\n", | |
| "G = nx.Graph()\n", | |
| "G.add_node('Node1')\n", | |
| "G.add_node('Node2')\n", | |
| "G.add_edge('Node1', 'Node2')\n", | |
| "\n", | |
| "for n in G:\n", | |
| " G.nodes[n][\"name\"] = \"My\" + str(n) \n", | |
| " G.nodes[n][\"xlink:href\"] = \"http://google.com\"\n", | |
| "\n", | |
| "d = nx.json_graph.node_link_data(G) # node-link format to serialize\n", | |
| "\n", | |
| "json.dump(d, open(\"force/force.json\", \"w\"))\n", | |
| "print(\"Wrote node-link JSON data to force/force.json\")\n", | |
| "d" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": false, | |
| "jupyter": { | |
| "outputs_hidden": false | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Serve the file over http to allow for cross origin requests\n", | |
| "import flask\n", | |
| "app = flask.Flask(__name__, static_folder=\"force\")\n", | |
| "\n", | |
| "@app.route(\"/\")\n", | |
| "def static_proxy():\n", | |
| " return app.send_static_file(\"force.html\")\n", | |
| "app.run(port=8001)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python [conda env:bjorn311]", | |
| "language": "python", | |
| "name": "conda-env-bjorn311-py" | |
| }, | |
| "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": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment