Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add mermaid output to show_graph #20631

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

kjgoodrick
Copy link

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 the show_graph method instead of explain because explain appears to be for generating printable representations of the query plan wheras show_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 calls explain.

Compared with #20607 this PR generates the mermaid string in rust instead of in python.

Here is a sample output:

image

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.

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Jan 9, 2025
Copy link

codecov bot commented Jan 9, 2025

Codecov Report

Attention: Patch coverage is 4.54545% with 63 lines in your changes missing coverage. Please review.

Project coverage is 79.02%. Comparing base (247f0b1) to head (11767b9).

Files with missing lines Patch % Lines
crates/polars-lazy/src/mermaid.rs 0.00% 38 Missing ⚠️
py-polars/polars/lazyframe/frame.py 5.88% 14 Missing and 2 partials ⚠️
py-polars/polars/_utils/various.py 16.66% 5 Missing ⚠️
crates/polars-python/src/lazyframe/general.rs 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #20631      +/-   ##
==========================================
- Coverage   79.03%   79.02%   -0.02%     
==========================================
  Files        1557     1558       +1     
  Lines      220528   220592      +64     
  Branches     2510     2514       +4     
==========================================
+ Hits       174303   174318      +15     
- Misses      45651    45698      +47     
- Partials      574      576       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mcrumiller
Copy link
Contributor

Any way to set it to fixed-width font like Consolas? Reading code in variable-width font is just hard on the eyes.

@kjgoodrick
Copy link
Author

@mcrumiller Yes, it is possible to specify a monospace font family in the initialization. If we were to do that, I think it would make sense to generate the raw output without any special settings, but we could insert it prior to displaying to users.

As an example, here is the same chart as above with an initialization of %%{init: {"fontFamily": "monospace"}}%%

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to LazyFrame.explain to export logical plan as Mermaid string
2 participants