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
feat: Add leafmap formatter (#407)
Myles Scolnick819e9b73331f
diff --git a/examples/third_party/leafmap.py b/examples/third_party/leafmap.pynew file mode 100644index 0000000000..a808391e92--- /dev/null+++ b/examples/third_party/leafmap.py@@ -0,0 +1,45 @@+import marimo++__generated_with = "0.1.59"+app = marimo.App()+++def __(mo):+ mo.md(+ f"""+ # Leafmap + marimo++ To get started, install **[leafmap](https://leafmap.org/)**:++ ```+ pip install leafmap marimo+ ```+ """+ )+ return+++def __(leafmap):+ m = leafmap.Map(center=(40, -100), zoom=4, height="400px")+ m.add_basemap("HYBRID")+ m.add_basemap("Esri.NatGeoWorldMap")+ m.add_tile_layer(+ url="https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",+ name="Google Satellite",+ attribution="Google",+ )+ m+ return m,+++def __():+ import marimo as mo+ import leafmap+ return leafmap, mo+++if __name__ == "__main__":+ app.run()diff --git a/frontend/index.html b/frontend/index.htmlindex 8d74347247..faf994874e 100644--- a/frontend/index.html+++ b/frontend/index.html@@ -15,8 +15,16 @@<script>function __resizeIframe(obj) {+ // Resize the iframe to the height of the contentobj.style.height =obj.contentWindow.document.documentElement.scrollHeight + "px";++ // Resize the iframe when the content changes+ const resizeObserver = new ResizeObserver((entries) => {+ obj.style.height =+ obj.contentWindow.document.documentElement.scrollHeight + "px";+ });+ resizeObserver.observe(obj.contentWindow.document.body);}</script><marimo-filename hidden>{{ filename }}</marimo-filename>diff --git a/marimo/_output/formatters/formatters.py b/marimo/_output/formatters/formatters.pyindex 63f3ab57d2..d52033f9e6 100644--- a/marimo/_output/formatters/formatters.py+++ b/marimo/_output/formatters/formatters.py@@ -6,6 +6,7 @@ from typing import Any, Callable, Sequencefrom marimo._output.formatters.altair_formatters import AltairFormatterfrom marimo._output.formatters.formatter_factory import FormatterFactory+from marimo._output.formatters.leafmap_formatters import LeafmapFormatterfrom marimo._output.formatters.matplotlib_formatters import MatplotlibFormatterfrom marimo._output.formatters.pandas_formatters import PandasFormatterfrom marimo._output.formatters.plotly_formatters import PlotlyFormatter@@ -21,6 +22,7 @@ THIRD_PARTY_FACTORIES: dict[str, FormatterFactory] = {PandasFormatter.package_name(): PandasFormatter(),PlotlyFormatter.package_name(): PlotlyFormatter(),SeabornFormatter.package_name(): SeabornFormatter(),+ LeafmapFormatter.package_name(): LeafmapFormatter(),}# Formatters for builtin types and other things that don't require adiff --git a/marimo/_output/formatters/leafmap_formatters.py b/marimo/_output/formatters/leafmap_formatters.pynew file mode 100644index 0000000000..135554f3af--- /dev/null+++ b/marimo/_output/formatters/leafmap_formatters.py@@ -0,0 +1,36 @@+# Copyright 2023 Marimo. All rights reserved.+from __future__ import annotations++import marimo._output.data.data as mo_data+from marimo._output.formatters.formatter_factory import FormatterFactory+from marimo._output.utils import flatten_string+++class LeafmapFormatter(FormatterFactory):+ @staticmethod+ def package_name() -> str:+ return "leafmap"++ def register(self) -> None:+ import leafmap # type: ignore[import-not-found]++ from marimo._output import formatting++ @formatting.formatter(leafmap.Map)+ def _show_dataframe(lmap: leafmap.Map) -> tuple[str, str]:+ # 540px is the pixel height that makes the map fit in the+ # notebook without scrolling+ height = lmap.layout.height or "540px"+ width = lmap.layout.width or "100%"+ html = mo_data.html(lmap.to_html(width=width, height=height))+ return (+ "text/html",+ (+ flatten_string(+ f"<iframe src='{html.url}'"+ "frameborder='0' scrolling='auto'"+ "style='width: 100%'"+ "onload='__resizeIframe(this)'></iframe>"+ )+ ),+ )