Skip to content

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.

fix: leafmap.folium.Map formatting (#1243)

Myles Scolnick0b23b779ee0a
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 813856146a..8b10388937 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -27,7 +27,9 @@ To build marimo from source, you'll need to have Node.js, pnpm, GNU make, and
Python (>=3.8) installed.
- Install [Node.js](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm#using-a-node-version-manager-to-install-nodejs-and-npm)
-- Install [pnpm](https://github.com/pnpm/pnpm) with `npm install -g pnpm`
+- Install [pnpm](https://github.com/pnpm/pnpm) with `npm install -g pnpm@8`
+ - We use version 8 of pnpm. If you have a different version installed, you can
+ install version 8 with `npm install -g pnpm@8`
- Install [GNU Make](https://www.gnu.org/software/make/) (you may already have it installed)
- Install [Python](https://www.python.org/). (You may already it installed. To see your version, use
`python -v` at the command line.)
diff --git a/frontend/src/plugins/core/RenderHTML.tsx b/frontend/src/plugins/core/RenderHTML.tsx
index 3d58b22fd0..5035bac381 100644
--- a/frontend/src/plugins/core/RenderHTML.tsx
+++ b/frontend/src/plugins/core/RenderHTML.tsx
@@ -22,6 +22,12 @@ export const renderHTML = ({ html }: Options) => {
) {
const element = document.createElement("iframe");
Object.entries(domNode.attribs).forEach(([key, value]) => {
+ // If it is wrapped in quotes, remove them
+ // html-react-parser will return quoted keys if they are
+ // valueless attributes (e.g. "allowfullscreen")
+ if (key.startsWith('"') && key.endsWith('"')) {
+ key = key.slice(1, -1);
+ }
element.setAttribute(key, value);
});
return <div dangerouslySetInnerHTML={{ __html: element.outerHTML }} />;
diff --git a/marimo/_output/formatters/leafmap_formatters.py b/marimo/_output/formatters/leafmap_formatters.py
index 654e1335f9..84e3ba8f42 100644
--- a/marimo/_output/formatters/leafmap_formatters.py
+++ b/marimo/_output/formatters/leafmap_formatters.py
@@ -1,8 +1,11 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
+from typing import Any, cast
+
import marimo._output.data.data as mo_data
from marimo._messaging.mimetypes import KnownMimeType
+from marimo._output.builder import h
from marimo._output.formatters.formatter_factory import FormatterFactory
from marimo._output.utils import flatten_string
@@ -17,8 +20,32 @@ class LeafmapFormatter(FormatterFactory):
from marimo._output import formatting
+ @formatting.formatter(leafmap.folium.Map)
+ def _show_folium_map(
+ fmap: leafmap.folium.Map,
+ ) -> tuple[KnownMimeType, str]:
+ # leafmap.folium.Map has a _repr_html_, which we have
+ # another custom formatter for, but this wraps the map in an
+ # additional iframe which can cause weird layout issues
+ html = mo_data.html(cast(Any, fmap).to_html())
+ return (
+ "text/html",
+ flatten_string(
+ h.iframe(
+ src=html.url,
+ scrolling="auto",
+ frameborder="0",
+ width="100%",
+ style="min-height: 540px",
+ onload="__resizeIframe(this)",
+ )
+ ),
+ )
+
@formatting.formatter(leafmap.Map)
- def _show_map(lmap: leafmap.Map) -> tuple[KnownMimeType, str]:
+ def _show_map(
+ lmap: leafmap.Map,
+ ) -> tuple[KnownMimeType, str]:
# 540px is the pixel height that makes the map fit in the
# notebook without scrolling
height = lmap.layout.height or "540px"
@@ -28,10 +55,13 @@ class LeafmapFormatter(FormatterFactory):
"text/html",
(
flatten_string(
- f"<iframe src='{html.url}'"
- "frameborder='0' scrolling='auto'"
- "style='width: 100%'"
- "onload='__resizeIframe(this)'></iframe>"
+ h.iframe(
+ src=html.url,
+ scrolling="auto",
+ frameborder="0",
+ width="100%",
+ onload="__resizeIframe(this)",
+ )
)
),
)
diff --git a/marimo/_smoke_tests/bugs/1241.py b/marimo/_smoke_tests/bugs/1241.py
new file mode 100644
index 0000000000..f1124e48ce
--- /dev/null
+++ b/marimo/_smoke_tests/bugs/1241.py
@@ -0,0 +1,29 @@
+import marimo
+
+__generated_with = "0.4.5"
+app = marimo.App()
+
+
+def __():
+ import marimo as mo
+ import leafmap.foliumap as leafmap
+
+ m = leafmap.Map(center=(40, -100), zoom=4)
+ return leafmap, m, mo
+
+
+def __(m):
+ m # Using our custom formatter
+ return
+
+
+def __(m, mo):
+ mo.Html(m._repr_html_()) # Using the built-in ipython formatter
+ return
+
+
+if __name__ == "__main__":
+ app.run()