Skip to content

Instantly share code, notes, and snippets.

Created March 28, 2017 17:55
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save goerz/178811cece8e96ae5378be71b8ea904a to your computer and use it in GitHub Desktop.
Render Sympy Expression as graphical tree inside IPython notebook
def render_sympy_tree(expr):
"""Render the given Sympy Expression as a graphical tree, inside an IPython notebook"""
from import dotprint
import tempfile
import subprocess
import os
from IPython.display import SVG
with tempfile.NamedTemporaryFile(mode='w', delete=False) as out_fh:
out_fh.write(dotprint(expr))['/usr/bin/dot', '-Tsvg',, '-o', + ".svg"])
svg = SVG(filename=( + ".svg"))
os.unlink( + ".svg")
return svg
Copy link

goerz commented Mar 28, 2017

I'm having problems with dot inside a conda environment (due to #1357), which also breaks the graphviz package. The above code solves the problem of rendering the graphviz tree representation of Sympy expressions in Python 3.5, via a temporary file. Note the use of the system /usr/bin/dot instead of just dot (which would refer to the broken binary that's installed by conda).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment