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
6.24k stars 202 forks source link

vegafusion plots don't show #2282

Closed MarcoGorelli closed 1 week ago

MarcoGorelli commented 1 week ago

Describe the bug

The code below gives

Traceback (most recent call last):
  File "/home/marcogorelli/polars-dev/.venv/lib/python3.12/site-packages/marimo/_output/formatting.py", line 172, in try_format
    mimetype, data = formatter(obj)
                     ^^^^^^^^^^^^^^
  File "/home/marcogorelli/polars-dev/.venv/lib/python3.12/site-packages/marimo/_output/formatters/altair_formatters.py", line 45, in _show_chart
    srcdoc=html.escape(chart.to_html()),
                       ^^^^^^^^^^^^^^^
  File "/home/marcogorelli/polars-dev/.venv/lib/python3.12/site-packages/altair/vegalite/v5/api.py", line 1952, in to_html
    self.to_dict(),
    ^^^^^^^^^^^^^^
  File "/home/marcogorelli/polars-dev/.venv/lib/python3.12/site-packages/altair/vegalite/v5/api.py", line 3778, in to_dict
    return super().to_dict(**kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/marcogorelli/polars-dev/.venv/lib/python3.12/site-packages/altair/vegalite/v5/api.py", line 1847, in to_dict
    raise ValueError(msg)
ValueError: When the "vegafusion" data transformer is enabled, the 
to_dict() and to_json() chart methods must be called with format="vega". 
For example: 
    >>> chart.to_dict(format="vega")
    >>> chart.to_jso

Environment

{ "marimo": "0.8.13", "OS": "Linux", "OS Version": "5.15.153.1-microsoft-standard-WSL2", "Processor": "x86_64", "Python Version": "3.12.5", "Binaries": { "Browser": "--", "Node": "v20.16.0" }, "Dependencies": { "click": "8.1.7", "importlib-resources": "missing", "jedi": "0.19.1", "markdown": "3.7", "pygments": "2.18.0", "pymdown-extensions": "10.9", "ruff": "0.6.4", "starlette": "0.38.4", "tomlkit": "0.13.2", "typing-extensions": "4.12.2", "uvicorn": "0.30.6", "websockets": "12.0" }, "Optional Dependencies": { "altair": "5.4.1", "duckdb": "1.0.0", "pandas": "2.2.2", "polars": "1.6.0", "pyarrow": "17.0.0" } }

Code to reproduce

import polars as pl
import altair as alt

alt.data_transformers.enable("vegafusion")
alt.Chart(pl.DataFrame({"a": range(6000)})).mark_point().encode(x="a")
mscolnick commented 1 week ago

Thanks for reporting - I'll get a fix out for this today

mscolnick commented 1 week ago

@MarcoGorelli - for a workaround, if you wrap the result in mo.ui.altair_chart it should render properly. but then there is a warning (which you can remove with chart_selection=None, field_selection=None.

I am making this work without mo.ui.altair_chart