{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Graphviz\n", "\n", "`Graph`- und `Digraph`-Objekte haben eine `_repr_svg_()`-Methode, sodass sie direkt in einem Jupyter-Notebook gerendert und dargestellt werden können." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installation\n", "\n", "Mit [Spack](https://jupyter-tutorial.readthedocs.io/de/latest/productive/envs/spack/index.html) könnt ihr Graphviz in eurem Kernel bereitstellen:\n", "\n", "``` bash\n", "$ spack env activate python-374\n", "$ spack env status\n", "==> In environment python-374\n", "$ spack install py-graphviz ^python@3.7.4%gcc@9.1.0\n", "…\n", "==> Successfully installed py-graphviz\n", "…\n", "==> Updating view at /srv/jupyter/spack/var/spack/environments/python-374/.spack-env/view\n", "```\n", "\n", "Alternativ könnt ihr Graphviz auch direkt in eurem Pipenv-Environment installieren:\n", "\n", "* für Debian/Ubuntu:\n", "\n", " ``` bash\n", "$ sudo apt install graphviz\n", " ```\n", "\n", "* für Mac OS X:\n", "\n", " ``` bash\n", "$ brew install graphviz\n", " ```\n", "\n", "Anschließend könnt ihr dann in eurem Kernel das zugehörige Python-Paket installieren:\n", "\n", "``` bash\n", "$ pipenv install graphviz\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Einfaches Beispiel" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "hello\n", "\n", "hello\n", "\n", "\n", "\n", "world\n", "\n", "world\n", "\n", "\n", "\n", "hello->world\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import graphviz\n", "\n", "d = graphviz.Digraph()\n", "d.edge('hello', 'world')\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dateien schreiben" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello-world.gv.pdf'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from graphviz import Source\n", "\n", "src = Source('digraph \"hello world\" { hello -> world }')\n", "\n", "src.render('hello-world.gv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternativ können Dot-Dateien auch mit anderen Programmen, z.B. [networkx](https://jupyter-tutorial.readthedocs.io/de/latest/viz/matplotlib/networkx.html) erstellt werden:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import networkx as nx\n", "from networkx.drawing.nx_pydot import write_dot\n", "\n", "G = nx.grid_2d_graph(5, 5) # 5x5 grid\n", "write_dot(G, \"grid.gv\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> Siehe auch:\n", "> \n", "> * [NetworkX: Write Dotfile](https://networkx.github.io/documentation/networkx-2.2/auto_examples/pygraphviz/write_dotfile.html#write-dotfile)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dot-Dateien konvertieren oder darstellen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Eine Dot-Datei kann in ein anderes Format, z.B. PDF, PNG, SVG etc., umgewandelt werden mit `render`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello-world.gv.png'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from graphviz import render\n", "\n", "render('dot', 'png', 'hello-world.gv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternativ kann die Dot-Datei auch direkt im Notebook angezeigt werden mit `Source.from_file`:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "hello world\n", "\n", "\n", "\n", "hello\n", "\n", "hello\n", "\n", "\n", "\n", "world\n", "\n", "world\n", "\n", "\n", "\n", "hello->world\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from graphviz import Source\n", "\n", "Source.from_file('hello-world.gv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> Siehe auch:\n", "> \n", "> * [graphviz manual](https://graphviz.readthedocs.io/en/stable/manual.html#jupyter-notebooks)\n", "> * [examples/notebook](https://nbviewer.jupyter.org/github/xflr6/graphviz/blob/master/examples/notebook.ipynb)\n", "> * [examples/graphviz-engines](https://nbviewer.jupyter.org/github/xflr6/graphviz/blob/master/examples/graphviz-engines.ipynb)\n", "> * [examples/graphviz-escapes](https://nbviewer.jupyter.org/github/xflr6/graphviz/blob/master/examples/graphviz-escapes.ipynb)\n", "> * [Graphviz Online](https://dreampuf.github.io/GraphvizOnline/)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.8", "language": "python", "name": "python-38" }, "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.8.12" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 2 }