Skip to content

Commit

Permalink
Monkey-patch pygwalker.walk
Browse files Browse the repository at this point in the history
Monkey-patch pygwalker.walk when running in a marimo notebook to point
to pygwalker.api.marimo.walk, so that pygwalker code examples from the wild
work automatically.
  • Loading branch information
akshayka committed Nov 5, 2024
1 parent 4c20bbc commit 2a5d349
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions marimo/_output/formatters/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from marimo._output.formatters.panel_formatters import PanelFormatter
from marimo._output.formatters.plotly_formatters import PlotlyFormatter
from marimo._output.formatters.pyecharts_formatters import PyechartsFormatter
from marimo._output.formatters.pygwalker_formatters import PygWalkerFormatter
from marimo._output.formatters.seaborn_formatters import SeabornFormatter
from marimo._output.formatters.structures import StructuresFormatter
from marimo._output.formatters.sympy_formatters import SympyFormatter
Expand All @@ -39,6 +40,7 @@
PandasFormatter.package_name(): PandasFormatter(),
PolarsFormatter.package_name(): PolarsFormatter(),
PyArrowFormatter.package_name(): PyArrowFormatter(),
PygWalkerFormatter.package_name(): PygWalkerFormatter(),
PlotlyFormatter.package_name(): PlotlyFormatter(),
SeabornFormatter.package_name(): SeabornFormatter(),
LeafmapFormatter.package_name(): LeafmapFormatter(),
Expand Down
25 changes: 25 additions & 0 deletions marimo/_output/formatters/pygwalker_formatters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations


from marimo._output.formatters.formatter_factory import FormatterFactory
from marimo._runtime.context.utils import running_in_notebook


class PygWalkerFormatter(FormatterFactory):
@staticmethod
def package_name() -> str:
return "pygwalker"

def register(self) -> None:
if running_in_notebook():
# monkey-patch pygwalker.walk to work in marimo;
# older versions of marimo may not have api.marimo, and not sure
# about pygwalker's API stability, so use a coarse try/except
try:
import pygwalker
from pygwalker.api.marimo import walk

pygwalker.walk = walk # type: ignore
except Exception:
pass
23 changes: 23 additions & 0 deletions marimo/_smoke_tests/pygwalker_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import marimo

__generated_with = "0.9.14"
app = marimo.App(width="medium")


@app.cell
def __():
import pygwalker

from vega_datasets import data
return data, pygwalker


@app.cell
def __(data, pygwalker):
df = data.iris()
pygwalker.walk(df)
return (df,)


if __name__ == "__main__":
app.run()

0 comments on commit 2a5d349

Please sign in to comment.