# TreeView package release for visualizing syntax trees

**URL:** <https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759>\
**Category:** Community\
**Tags:** tree\
**Created:** [January 29, 2017, 12:20pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759 "2017-01-29T12:20:47Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 29, 2017, 12:20pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/1 "2017-01-29T12:20:47Z")

</div>

I have just released the first version of the [`TreeView` package](https://github.com/dpsanders/TreeView.jl/), which makes it easy to visualize the syntax tree of a Julia `Expr`ession as a graph.

It is as simple as typing

```julia
using TreeView
@tree x^2 + y^2

```

in an IJulia notebook.  
You can see a couple of examples [here](https://github.com/dpsanders/TreeView.jl/blob/master/examples/TreeView.ipynb).

This uses the nice [`TikzGraphs` package](https://github.com/sisl/TikzGraphs.jl), so requires LaTeX to be installed on your system, as well as `pdf2svg`. (See the README of `TikzGraphs`.)

Apart from being useful for visualization purposes, it is also intended as a pedagogical example of how to walk a syntax tree and do basic metaprogramming.

Suggestions and Pull Requests are, of course, welcome.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 29, 2017, 4:37pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/2 "2017-01-29T16:37:26Z")

</div>

Thanks @dpsanders, it can be very useful for people playing with Deep Learning or Mathematical Optimization. It would be great if all Julia packages that manipulate Julia expressions somehow used the same visualization API.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 29, 2017, 4:48pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/3 "2017-01-29T16:48:17Z")

</div>

> [@juliohm](#):
>
> It would be great if all Julia packages that manipulate Julia expressions somehow used the same visualization API.

I believe there’s already a plot recipe for handling something like this. I wonder how it’s different. This does look nice though.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [January 29, 2017, 4:49pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/4 "2017-01-29T16:49:56Z")

</div>

> [@juliohm](#):
>
> same visualization API.

Which other APIs do you in this area?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 29, 2017, 4:51pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/5 "2017-01-29T16:51:36Z")

</div>

There is indeed a recipe, I actually suggested the recipe to be part of Documenter.jl to have type DAGs a la Doxygen: [https://github.com/JuliaDocs/Documenter.jl/issues/364](https://github.com/JuliaDocs/Documenter.jl/issues/364)

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 29, 2017, 4:52pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/6 "2017-01-29T16:52:53Z")

</div>

@lobingera I am not following the development of the Deep Learning frameworks in Julia closely, but I am sure they have a representation of the computation in graph form that can be visualized somewhere.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 29, 2017, 5:24pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/7 "2017-01-29T17:24:51Z")

</div>

I forgot to say that apart from being useful in itself, it is also intended as a pedagogical example of how to walk a syntax tree and do basic metaprogramming.

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [January 29, 2017, 5:48pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/8 "2017-01-29T17:48:15Z")

</div>

It’s indeed a great pedagogical tool. I also needed to install `pdf2svg` (which wasn’t part of TeXLive on OSX). In the example with functions `f` and `g`, is it possible to go recursively into the functions?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 29, 2017, 6:23pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/9 "2017-01-29T18:23:39Z")

</div>

I’m not quite sure what you mean by “recursively” here.  
The package will just display a literal tree of the expression itself; no evaluation is carried out.  
Can you give an example of what you had in mind?

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [January 29, 2017, 7:41pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/10 "2017-01-29T19:41:25Z")

</div>

What I mean is descend recursively into functions. `f` itself has an expression graph associated to it. For example instead of

![](https://global.discourse-cdn.com/julialang/original/3X/4/f/4f1dea06b23e2b3ecb882607acbc5fb1bcaca25e.png)

Can we get

![](https://global.discourse-cdn.com/julialang/original/3X/d/4/d4cb185e8eacefa11282f20fdd257c62b23435fa.png)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 30, 2017, 11:18am UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/11 "2017-01-30T11:18:37Z")

</div>

That seems quite difficult to do in general. In the case you show it may not be hard, but what if you have eg f(1,y)? This is getting into parser territory. Maybe use Sugar.jl?

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [January 30, 2017, 1:07pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/12 "2017-01-30T13:07:49Z")

</div>

_Note to myself: Ask concrete and understandable questions._

What i actually wanted to ask was: Have you seen other visualisation APIs in use here? Going to Tikz and LaTeX in the background sounds too heavy for drawing “simple” trees. I always wondered if Compose’s introspect could be reused here:  
[http://giovineitalia.github.io/Compose.jl/latest/#Trees-can-be-visualized-with-introspect-1](http://giovineitalia.github.io/Compose.jl/latest/#Trees-can-be-visualized-with-introspect-1)

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [January 30, 2017, 2:10pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/13 "2017-01-30T14:10:12Z")

</div>

> [@juliohm](#):
>
> There is indeed a recipe

Here’s an example: [GitHub - JuliaPlots/GraphRecipes.jl: Graph-related recipes to be used with Plots.jl](https://github.com/JuliaPlots/PlotRecipes.jl#julia-code----ast)

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 30, 2017, 4:46pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/14 "2017-01-30T16:46:13Z")

</div>

That is precisely what you are looking for @dpo

---

<div class="post-metadata">

**Author:** ![gabrielgellner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielgellner/32/75_2.png) [@gabrielgellner](https://discourse.julialang.org/u/gabrielgellner)\
**Post date:** [January 30, 2017, 7:43pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/15 "2017-01-30T19:43:49Z")

</div>

How hard might it be to get a style more like the tikz trees shown? (ie black and wite without boxes etc) To my eye they look much nicer, but I would rather use Plots.jl

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 30, 2017, 8:04pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/16 "2017-01-30T20:04:10Z")

</div>

It just requires rewriting the plot recipe.

---

<div class="post-metadata">

**Author:** ![gabrielgellner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielgellner/32/75_2.png) [@gabrielgellner](https://discourse.julialang.org/u/gabrielgellner)\
**Post date:** [January 30, 2017, 8:17pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/17 "2017-01-30T20:17:35Z")

</div>

lol. Good point … looking at the code for the recipe, I realize I don’t really understand how it works … so installing tikz it is … Thanks for the cool package.

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [January 30, 2017, 10:45pm UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/18 "2017-01-30T22:45:02Z")

</div>

As far as I can tell, the Plots recipe doesn’t do it either. Anyways, it was just curiosity.

![](https://global.discourse-cdn.com/julialang/original/3X/5/4/5458428055a5516a91ae16da746834238742994d.png)

![](https://global.discourse-cdn.com/julialang/original/3X/f/4/f4943a855c0f4cf6e7f96c9383bd3cedcf08fb4a.png)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 31, 2017, 7:07am UTC](https://discourse.julialang.org/t/treeview-package-release-for-visualizing-syntax-trees/1759/19 "2017-01-31T07:07:38Z")

</div>

You could try passing arguments such as `method = :tree, markercolor = :white, markerstrokewidth = 0` to the plot recipe?
