A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: downgrade dataframe in non-interactive environments (#7940)
Myles Scolnickba994d1cea23
diff --git a/marimo/_output/formatters/df_formatters.py b/marimo/_output/formatters/df_formatters.pyindex 24d541c44b..7b986f1f75 100644--- a/marimo/_output/formatters/df_formatters.py+++ b/marimo/_output/formatters/df_formatters.py@@ -13,7 +13,7 @@ from marimo._output.formatters.formatter_factory import (FormatterFactory,Unregister,)-from marimo._output.hypertext import is_no_js+from marimo._output.hypertext import is_non_interactivefrom marimo._plugins.stateless.json_output import json_outputfrom marimo._plugins.stateless.mermaid import mermaidfrom marimo._plugins.stateless.plain_text import plain_text@@ -30,7 +30,7 @@ def include_opinionated() -> bool:runtime_context_installed,)- if is_no_js():+ if is_non_interactive():return Falseif runtime_context_installed():diff --git a/marimo/_output/formatting.py b/marimo/_output/formatting.pyindex 4ef900eb6d..78451ed901 100644--- a/marimo/_output/formatting.py+++ b/marimo/_output/formatting.py@@ -28,7 +28,7 @@ from marimo._messaging.mimetypes import KnownMimeTypefrom marimo._output.builder import hfrom marimo._output.formatters.repr_formatters import maybe_get_repr_formatterfrom marimo._output.formatters.utils import src_or_src_doc-from marimo._output.hypertext import Html, is_no_js+from marimo._output.hypertext import Html, is_non_interactivefrom marimo._output.rich_help import mddocfrom marimo._output.utils import flatten_stringfrom marimo._plugins.core.media import io_to_data_url@@ -157,7 +157,7 @@ def get_formatter(# If not explicitly opinionated, we defer to the environment.if include_opinionated is None:- include_opinionated = not is_no_js()+ include_opinionated = not is_non_interactive()# Plain opts out of opinionated formattersif isinstance(obj, Plain):diff --git a/marimo/_output/hypertext.py b/marimo/_output/hypertext.pyindex f53f96ac8c..3215a69ec1 100644--- a/marimo/_output/hypertext.py+++ b/marimo/_output/hypertext.py@@ -137,7 +137,7 @@ class Html(MIME):return self._textdef _mime_(self) -> tuple[KnownMimeType, str]:- if not is_no_js():+ if not is_non_interactive():return ("text/html", self.text)# Try PNG representation first (for objects with _repr_png_)@@ -310,7 +310,7 @@ def patch_html_for_non_interactive_output() -> Iterator[None]:os.environ[MARIMO_NO_JS_KEY] = old_no_js-def is_no_js() -> bool:+def is_non_interactive() -> bool:"""Whether to render HTML objects as best as possible assumingthat this will be rendered without javascript.diff --git a/marimo/_output/md.py b/marimo/_output/md.pyindex 9527b86d2b..253ebc4719 100644--- a/marimo/_output/md.py+++ b/marimo/_output/md.py@@ -14,7 +14,7 @@ import markdown.preprocessors # type: ignoreimport pymdownx.emoji # type: ignorefrom marimo._messaging.mimetypes import KnownMimeType-from marimo._output.hypertext import Html, is_no_js+from marimo._output.hypertext import Html, is_non_interactivefrom marimo._output.md_extensions.breakless_lists import (BreaklessListsExtension,)@@ -268,7 +268,7 @@ class _md(Html):return self._markdown_textdef _mime_(self) -> tuple[KnownMimeType, str]:- no_js = is_no_js()+ no_js = is_non_interactive()if no_js:return ("text/markdown", self._markdown_text)# We return text/markdown instead of text/htmldiff --git a/marimo/_plugins/ui/_impl/altair_chart.py b/marimo/_plugins/ui/_impl/altair_chart.pyindex e5760a76ae..008b953b1d 100644--- a/marimo/_plugins/ui/_impl/altair_chart.py+++ b/marimo/_plugins/ui/_impl/altair_chart.py@@ -22,7 +22,7 @@ from narwhals.typing import IntoDataFrame, IntoLazyFramefrom marimo import _loggersfrom marimo._dependencies.dependencies import DependencyManagerfrom marimo._messaging.mimetypes import KnownMimeType-from marimo._output.hypertext import is_no_js+from marimo._output.hypertext import is_non_interactivefrom marimo._output.rich_help import mddocfrom marimo._plugins.ui._core.ui_element import UIElementfrom marimo._plugins.ui._impl.charts.altair_transformer import (@@ -657,7 +657,7 @@ class altair_chart(UIElement[ChartSelection, ChartDataType]):# Override _mime_ to return an Altair spec in non-JS environmentsdef _mime_(self) -> tuple[KnownMimeType, str]:- if is_no_js():+ if is_non_interactive():return (get_chart_mimetype(spec_format="vega"),chart_to_json(self._chart, validate=False),diff --git a/marimo/_plugins/ui/_impl/dataframes/dataframe.py b/marimo/_plugins/ui/_impl/dataframes/dataframe.pyindex 13723dceea..0d405df59e 100644--- a/marimo/_plugins/ui/_impl/dataframes/dataframe.py+++ b/marimo/_plugins/ui/_impl/dataframes/dataframe.py@@ -11,8 +11,11 @@ from typing import (Literal,Optional,Union,+ cast,)+from marimo._messaging.mimetypes import KnownMimeType+from marimo._output.hypertext import is_non_interactivefrom marimo._output.rich_help import mddocfrom marimo._plugins.ui._core.ui_element import UIElementfrom marimo._plugins.ui._impl.dataframes.transforms.apply import (@@ -226,6 +229,17 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):),)+ # Override _mime_ to return a plain HTML representation in non-interactive environments+ def _mime_(self) -> tuple[KnownMimeType, str]:+ if is_non_interactive():+ # Generates a plain HTML representation of the table data,+ # useful for rendering in the GitHub viewer.+ repr_html = getattr(self._data, "_repr_html_", None)+ if repr_html is not None and callable(repr_html):+ return ("text/html", cast(str, repr_html()))+ return ("text/html", str(self._data))+ return ("text/html", self.text)+def _get_column_types(self) -> list[list[Union[str, int]]]:return [[name, dtype[0], dtype[1]]diff --git a/marimo/_plugins/ui/_impl/plotly.py b/marimo/_plugins/ui/_impl/plotly.pyindex 09466622c0..eab2f43f0a 100644--- a/marimo/_plugins/ui/_impl/plotly.py+++ b/marimo/_plugins/ui/_impl/plotly.py@@ -14,7 +14,7 @@ from typing import (from marimo import _loggersfrom marimo._dependencies.dependencies import DependencyManagerfrom marimo._messaging.mimetypes import KnownMimeType-from marimo._output.hypertext import is_no_js+from marimo._output.hypertext import is_non_interactivefrom marimo._output.rich_help import mddocfrom marimo._plugins.core.web_component import JSONTypefrom marimo._plugins.ui._core.ui_element import UIElement@@ -307,7 +307,7 @@ class plotly(UIElement[PlotlySelection, list[dict[str, Any]]]):# Override _mime_ to return plotly HTML in non-JS environmentsdef _mime_(self) -> tuple[KnownMimeType, str]:- if is_no_js():+ if is_non_interactive():return ("text/html", self._figure._repr_html_())return ("text/html", self.text)diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.pyindex 57c6996b23..d37b39d7e9 100644--- a/marimo/_plugins/ui/_impl/table.py+++ b/marimo/_plugins/ui/_impl/table.py@@ -21,7 +21,9 @@ from marimo import _loggersfrom marimo._data.models import BinValue, ColumnStats, ValueCountfrom marimo._data.preview_column import get_column_preview_datasetfrom marimo._dependencies.dependencies import DependencyManager+from marimo._messaging.mimetypes import KnownMimeTypefrom marimo._messaging.notification import ColumnPreview+from marimo._output.hypertext import is_non_interactivefrom marimo._output.mime import MIMEfrom marimo._output.rich_help import mddocfrom marimo._plugins.core.web_component import JSONType@@ -1470,20 +1472,17 @@ class table(error=f"Failed to get row IDs: {str(e)}",)- def _repr_markdown_(self) -> str:- """Return a markdown representation of the table.-- Generates a markdown or HTML representation of the table data,- useful for rendering in the GitHub viewer.-- Returns:- str: HTML representation of the table if available,- otherwise string representation.- """- df = self.data- if hasattr(df, "_repr_html_"):- return df._repr_html_() # type: ignore[attr-defined,no-any-return]- return str(df)+ # Override _mime_ to return a plain HTML representation in non-interactive environments+ def _mime_(self) -> tuple[KnownMimeType, str]:+ if is_non_interactive():+ df = self.data+ # Generates a plain HTML representation of the table data,+ # useful for rendering in the GitHub viewer.+ repr_html = getattr(df, "_repr_html_", None)+ if repr_html is not None and callable(repr_html):+ return ("text/html", cast(str, repr_html()))+ return ("text/html", str(df))+ return ("text/html", self.text)@functools.cached_propertydef default_page_size(self) -> int:diff --git a/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt b/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txtindex d1cda28d2c..adcff3ed3c 100644--- a/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt+++ b/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt@@ -146,6 +146,104 @@"execution_count": null,"id": "Xref","metadata": {},+ "outputs": [+ {+ "data": {+ "text/html": [+ "<div>\n",+ "<style scoped>\n",+ " .dataframe tbody tr th:only-of-type {\n",+ " vertical-align: middle;\n",+ " }\n",+ "\n",+ " .dataframe tbody tr th {\n",+ " vertical-align: top;\n",+ " }\n",+ "\n",+ " .dataframe thead th {\n",+ " text-align: right;\n",+ " }\n",+ "</style>\n",+ "<table border=\"1\" class=\"dataframe\">\n",+ " <thead>\n",+ " <tr style=\"text-align: right;\">\n",+ " <th></th>\n",+ " <th>_marimo_row_id</th>\n",+ " <th>x</th>\n",+ " </tr>\n",+ " </thead>\n",+ " <tbody>\n",+ " <tr>\n",+ " <th>0</th>\n",+ " <td>0</td>\n",+ " <td>1</td>\n",+ " </tr>\n",+ " </tbody>\n",+ "</table>\n",+ "</div>"+ ]+ },+ "metadata": {},+ "output_type": "display_data"+ }+ ],+ "source": [+ "mo.ui.table(df)"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "SFPL",+ "metadata": {},+ "outputs": [+ {+ "data": {+ "text/html": [+ "<div>\n",+ "<style scoped>\n",+ " .dataframe tbody tr th:only-of-type {\n",+ " vertical-align: middle;\n",+ " }\n",+ "\n",+ " .dataframe tbody tr th {\n",+ " vertical-align: top;\n",+ " }\n",+ "\n",+ " .dataframe thead th {\n",+ " text-align: right;\n",+ " }\n",+ "</style>\n",+ "<table border=\"1\" class=\"dataframe\">\n",+ " <thead>\n",+ " <tr style=\"text-align: right;\">\n",+ " <th></th>\n",+ " <th>x</th>\n",+ " </tr>\n",+ " </thead>\n",+ " <tbody>\n",+ " <tr>\n",+ " <th>0</th>\n",+ " <td>1</td>\n",+ " </tr>\n",+ " </tbody>\n",+ "</table>\n",+ "</div>"+ ]+ },+ "metadata": {},+ "output_type": "display_data"+ }+ ],+ "source": [+ "mo.ui.dataframe(df)"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "BYtC",+ "metadata": {},"outputs": [{"data": {@@ -163,7 +261,7 @@{"cell_type": "code","execution_count": null,- "id": "SFPL",+ "id": "RGSE","metadata": {"marimo": {"name": "cell_altair"@@ -188,7 +286,7 @@{"cell_type": "code","execution_count": null,- "id": "BYtC",+ "id": "Kclp","metadata": {},"outputs": [{@@ -207,7 +305,7 @@{"cell_type": "code","execution_count": null,- "id": "RGSE",+ "id": "emfo","metadata": {"marimo": {"name": "cell_plotly"@@ -234,7 +332,7 @@{"cell_type": "code","execution_count": null,- "id": "Kclp",+ "id": "Hstk","metadata": {},"outputs": [{diff --git a/tests/fixtures/notebook_with_media.py b/tests/fixtures/notebook_with_media.pyindex 23ad0c8063..76d974943f 100644--- a/tests/fixtures/notebook_with_media.py+++ b/tests/fixtures/notebook_with_media.py@@ -68,6 +68,18 @@ def cell_matplotlib(np, plt):def basic_dataframe(pd):df = pd.DataFrame({"x": [1]})df+ return (df,)+++def _(df, mo):+ mo.ui.table(df)+ return+++def _(df, mo):+ mo.ui.dataframe(df)return