{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('0.20.dev0', (2, 40, 1))" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%cd -q ..\n", "\n", "import graphviz\n", "\n", "graphviz.__version__, graphviz.version()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def make_graph(node_label=None, graph_name=None, **node_kwargs):\n", " result = graphviz.Digraph(name=graph_name)\n", " result.node('A', label=node_label, **node_kwargs)\n", " print(result)\n", " return result" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "digraph {\n", "\tA [label=\"\\\"\"]\n", "}\n", "\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "A\n", "\n", ""\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_graph(node_label='\"')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "digraph {\n", "\tA [label=\"\\\"\"]\n", "}\n", "\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "A\n", "\n", ""\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_graph(node_label='\\\\\"')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/jovyan/graphviz/quoting.py:88: DotSyntaxWarning: expect syntax error scanning invalid quoted string: '\\\\'\n", " category=exceptions.DotSyntaxWarning)\n", "Error: : syntax error in line 2 scanning a quoted string (missing endquote? longer than 16384?)\n", "String starting:\"\"]\n", "}\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "digraph {\n", "\tA [label=\"\\\"]\n", "}\n", "\n" ] } ], "source": [ "try:\n", " make_graph(node_label='\\\\').pipe(format='svg', encoding='utf-8');\n", "except graphviz.CalledProcessError as e:\n", " assert 'syntax error' in e.stderr" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "digraph {\n", "\tA [label=\"\\\\\"]\n", "}\n", "\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "A\n", "\n", "\\\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_graph(node_label=r'\\\\')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "digraph {\n", "\tA [label=\"\\\\\\\"\"]\n", "}\n", "\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "A\n", "\n", "\\"\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_graph(node_label=r'\\\\\"')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://www.graphviz.org/doc/info/attrs.html#k:escString" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "digraph {\n", "\tA [label=\"node: \\N\"]\n", "}\n", "\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "A\n", "\n", "node: A\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_graph(node_label=r'node: \\N')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "digraph spam {\n", "\tA [label=\"graph: \\G\"]\n", "}\n", "\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "spam\n", "\n", "\n", "\n", "A\n", "\n", "graph: spam\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_graph(node_label=r'graph: \\G', graph_name='spam')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "digraph {\n", "\tA [label=spam URL=\"https://example.org/\\L\"]\n", "}\n", "\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "A\n", "\n", "\n", "spam\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_graph(node_label='spam', URL=r'https://example.org/\\L')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "digraph {\n", "\tA [label=\"centered\\nleft\\lright\\r\"]\n", "}\n", "\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "A\n", "\n", "centered\n", "left\n", "right\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_graph(node_label=r'centered\\nleft\\lright\\r')" ] } ], "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.7.12" } }, "nbformat": 4, "nbformat_minor": 4 }