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.

feat: add mo.iframe, and a-frame example (#2398)

Myles Scolnicka181691d3ebd
diff --git a/docs/api/html.md b/docs/api/html.md
index 67e8f06816..f0e851c452 100644
--- a/docs/api/html.md
+++ b/docs/api/html.md
@@ -12,3 +12,7 @@ All marimo elements extend the HTML element class.
.. autoclasstoc::
```
+
+```{eval-rst}
+.. autofunction:: marimo.iframe
+```
diff --git a/examples/third_party/aframe/aframe_example.py b/examples/third_party/aframe/aframe_example.py
new file mode 100644
index 0000000000..654322a036
--- /dev/null
+++ b/examples/third_party/aframe/aframe_example.py
@@ -0,0 +1,64 @@
+import marimo
+
+__generated_with = "0.8.19"
+app = marimo.App()
+
+
+def __(Aframe, xyz):
+ a = Aframe()
+ a.set_scene(
+ background="color: #ECECEC; fog: type: exponential; color: #AAA; density: 0.05"
+ )
+ a.sky(color="#ECECEC")
+ a.box(
+ position=xyz(-1, 0.5, -3),
+ rotation=xyz(0, 45, 0),
+ color="#4CC3D9",
+ shadow=True,
+ animation="property: rotation; to: 0 405 0; loop: true; dur: 10000",
+ )
+ a.sphere(
+ position=xyz(0, 1.25, -5),
+ radius=1.25,
+ color="#EF2D5E",
+ shadow=True,
+ animation="property: position; to: 0 2.5 -5; dir: alternate; loop: true; dur: 2000; easing: easeInOutSine",
+ )
+ a.cylinder(
+ position=xyz(1, 0.75, -3),
+ radius=0.5,
+ height=1.5,
+ color="#FFC65D",
+ shadow=True,
+ animation="property: rotation; to: 0 360 0; loop: true; dur: 8000",
+ )
+ a.plane(
+ position=xyz(0, 0, -4),
+ rotation=xyz(-90, 0, 0),
+ width=4,
+ height=4,
+ color="#7BC8A4",
+ repeat="4 4",
+ )
+ a.light(type="ambient", color="#445451")
+ a.light(type="point", intensity=0.5, position=xyz(2, 4, -2))
+ a.camera(position=xyz(0, 1.6, 0))
+ return (a,)
+
+
+def __(a, mo):
+ mo.iframe(a.generate())
+ return
+
+
+def __():
+ import marimo as mo
+ from aframe import Aframe, xyz
+ return Aframe, mo, xyz
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/marimo/__init__.py b/marimo/__init__.py
index 4757fbe43d..ee70149d18 100644
--- a/marimo/__init__.py
+++ b/marimo/__init__.py
@@ -38,6 +38,7 @@ __all__ = [
"hstack",
"Html",
"icon",
+ "iframe",
"image",
"lazy",
"left",
@@ -76,7 +77,7 @@ from marimo._ast.app import App
from marimo._ast.cell import Cell
from marimo._islands.island_generator import MarimoIslandGenerator
from marimo._output.doc import doc
-from marimo._output.formatting import as_html, plain
+from marimo._output.formatting import as_html, iframe, plain
from marimo._output.hypertext import Html
from marimo._output.justify import center, left, right
from marimo._output.md import md
diff --git a/marimo/_output/formatting.py b/marimo/_output/formatting.py
index 347f0f6fea..c3584975e0 100644
--- a/marimo/_output/formatting.py
+++ b/marimo/_output/formatting.py
@@ -27,6 +27,8 @@ from typing import Any, Callable, Optional, Tuple, Type, TypeVar, cast
from marimo import _loggers as loggers
from marimo._messaging.mimetypes import KnownMimeType
+from marimo._output.builder import h
+from marimo._output.formatters.utils import src_or_src_doc
from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
from marimo._output.utils import flatten_string
@@ -407,3 +409,40 @@ def _is_callable_method(obj: Any, attr: str) -> bool:
if inspect.isclass(obj) and not isinstance(method, (types.MethodType)):
return False
return callable(method)
+
+
+@mddoc
+def iframe(html: str, *, width: str = "100%", height: str = "400px") -> Html:
+ """
+ Embed an HTML string in an iframe.
+
+ Scripts by default are not executed using `mo.as_html` or `mo.Html`,
+ so if you have a <script/> tag, you can use `mo.iframe` for
+ scripts to be executed.
+
+ You may also want to use this function to display HTML content
+ that may contain styles that could interfere with the rest of the
+ page.
+
+ **Example.**
+
+ ```python
+ html = "<h1>Hello, world!</h1>"
+ mo.iframe(html)
+ ```
+
+ **Args.**
+
+ - `html`: An HTML string
+ """
+
+ return Html(
+ flatten_string(
+ h.iframe(
+ **src_or_src_doc(html),
+ onload="__resizeIframe(this)",
+ width=width,
+ height=height,
+ )
+ ),
+ )