# Representing Julia expressions AST using a directed graph

**URL:** <https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751>\
**Category:** Visualization\
**Tags:** question, package, plotting, visualization\
**Created:** [May 26, 2024, 4:19pm UTC](https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751 "2024-05-26T16:19:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 26, 2024, 4:19pm UTC](https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751/1 "2024-05-26T16:19:38Z")

</div>

My graph should represent the AST of Julia expressions like `y = x + 10/a` for instance; there is a catch though, these `y`s and `x`s are not just mere numbers; they are custom types (wrappers) that hold, among other things, numeric values.

I want my graph to also represent the label of the object `x`, which also holds a value, say 8.0

---

<div class="post-metadata">

**Author:** ![Julius\_Martensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julius_martensen/32/17077_2.png) [@Julius\_Martensen](https://discourse.julialang.org/u/Julius_Martensen)\
**Post date:** [May 26, 2024, 5:37pm UTC](https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751/2 "2024-05-26T17:37:14Z")

</div>

Maybe GraphMakie.jl can help you here. If you have the associated DAG, you could just set the labels.

---

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 26, 2024, 6:43pm UTC](https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751/3 "2024-05-26T18:43:16Z")

</div>

could you please point out to an example or a documentation link? I looked into the docs, but didn’t find a simple use case

---

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 26, 2024, 7:39pm UTC](https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751/4 "2024-05-26T19:39:43Z")

</div>

By the way, I expect it to look like:

`a|data: 8.0 -------> b|data: 5.0`

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [May 26, 2024, 8:39pm UTC](https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751/5 "2024-05-26T20:39:40Z")

</div>

Does this help?

```julia-repl
julia> using SimpleExpressions, AbstractTrees

julia> @symbolic x y;

julia> expression = 2*x + 3*x*y
(2 * x) + (y * (3 * x))

julia> print_tree(expression)
+
├─ *
│ ├─ 2
│ └─ x
└─ *
   ├─ y
   └─ *
      ├─ 3
      └─ x

```

On Slack I recommended other solutions, but now it seems that you just want a visualization of the tree?

BTW, if you take a look at the AbstractTrees.jl documentation, it points to D3Trees.jl, which can visualize the expression tree in a more graphic and interactive manner.

---

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 26, 2024, 10:15pm UTC](https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751/6 "2024-05-26T22:15:41Z")

</div>

thank you, this is the closest to what I’m looking for:

![image](https://global.discourse-cdn.com/julialang/original/3X/6/2/62caab1620678115ebdbe75ba2bc93bbcd85fc97.png)  
(from [Deep Neural Networks As Computational Graphs | by Tyler Elliot Bettilyon | Teb’s Lab | Medium](https://medium.com/tebs-lab/deep-neural-networks-as-computational-graphs-867fcaa56c9))

I don’t care too much about the style of the graph as long as I have control over the nodes’ display (meaning, I can customize the information to be displayed on each node)… like an “AST weighted computational graph”

Hopefully, that makes sense

---

<div class="post-metadata">

**Author:** ![slwu89](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/slwu89/32/217323_2.png) [@slwu89](https://discourse.julialang.org/u/slwu89)\
**Post date:** [May 26, 2024, 11:58pm UTC](https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751/7 "2024-05-26T23:58:21Z")

</div>

For really fine control over labels attached to each node and edge, a tool you may consider is GraphViz, which can allow for fairly complex types of information to be included in each node (tables, for example [Node Shapes | Graphviz](https://graphviz.org/doc/info/shapes.html#html)).There is a graphviz\_jll package that you could use to link to it.

---

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 27, 2024, 10:49am UTC](https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751/8 "2024-05-27T10:49:17Z")

</div>

Thank you.

I spent a lot of time looking into different libraries; I decided to use Graphviz but through Python using `PyCall`, because the Julia version is not maintained anymore (for at least a couple of years).

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [May 27, 2024, 11:11am UTC](https://discourse.julialang.org/t/representing-julia-expressions-ast-using-a-directed-graph/114751/9 "2024-05-27T11:11:44Z")

</div>

Personally I wouldn’t bother with any library bindings, instead I’d just generate the graph descriptions as text.

FTR, alternatives to Graphviz include:

- [Graphia](https://graphia.app), [Git repo](https://github.com/graphia-app/graphia): for interactive 3D visualization

- [Metapost](https://en.wikipedia.org/wiki/MetaPost)

- [Pgf/Tikz](https://en.wikipedia.org/wiki/PGF/TikZ)

- [Asymptote](https://en.wikipedia.org/wiki/Asymptote_(vector_graphics_language))

- [D3.js](https://en.wikipedia.org/wiki/D3.js)

BTW, a fun fact that I think not many people know, is that AFAIK Graphviz originated at Bell Labs together with Unix and other things.
