diff --git a/py-polars/polars/_typing.py b/py-polars/polars/_typing.py index 88a0882e5679..e65f45f207e2 100644 --- a/py-polars/polars/_typing.py +++ b/py-polars/polars/_typing.py @@ -187,6 +187,7 @@ def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: TransferEncoding: TypeAlias = Literal["hex", "base64"] WindowMappingStrategy: TypeAlias = Literal["group_to_rows", "join", "explode"] ExplainFormat: TypeAlias = Literal["plain", "tree"] +ShowGraphFormat: TypeAlias = Literal["dot", "mermaid"] # type signature for allowed frame init FrameInitTypes: TypeAlias = Union[ diff --git a/py-polars/polars/_utils/various.py b/py-polars/polars/_utils/various.py index ef583250a254..9585de35a9d1 100644 --- a/py-polars/polars/_utils/various.py +++ b/py-polars/polars/_utils/various.py @@ -46,7 +46,7 @@ from collections.abc import Iterator, MutableMapping, Reversible from polars import DataFrame, Expr - from polars._typing import PolarsDataType, SizeUnit + from polars._typing import PolarsDataType, ShowGraphFormat, SizeUnit if sys.version_info >= (3, 13): from typing import TypeIs @@ -652,6 +652,31 @@ def re_escape(s: str) -> str: return re.sub(f"([{re_rust_metachars}])", r"\\\1", s) +def dot_to_mermaid(dot: str) -> str: + """Not comprehensive, only handles components of the dot language used by polars.""" + edge_regex = r"(?P\w+) -- (?P\w+)" + node_regex = r"(?P\w+)(\s+)?\[label=\"(?P