- 
                Notifications
    You must be signed in to change notification settings 
- Fork 749
improvement: fallbacks and warnings for missing features from sandboxed iframes + docs #6883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| # Embedding | ||
|  | ||
| There are various ways to embed marimo notebooks in other web pages, such | ||
| There are various ways to embed marimo notebooks in other web pages, such | ||
| as web documentation, educational platforms, or static sites in general. Here | ||
| are two ways: | ||
| are the main approaches: | ||
|  | ||
| * Host on [GitHub Pages](github_pages.md) or [self-host WASM HTML](self_host_wasm.md), | ||
| and `<iframe>` the published notebook. | ||
|  | @@ -12,3 +12,70 @@ are two ways: | |
|  | ||
| We plan to provide more turn-key solutions for static site generation with | ||
| marimo notebooks in the future. | ||
|  | ||
| ## Iframe Sandbox Configuration | ||
|  | ||
| When embedding marimo notebooks in sandboxed iframes, proper configuration is essential for full functionality. marimo is designed to gracefully degrade when certain features are restricted, but understanding these requirements will help you provide the best experience. | ||
|  | ||
| ### Required Sandbox Attributes | ||
|  | ||
| For marimo to function properly in an iframe, you need this **minimum** sandbox attribute: | ||
|  | ||
| ```html | ||
| <iframe | ||
| src="https://marimo.app/your-notebook" | ||
| sandbox="allow-scripts" | ||
| width="100%" | ||
| height="600" | ||
| ></iframe> | ||
| ``` | ||
|  | ||
| * **`allow-scripts`**: Required for JavaScript execution (essential for marimo to run) | ||
|  | ||
| !!! note "Basic Functionality" | ||
| With only `allow-scripts`, marimo will work but with limitations: WebSocket connections will function, but storage will be in-memory only (state resets on page reload), and clipboard access will use browser prompts instead of the clipboard API. | ||
|  | ||
| ### Recommended Sandbox Attributes | ||
|  | ||
| For the best user experience, include these additional attributes: | ||
|  | ||
| ```html | ||
| <iframe | ||
| src="https://marimo.app/your-notebook" | ||
| sandbox="allow-scripts allow-same-origin allow-downloads allow-popups" | ||
| allow="microphone" | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. webcam too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed for our components | ||
| allowfullscreen | ||
| width="100%" | ||
| height="600" | ||
| ></iframe> | ||
| ``` | ||
|  | ||
| **Additional Attributes:** | ||
|  | ||
| * **`allow-same-origin`**: Enables persistent storage (localStorage) and full clipboard API. Only use this if you trust the content of the iframe or the iframe URL is hosted on a different domain. | ||
| * **`allow-downloads`**: Enables downloading notebook outputs, data exports, and screenshots | ||
| * **`allow-popups`**: Allows opening links and notebooks in new tabs | ||
| * **`allowfullscreen`** (attribute, not sandbox): Enables fullscreen mode for slides and outputs | ||
|  | ||
| **Permission Policy:** | ||
|  | ||
| * **`allow="microphone"`**: Required for `mo.ui.microphone()` widget functionality | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh ok. We could add  | ||
|  | ||
| !!! tip "Security Considerations" | ||
| Only use `allow-same-origin` with trusted content or the iframe URL is hosted on a different domain. Combining `allow-scripts` and `allow-same-origin` allows the iframe to remove the sandbox attribute entirely, making the iframe as powerful as if it weren't sandboxed at all. | ||
|  | ||
| ### Example: Full Configuration | ||
|  | ||
| Here's a complete example with all recommended settings: | ||
|  | ||
| ```html | ||
| <iframe | ||
| src="https://marimo.app/l/your-notebook-id?embed=true&mode=read" | ||
| sandbox="allow-scripts allow-same-origin allow-downloads allow-popups allow-downloads-without-user-activation" | ||
| allow="microphone" | ||
| allowfullscreen | ||
| width="100%" | ||
| height="600" | ||
| style="border: 1px solid #ddd; border-radius: 8px;" | ||
| ></iframe> | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* Copyright 2024 Marimo. All rights reserved. */ | ||
|  | ||
| import { useMemo } from "react"; | ||
| import { | ||
| getIframeCapabilities, | ||
| type IframeCapabilities, | ||
| } from "@/utils/capabilities"; | ||
|  | ||
| /** | ||
| * React hook to access iframe capabilities | ||
| */ | ||
| export function useIframeCapabilities(): IframeCapabilities { | ||
| return useMemo(() => getIframeCapabilities(), []); | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
classic off-by-one error