Skip to content

Instantly share code, notes, and snippets.

@alonsosilvaallende
Created June 8, 2025 15:14
Show Gist options
  • Select an option

  • Save alonsosilvaallende/722927a800e4beb6afc0022588de60be to your computer and use it in GitHub Desktop.

Select an option

Save alonsosilvaallende/722927a800e4beb6afc0022588de60be to your computer and use it in GitHub Desktop.
Graphviz_response.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPpTw1yITTJ9Hk1uDa8MD6h",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/alonsosilvaallende/722927a800e4beb6afc0022588de60be/graphviz_response.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "wD_60o5CF_vf"
},
"outputs": [],
"source": [
"assistant_response = \"\"\"{\"nodes\":[{\"id\":1,\"label\":\"Alice\",\"color\":\"red\"},{\"id\":2,\"label\":\"Bob\",\"color\":\"blue\"}],\"edges\":[{\"source\":1,\"target\":2,\"label\":\"loves\"}]}\"\"\""
]
},
{
"cell_type": "code",
"source": [
"import json\n",
"\n",
"response_json = json.loads(assistant_response)"
],
"metadata": {
"id": "dFKHpvfLGGn3"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import graphviz\n",
"\n",
"dot = graphviz.Digraph()\n",
"\n",
"nodes = response_json.get(\"nodes\", [])\n",
"edges = response_json.get(\"edges\", [])\n",
"for node in nodes:\n",
" dot.node(name=str(node[\"id\"]), label=node[\"label\"], color=node[\"color\"])\n",
"for edge in edges:\n",
" dot.edge(tail_name=str(edge[\"source\"]), head_name=str(edge[\"target\"]), label=edge[\"label\"])\n",
"display(dot)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 196
},
"id": "dDd9NTYcGN4Z",
"outputId": "653ac1fb-1b49-4bb3-bad2-8f56827c7c43"
},
"execution_count": 3,
"outputs": [
{
"output_type": "display_data",
"data": {
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"68pt\" height=\"131pt\"\n viewBox=\"0.00 0.00 67.79 131.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 127)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-127 63.79,-127 63.79,4 -4,4\"/>\n<!-- 1 -->\n<g id=\"node1\" class=\"node\">\n<title>1</title>\n<ellipse fill=\"none\" stroke=\"red\" cx=\"29.9\" cy=\"-105\" rx=\"29.8\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"29.9\" y=\"-101.3\" font-family=\"Times,serif\" font-size=\"14.00\">Alice</text>\n</g>\n<!-- 2 -->\n<g id=\"node2\" class=\"node\">\n<title>2</title>\n<ellipse fill=\"none\" stroke=\"blue\" cx=\"29.9\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"29.9\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Bob</text>\n</g>\n<!-- 1&#45;&gt;2 -->\n<g id=\"edge1\" class=\"edge\">\n<title>1&#45;&gt;2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M29.9,-86.8C29.9,-75.16 29.9,-59.55 29.9,-46.24\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"33.4,-46.18 29.9,-36.18 26.4,-46.18 33.4,-46.18\"/>\n<text text-anchor=\"middle\" x=\"44.4\" y=\"-57.8\" font-family=\"Times,serif\" font-size=\"14.00\">loves</text>\n</g>\n</g>\n</svg>\n",
"text/plain": [
"<graphviz.graphs.Digraph at 0x7dc7545bd190>"
]
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "7aA9V6h0GTlt"
},
"execution_count": 3,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment