{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tales to tell\n", "\n", "module: [graphviz](https://graphviz.readthedocs.io/en/stable/)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from graphviz import Digraph" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "dot = Digraph()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Add nodes 1 and 2\n", "dot.node('1')\n", "dot.node('2')\n", "\n", "# Add edge between 1 and 2\n", "dot.edges(['12'])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "text = \"\"\"I have a tale to tell\n", "Sometimes it gets so hard to hide it well\n", "I was not ready for the fall\n", "Too blind to see the writing on the wall\"\"\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I\n", "have\n", "a\n", "tale\n", "to\n", "tell\n", "Sometimes\n", "it\n", "gets\n", "so\n", "hard\n", "to\n", "hide\n", "it\n", "well\n", "I\n", "was\n", "not\n", "ready\n", "for\n", "the\n", "fall\n", "Too\n", "blind\n", "to\n", "see\n", "the\n", "writing\n", "on\n", "the\n", "wall\n" ] } ], "source": [ "for word in text.split():\n", " print (word)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://docs.python.org/3/library/itertools.html#recipes" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from itertools import tee\n", "def pairwise(iterable):\n", " \"s -> (s0,s1), (s1,s2), (s2, s3), ...\"\n", " a, b = tee(iterable)\n", " next(b, None)\n", " return zip(a, b)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I have\n", "have a\n", "a tale\n", "tale to\n", "to tell\n", "tell Sometimes\n", "Sometimes it\n", "it gets\n", "gets so\n", "so hard\n", "hard to\n", "to hide\n", "hide it\n", "it well\n", "well I\n", "I was\n", "was not\n", "not ready\n", "ready for\n", "for the\n", "the fall\n", "fall Too\n", "Too blind\n", "blind to\n", "to see\n", "see the\n", "the writing\n", "writing on\n", "on the\n", "the wall\n" ] } ], "source": [ "for w1, w2 in pairwise(text.split()):\n", " print (w1, w2)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "for w1, w2 in pairwise(text.split()):\n", " dot.edge(w1, w2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "\n", "\n", "\n", "I\n", "\n", "I\n", "\n", "\n", "\n", "have\n", "\n", "have\n", "\n", "\n", "\n", "I->have\n", "\n", "\n", "\n", "\n", "\n", "was\n", "\n", "was\n", "\n", "\n", "\n", "I->was\n", "\n", "\n", "\n", "\n", "\n", "a\n", "\n", "a\n", "\n", "\n", "\n", "have->a\n", "\n", "\n", "\n", "\n", "\n", "tale\n", "\n", "tale\n", "\n", "\n", "\n", "a->tale\n", "\n", "\n", "\n", "\n", "\n", "to\n", "\n", "to\n", "\n", "\n", "\n", "tale->to\n", "\n", "\n", "\n", "\n", "\n", "tell\n", "\n", "tell\n", "\n", "\n", "\n", "to->tell\n", "\n", "\n", "\n", "\n", "\n", "hide\n", "\n", "hide\n", "\n", "\n", "\n", "to->hide\n", "\n", "\n", "\n", "\n", "\n", "see\n", "\n", "see\n", "\n", "\n", "\n", "to->see\n", "\n", "\n", "\n", "\n", "\n", "Sometimes\n", "\n", "Sometimes\n", "\n", "\n", "\n", "tell->Sometimes\n", "\n", "\n", "\n", "\n", "\n", "it\n", "\n", "it\n", "\n", "\n", "\n", "Sometimes->it\n", "\n", "\n", "\n", "\n", "\n", "gets\n", "\n", "gets\n", "\n", "\n", "\n", "it->gets\n", "\n", "\n", "\n", "\n", "\n", "well\n", "\n", "well\n", "\n", "\n", "\n", "it->well\n", "\n", "\n", "\n", "\n", "\n", "so\n", "\n", "so\n", "\n", "\n", "\n", "gets->so\n", "\n", "\n", "\n", "\n", "\n", "hard\n", "\n", "hard\n", "\n", "\n", "\n", "so->hard\n", "\n", "\n", "\n", "\n", "\n", "hard->to\n", "\n", "\n", "\n", "\n", "\n", "hide->it\n", "\n", "\n", "\n", "\n", "\n", "well->I\n", "\n", "\n", "\n", "\n", "\n", "not\n", "\n", "not\n", "\n", "\n", "\n", "was->not\n", "\n", "\n", "\n", "\n", "\n", "ready\n", "\n", "ready\n", "\n", "\n", "\n", "not->ready\n", "\n", "\n", "\n", "\n", "\n", "for\n", "\n", "for\n", "\n", "\n", "\n", "ready->for\n", "\n", "\n", "\n", "\n", "\n", "the\n", "\n", "the\n", "\n", "\n", "\n", "for->the\n", "\n", "\n", "\n", "\n", "\n", "fall\n", "\n", "fall\n", "\n", "\n", "\n", "the->fall\n", "\n", "\n", "\n", "\n", "\n", "writing\n", "\n", "writing\n", "\n", "\n", "\n", "the->writing\n", "\n", "\n", "\n", "\n", "\n", "wall\n", "\n", "wall\n", "\n", "\n", "\n", "the->wall\n", "\n", "\n", "\n", "\n", "\n", "Too\n", "\n", "Too\n", "\n", "\n", "\n", "fall->Too\n", "\n", "\n", "\n", "\n", "\n", "blind\n", "\n", "blind\n", "\n", "\n", "\n", "Too->blind\n", "\n", "\n", "\n", "\n", "\n", "blind->to\n", "\n", "\n", "\n", "\n", "\n", "see->the\n", "\n", "\n", "\n", "\n", "\n", "on\n", "\n", "on\n", "\n", "\n", "\n", "writing->on\n", "\n", "\n", "\n", "\n", "\n", "on->the\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'tale.pdf'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot.render(\"tale\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0mdot\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrender\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdirectory\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mview\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcleanup\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mformat\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mrenderer\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mformatter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mquiet\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mquiet_view\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Save the source to file and render with the Graphviz engine.\n", "\n", "Args:\n", " filename: Filename for saving the source (defaults to ``name`` + ``'.gv'``)\n", " directory: (Sub)directory for source saving and rendering.\n", " view (bool): Open the rendered result with the default application.\n", " cleanup (bool): Delete the source file after rendering.\n", " format: The output format used for rendering (``'pdf'``, ``'png'``, etc.).\n", " renderer: The output renderer used for rendering (``'cairo'``, ``'gd'``, ...).\n", " formatter: The output formatter used for rendering (``'cairo'``, ``'gd'``, ...).\n", " quiet (bool): Suppress ``stderr`` output from the layout subprocess.\n", " quiet_view (bool): Suppress ``stderr`` output from the viewer process\n", " (implies ``view=True``, ineffective on Windows).\n", "Returns:\n", " The (possibly relative) path of the rendered file.\n", "Raises:\n", " ValueError: If ``format``, ``renderer``, or ``formatter`` are not known.\n", " graphviz.RequiredArgumentError: If ``formatter`` is given but ``renderer`` is None.\n", " graphviz.ExecutableNotFound: If the Graphviz executable is not found.\n", " subprocess.CalledProcessError: If the exit status is non-zero.\n", " RuntimeError: If viewer opening is requested but not supported.\n", "\n", "The layout command is started from the directory of ``filepath``, so that\n", "references to external files (e.g. ``[image=...]``) can be given as paths\n", "relative to the DOT source file.\n", "\u001b[0;31mFile:\u001b[0m /opt/tljh/user/lib/python3.7/site-packages/graphviz/files.py\n", "\u001b[0;31mType:\u001b[0m method\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "dot.render?" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# Example of using the graph as a generator ??" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'tale.png'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot.render(\"tale\", format=\"png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](tale.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }