feat: Add mermaid output to show_graph
#20631
Open
+116
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request would close #12075 and supersedes #20607. Thanks again to @ritchie46, @eitsupi, and @alexander-beedie for their feedback.
This PR adds support for mermaid string generation to polars. In #12075 it was suggested that mermaid output be added to
explain
, but I believe that it makes more sense to have mermaid output come from theshow_graph
method instead ofexplain
becauseexplain
appears to be for generating printable representations of the query plan wherasshow_graph
is for generating strings that can be fed to a visualization engine (currently just graphviz).In addition to being able to output mermaid strings this PR will output a mermaid diagram if the user is in a notebook and graphviz is not installed when calling
show_graph
. I think at some point it may be a better user experience to always export mermaid instead of the SVG in notebooks, but for now I did not want to change the behavior. I also considered adding functionality that would change the behavior of_repr_html_
to show a mermaid diagram if graphviz is not installed, but if without a way to check if the user can render mermaid this could be a worse user experience than the current behavior which callsexplain
.Compared with #20607 this PR generates the mermaid string in rust instead of in python.
Here is a sample output:
Note that the mermaid renderer in Jupyter Lab and VS Code (with extension) is theme aware, so the graph changes color with the user theme.