Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display graphviz dot in Jupyter using IPython2
{
"cells": [
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from IPython.display import SVG\n",
"from IPython.display import Image"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test.gv\n"
]
}
],
"source": [
"%%file test.gv\n",
"\n",
"digraph {\n",
" a -> b\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg height=\"116pt\" viewBox=\"0.00 0.00 62.00 116.00\" width=\"62pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><g class=\"graph\" id=\"graph0\" transform=\"scale(1 1) rotate(0) translate(4 112)\"><title>%3</title><polygon fill=\"white\" points=\"-4,4 -4,-112 58,-112 58,4 -4,4\" stroke=\"none\"/><!-- a --><g class=\"node\" id=\"node1\"><title>a</title><ellipse cx=\"27\" cy=\"-90\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"black\"/><text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"27\" y=\"-86.3\">a</text></g><!-- b --><g class=\"node\" id=\"node2\"><title>b</title><ellipse cx=\"27\" cy=\"-18\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"black\"/><text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"27\" y=\"-14.3\">b</text></g><!-- a&#45;&gt;b --><g class=\"edge\" id=\"edge1\"><title>a-&gt;b</title><path d=\"M27,-71.6966C27,-63.9827 27,-54.7125 27,-46.1124\" fill=\"none\" stroke=\"black\"/><polygon fill=\"black\" points=\"30.5001,-46.1043 27,-36.1043 23.5001,-46.1044 30.5001,-46.1043\" stroke=\"black\"/></g></g></svg>"
],
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"out = !dot -Tsvg test.gv\n",
"SVG(\"\".join(out))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg height=\"116pt\" viewBox=\"0.00 0.00 62.00 116.00\" width=\"62pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g class=\"graph\" id=\"graph0\" transform=\"scale(1 1) rotate(0) translate(4 112)\">\n",
"<title>%3</title>\n",
"<polygon fill=\"white\" points=\"-4,4 -4,-112 58,-112 58,4 -4,4\" stroke=\"none\"/>\n",
"<!-- a -->\n",
"<g class=\"node\" id=\"node1\"><title>a</title>\n",
"<ellipse cx=\"27\" cy=\"-90\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"27\" y=\"-86.3\">a</text>\n",
"</g>\n",
"<!-- b -->\n",
"<g class=\"node\" id=\"node2\"><title>b</title>\n",
"<ellipse cx=\"27\" cy=\"-18\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"27\" y=\"-14.3\">b</text>\n",
"</g>\n",
"<!-- a&#45;&gt;b -->\n",
"<g class=\"edge\" id=\"edge1\"><title>a-&gt;b</title>\n",
"<path d=\"M27,-71.6966C27,-63.9827 27,-54.7125 27,-46.1124\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"30.5001,-46.1043 27,-36.1043 23.5001,-46.1044 30.5001,-46.1043\" stroke=\"black\"/>\n",
"</g>\n",
"</g>\n",
"</svg>"
],
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"!dot -Tsvg test.gv -o test.svg\n",
"SVG(filename=\"test.svg\")"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAFMAAACbCAYAAAAeCafZAAAABmJLR0QA/wD/AP+gvaeTAAAMOUlE\nQVR4nO2da0yT1x/Hv70AlYuwQrsArUyHtUCNmG0ZXWCaTTcdaAQCk3mZS3SLIUMTNpuwhOmWvRgZ\nMt0Ll2XJ5pZlm8w4s0Xj3DKFVJ0i2eoVujCgQHACUqPQ0svv/8K/xNkLhZ72acn5JL6o5/R5vv2k\nF+A5p18RERE4LGgWC51gNsFlMoTLZIhU6AAP4nQ6cePGDfT09GBsbAzj4+Ow2WyT43PnzoVEIoFS\nqURmZiZSU1MFTOuJIDLtdjva2tpw6dIlXL58GVeuXIHZbMbg4CBcLlfAx5kzZw7UajVycnKQl5eH\nxYsXIz8/H1qtNoTpfSMKx6e52+3GmTNncPz4cbS2tuLChQuw2WxQKBTQarXQarXIzs6GWq2GWq3G\nvHnzkJSU5PVYRITBwUH09fWhr68PPT096OjomPw3Pj4OpVKJwsJCLF++HGvWrMFjjz0W6ocIAM2g\nEPL777/T1q1bSalUEgDSarW0fft2+u6772hgYID5+VwuF/3111+0f/9+Ki8vp9TUVAJA+fn5tGfP\nHurt7WV+zgc4xFzmnTt36MCBA6TT6QgALV26lD744AO6du0a61NNicPhoJMnT9L27dtJqVSSRCKh\nsrIy+u2330JxOnYyR0ZGyGAwUFJSEsnlcjIYDHT9+nVWhw8al8tFJ0+epIqKCpJIJKTT6ejgwYPk\ncrlYnSJ4mS6Xiz777DNSKBSUkpJC9fX1NDw8zCJcyGhvb6fS0lISiUSk1+upvb2dxWGDk2kymeiZ\nZ56h2NhYqquro9HRURahwobJZKIVK1aQRCKhmpoaun37djCHm5lMt9tNH3/8McXFxdFzzz0XUS/n\nmfDtt99SRkYGZWdn04ULF2Z6mOnLHB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h/eaM4P3mjOD95ozg/eYM4f3mDOD95ozg/eaM4P3mjOD95gzg/eYM\n4P3mDOD95kHC+82DhPebzxDeb877zdnC+805vuD95izhMhnCZTJECqBZ6BCzhHP/A5fVWGykDAxU\nAAAAAElFTkSuQmCC\n",
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"!dot -Tpng test.gv -o test.png\n",
"Image(filename=\"test.png\")"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%script dot -Tsvg --out svg\n",
"\n",
"digraph {\n",
" a -> b\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg height=\"116pt\" viewBox=\"0.00 0.00 62.00 116.00\" width=\"62pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g class=\"graph\" id=\"graph0\" transform=\"scale(1 1) rotate(0) translate(4 112)\">\n",
"<title>%3</title>\n",
"<polygon fill=\"white\" points=\"-4,4 -4,-112 58,-112 58,4 -4,4\" stroke=\"none\"/>\n",
"<!-- a -->\n",
"<g class=\"node\" id=\"node1\"><title>a</title>\n",
"<ellipse cx=\"27\" cy=\"-90\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"27\" y=\"-86.3\">a</text>\n",
"</g>\n",
"<!-- b -->\n",
"<g class=\"node\" id=\"node2\"><title>b</title>\n",
"<ellipse cx=\"27\" cy=\"-18\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"27\" y=\"-14.3\">b</text>\n",
"</g>\n",
"<!-- a&#45;&gt;b -->\n",
"<g class=\"edge\" id=\"edge1\"><title>a-&gt;b</title>\n",
"<path d=\"M27,-71.6966C27,-63.9827 27,-54.7125 27,-46.1124\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"30.5001,-46.1043 27,-36.1043 23.5001,-46.1044 30.5001,-46.1043\" stroke=\"black\"/>\n",
"</g>\n",
"</g>\n",
"</svg>"
],
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"SVG(svg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.