marimo-team / marimo

A reactive notebook for Python — run reproducible experiments, execute as a script, deploy as an app, and version with git.
https://marimo.io
Apache License 2.0
7.78k stars 274 forks source link

Support IPython style `_repr_mimebundle_` notation #2880

Open rgbkrk opened 17 hours ago

rgbkrk commented 17 hours ago

Describe the bug

Some objects in Python land use the _repr_mimebundle_ function to denote representing itself with a variety of media types mimetypes.

class Example:
    def __init__(self, content="This is my _example_"):
        self.content = content

    def _repr_mimebundle_(self, include=None, exclude=None):
        return {"text/markdown": self.content}

Example()

I realize marimo supports _repr_markdown_ but there are already libraries that have _repr_mimebundle_ that I'd like to use without having to add shims to.

Environment

{
  "marimo": "0.9.20",
  "OS": "Darwin",
  "OS Version": "24.1.0",
  "Processor": "arm",
  "Python Version": "3.12.2",
  "Binaries": {
    "Browser": "130.0.6723.117",
    "Node": "v20.11.0"
  },
  "Dependencies": {
    "click": "8.1.7",
    "docutils": "0.18.1",
    "itsdangerous": "2.2.0",
    "jedi": "0.19.1",
    "markdown": "3.7",
    "narwhals": "1.13.5",
    "packaging": "23.2",
    "psutil": "5.9.0",
    "pygments": "2.18.0",
    "pymdown-extensions": "10.12",
    "pyyaml": "6.0.1",
    "ruff": "0.5.5",
    "starlette": "0.37.2",
    "tomlkit": "0.13.2",
    "typing-extensions": "4.12.2",
    "uvicorn": "0.30.3",
    "websockets": "12.0"
  },
  "Optional Dependencies": {
    "altair": "5.0.1",
    "anywidget": "0.9.13",
    "duckdb": "1.1.2",
    "pandas": "2.2.3",
    "polars": "1.12.0",
    "pyarrow": "18.0.0"
  }
}

Code to reproduce

class Example:
    def __init__(self, content="This is my _example_"):
        self.content = content

    def _repr_mimebundle_(self, include=None, exclude=None):
        return {"text/markdown": self.content}

Example()
rgbkrk commented 17 hours ago

Oops, this should have been a feature request not a bug. Sorry!

mscolnick commented 14 hours ago

We do already support _repr_mimebundle_. We just render text/markdown as a markdown code editor, which might not be the desired effect. What are you expecting?

rgbkrk commented 4 hours ago

At least for compatibility, I'd want it to render the markdown.

mscolnick commented 4 hours ago

Makes sense. I've added it here https://github.com/marimo-team/marimo/pull/2881