-
Notifications
You must be signed in to change notification settings - Fork 327
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
Polars extension #3356
Draft
kjgoodrick
wants to merge
2
commits into
marimo-team:main
Choose a base branch
from
kjgoodrick:polars_extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Polars extension #3356
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import re | ||
from typing import TYPE_CHECKING, Any | ||
|
||
from marimo import mermaid | ||
from marimo._dependencies.dependencies import DependencyManager | ||
from marimo._output.hypertext import Html | ||
|
||
if TYPE_CHECKING: | ||
import polars as pl | ||
|
||
if DependencyManager.polars.has(): | ||
import polars as pl | ||
|
||
@pl.api.register_lazyframe_namespace("mo") | ||
class Marimo: | ||
def __init__(self, ldf: pl.LazyFrame): | ||
self._ldf: pl.LazyFrame = ldf | ||
|
||
def show_graph(self, **kwargs: Any) -> Html | str: | ||
# We are specifying raw_output already, so we need to remove if it is passed | ||
raw_output = kwargs.pop("raw_output", False) | ||
|
||
dot = self._ldf.show_graph(raw_output=True, **kwargs) | ||
|
||
if raw_output: | ||
return dot | ||
|
||
return self._dot_to_mermaid_html(dot) | ||
|
||
# _dot_to_mermaid_html is a separate method from show_graph so that in the future | ||
# polars can be updated to output mermaid directly when calling native show_graph | ||
# inside a marimo environment. | ||
# In order to do that we need a function that will not recursively call show_graph | ||
@classmethod | ||
def _dot_to_mermaid_html(self, dot: str) -> Html: | ||
return mermaid(self._polars_dot_to_mermaid(dot)) | ||
|
||
@staticmethod | ||
def _parse_node_label(label: str) -> str: | ||
# replace escaped newlines | ||
label = label.replace(r"\n", "\n") | ||
# replace escaped quotes | ||
label = label.replace('\\"', "#quot;") | ||
return label | ||
|
||
@classmethod | ||
def _polars_dot_to_mermaid(cls, dot: str) -> str: | ||
edge_regex = r"(?P<node1>\w+) -- (?P<node2>\w+)" | ||
node_regex = r"(?P<node>\w+)(\s+)?\[label=\"(?P<label>.*)\"]" | ||
|
||
nodes = [n for n in re.finditer(node_regex, dot)] | ||
edges = [e for e in re.finditer(edge_regex, dot)] | ||
|
||
return "\n".join( | ||
[ | ||
"graph TD", | ||
*[ | ||
f'\t{n["node"]}["{cls._parse_node_label(n["label"])}"]' | ||
for n in nodes | ||
], | ||
*[f'\t{e["node1"]} --- {e["node2"]}' for e in edges], | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import pytest | ||
|
||
from marimo._dependencies.dependencies import DependencyManager | ||
from marimo._output.hypertext import Html | ||
|
||
HAS_DEPS = DependencyManager.polars.has() | ||
|
||
|
||
@pytest.fixture | ||
def simple_lf(): | ||
import polars as pl | ||
|
||
return ( | ||
pl.LazyFrame( | ||
{ | ||
"a": [1, 2, 3], | ||
"b": [4, 5, 6], | ||
} | ||
) | ||
.filter(pl.col("a") > 1) | ||
.group_by("a") | ||
.agg(pl.col("b").sum()) | ||
) | ||
|
||
|
||
@pytest.mark.skipif(not HAS_DEPS, reason="polars is required") | ||
def test_show_graph(simple_lf): | ||
lf = simple_lf | ||
|
||
assert type(lf.mo.show_graph()) is Html | ||
assert type(lf.mo.show_graph(raw_output=True)) is str | ||
|
||
|
||
@pytest.mark.skipif(not HAS_DEPS, reason="polars is required") | ||
def test_dot_to_html(simple_lf): | ||
lf = simple_lf | ||
|
||
dot = lf.show_graph(raw_output=True) | ||
|
||
assert type(lf.mo._dot_to_mermaid_html(dot)) is Html | ||
|
||
|
||
@pytest.mark.skipif(not HAS_DEPS, reason="polars is required") | ||
def test_parse_node_label(simple_lf): | ||
lf = simple_lf | ||
|
||
assert lf.mo._parse_node_label(r"\"") == "#quot;" | ||
assert lf.mo._parse_node_label(r"\n") == "\n" | ||
assert lf.mo._parse_node_label(r"\"\n\"") == "#quot;\n#quot;" | ||
|
||
|
||
@pytest.mark.skipif(not HAS_DEPS, reason="polars is required") | ||
def test_dot_to_mermaid(simple_lf): | ||
lf = simple_lf | ||
dot = lf.show_graph(raw_output=True) | ||
|
||
mermaid_str = lf.mo._polars_dot_to_mermaid(dot) | ||
|
||
assert type(mermaid_str) is str | ||
assert mermaid_str == ( | ||
"""graph TD | ||
p2["TABLE | ||
π 2/2; | ||
σ [(col(#quot;a#quot;)) > (1)]"] | ||
p1["AGG [col(#quot;b#quot;).sum()] | ||
BY | ||
[col(#quot;a#quot;)]"] | ||
p1 --- p2""" | ||
) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If registration happens on import of the polars module, then this can be removed.
https://github.com/marimo-team/marimo/blob/main/marimo/_output/formatters/df_formatters.py
As far as I can tell there's no need to add
lazyframe
to the marimo public API?