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: mo.carousel and full screen slides (#908)

Myles Scolnick0b6a9063a82d
diff --git a/docs/api/layouts/carousel.md b/docs/api/layouts/carousel.md
new file mode 100644
index 0000000000..92dfe32275
--- /dev/null
+++ b/docs/api/layouts/carousel.md
@@ -0,0 +1,21 @@
+# Carousel
+
+```{eval-rst}
+.. marimo-embed::
+ :size: large
+
+ @app.cell
+ def __():
+ mo.carousel([
+ mo.md("# Introduction"),
+ "By the marimo team",
+ mo.md("## What is marimo?"),
+ mo.md("![marimo moss ball](https://marimo.io/logo.png)"),
+ mo.md("## Questions?"),
+ ])
+ return
+```
+
+```{eval-rst}
+.. autofunction:: marimo.carousel
+```
diff --git a/docs/api/layouts/index.md b/docs/api/layouts/index.md
index 45bf61f895..5568e77935 100644
--- a/docs/api/layouts/index.md
+++ b/docs/api/layouts/index.md
@@ -6,6 +6,7 @@
:hidden:
accordion
+ carousel
callout
justify
stacks
@@ -25,6 +26,7 @@ them but just render their children in a certain way.
:nosignatures:
marimo.accordion
+ marimo.carousel
marimo.callout
marimo.center
marimo.hstack
diff --git a/frontend/package.json b/frontend/package.json
index 9222c8857f..f87f3056d6 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -35,9 +35,11 @@
"@lezer/lr": "^1.4.0",
"@open-rpc/client-js": "^1.8.1",
"@radix-ui/colors": "^3.0.0",
+ "@radix-ui/primitive": "^1.0.1",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-checkbox": "^1.0.4",
+ "@radix-ui/react-compose-refs": "^1.0.1",
"@radix-ui/react-context-menu": "^2.1.5",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
@@ -54,6 +56,7 @@
"@radix-ui/react-toast": "^1.1.5",
"@radix-ui/react-toggle": "^1.0.3",
"@radix-ui/react-tooltip": "^1.0.7",
+ "@radix-ui/react-use-callback-ref": "^1.0.1",
"@radix-ui/react-use-controllable-state": "^1.0.1",
"@react-aria/focus": "^3.16.2",
"@replit/codemirror-vim": "^6.2.0",
@@ -105,6 +108,7 @@
"react-use-event-hook": "^0.9.6",
"react-vega": "^7.6.0",
"reactflow": "^11.10.4",
+ "swiper": "^11.0.7",
"tailwind-merge": "^2.2.1",
"tailwindcss-animate": "^1.0.7",
"timestring": "^7.0.0",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index b668584d41..4a7a843991 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -74,6 +74,9 @@ dependencies:
'@radix-ui/colors':
specifier: ^3.0.0
version: 3.0.0
+ '@radix-ui/primitive':
+ specifier: ^1.0.1
+ version: 1.0.1
'@radix-ui/react-accordion':
specifier: ^1.1.2
@@ -83,6 +86,9 @@ dependencies:
'@radix-ui/react-checkbox':
specifier: ^1.0.4
+ '@radix-ui/react-compose-refs':
+ specifier: ^1.0.1
+ version: 1.0.1(@types/[email protected])([email protected])
'@radix-ui/react-context-menu':
specifier: ^2.1.5
@@ -131,6 +137,9 @@ dependencies:
'@radix-ui/react-tooltip':
specifier: ^1.0.7
+ '@radix-ui/react-use-callback-ref':
+ specifier: ^1.0.1
+ version: 1.0.1(@types/[email protected])([email protected])
'@radix-ui/react-use-controllable-state':
specifier: ^1.0.1
version: 1.0.1(@types/[email protected])([email protected])
@@ -284,6 +293,9 @@ dependencies:
reactflow:
specifier: ^11.10.4
+ swiper:
+ specifier: ^11.0.7
+ version: 11.0.7
tailwind-merge:
specifier: ^2.2.1
version: 2.2.1
@@ -15645,6 +15657,11 @@ packages:
picocolors: 1.0.0
dev: false
+ resolution: {integrity: sha512-cDfglW1B6uSmB6eB6pNmzDTNLmZtu5bWWa1vak0RU7fOI9qHjMzl7gVBvYSl34b0RU2N11HxxETJqQ5LeqI1cA==}
+ engines: {node: '>= 4.7.0'}
+ dev: false
+
resolution: {integrity: sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==}
dev: true
diff --git a/frontend/src/plugins/impl/vega/VegaPlugin.tsx b/frontend/src/plugins/impl/vega/VegaPlugin.tsx
index 614778df6f..29f148c928 100644
--- a/frontend/src/plugins/impl/vega/VegaPlugin.tsx
+++ b/frontend/src/plugins/impl/vega/VegaPlugin.tsx
@@ -21,6 +21,7 @@ import "./vega.css";
import { useTheme } from "@/theme/useTheme";
import { Objects } from "@/utils/objects";
import { resolveVegaSpecData } from "./resolve-data";
+import { Events } from "@/utils/events";
interface Data {
spec: VegaLiteSpec;
@@ -186,14 +187,20 @@ const LoadedVegaComponent = ({
<div className="text-md">{error.stack}</div>
</Alert>
)}
- <VegaLite
- spec={selectableSpec}
- theme={theme === "dark" ? "dark" : undefined}
- actions={actions}
- signalListeners={signalListeners}
- onError={handleError}
- onNewView={handleNewView}
- />
+ <div
+ className="contents"
+ // Capture the pointer down event to prevent the parent from handling it
+ onPointerDown={Events.stopPropagation()}
+ >
+ <VegaLite
+ spec={selectableSpec}
+ theme={theme === "dark" ? "dark" : undefined}
+ actions={actions}
+ signalListeners={signalListeners}
+ onError={handleError}
+ onNewView={handleNewView}
+ />
+ </div>
</>
);
};
diff --git a/frontend/src/plugins/layout/CarouselPlugin.tsx b/frontend/src/plugins/layout/CarouselPlugin.tsx
new file mode 100644
index 0000000000..32ce7137bb
--- /dev/null
+++ b/frontend/src/plugins/layout/CarouselPlugin.tsx
@@ -0,0 +1,125 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import React, { PropsWithChildren, useEffect } from "react";
+import { z } from "zod";
+import { IStatelessPlugin, IStatelessPluginProps } from "../stateless-plugin";
+import { Swiper, SwiperSlide, SwiperRef } from "swiper/react";
+import {
+ Virtual,
+ Keyboard,
+ Pagination,
+ Zoom,
+ Navigation,
+} from "swiper/modules";
+import { Button } from "@/components/ui/button";
+import { cn } from "@/utils/cn";
+import { useEventListener } from "@/hooks/useEventListener";
+
+import "swiper/css";
+import "swiper/css/virtual";
+import "swiper/css/keyboard";
+import "swiper/css/navigation";
+import "swiper/css/pagination";
+import "swiper/css/scrollbar";
+import "./carousel.css";
+
+interface Data {
+ index?: string | null;
+ height?: string | number | null;
+}
+
+export class CarouselPlugin implements IStatelessPlugin<Data> {
+ tagName = "marimo-carousel";
+
+ validator = z.object({
+ index: z.string().nullish(),
+ height: z.union([z.string(), z.number()]).nullish(),
+ });
+
+ render(props: IStatelessPluginProps<Data>): JSX.Element {
+ return (
+ <CarouselComponent {...props.data}>{props.children}</CarouselComponent>
+ );
+ }
+}
+
+type CarouselComponentProps = Data;
+
+const CarouselComponent = ({
+ children,
+ height,
+}: PropsWithChildren<CarouselComponentProps>): JSX.Element => {
+ const el = React.useRef<SwiperRef>(null);
+ const [isFullscreen, setIsFullscreen] = React.useState(false);
+
+ useEventListener(document, "fullscreenchange", () => {
+ setIsFullscreen(!!document.fullscreenElement);
+ });
+
+ useEffect(() => {
+ requestAnimationFrame(() => {
+ window.dispatchEvent(new Event("resize"));
+ });
+ }, [isFullscreen]);
+
+ return (
+ <Swiper
+ ref={el}
+ className="relative w-full border rounded bg-background"
+ spaceBetween={50}
+ style={{
+ height: isFullscreen ? "100%" : height || "550px",
+ }}
+ slidesPerView={1}
+ modules={[Virtual, Keyboard, Pagination, Zoom, Navigation]}
+ zoom={{
+ maxRatio: 5,
+ }}
+ simulateTouch={true}
+ keyboard={{
+ enabled: true,
+ onlyInViewport: true,
+ }}
+ navigation={true}
+ pagination={{
+ clickable: true,
+ }}
+ virtual={true}
+ >
+ {React.Children.map(children, (child, index) => {
+ return (
+ <SwiperSlide key={index}>
+ <div
+ className={cn(
+ "h-full w-full flex items-center justify-center box-border overflow-hidden",
+ isFullscreen ? "p-20" : "p-6",
+ )}
+ >
+ {child}
+ </div>
+ </SwiperSlide>
+ );
+ })}
+ <Button
+ variant="link"
+ size="sm"
+ onClick={async () => {
+ if (!el.current) {
+ return;
+ }
+ const domEl = el.current as unknown as HTMLElement;
+
+ if (document.fullscreenElement) {
+ await document.exitFullscreen();
+ setIsFullscreen(false);
+ } else {
+ await domEl.requestFullscreen();
+ setIsFullscreen(true);
+ }
+ }}
+ className="absolute bottom-0 right-0 z-10 mx-1 mb-0"
+ >
+ {isFullscreen ? "Exit Fullscreen" : "Fullscreen"}
+ </Button>
+ </Swiper>
+ );
+};
diff --git a/frontend/src/plugins/layout/carousel.css b/frontend/src/plugins/layout/carousel.css
new file mode 100644
index 0000000000..5e0f54f2fa
--- /dev/null
+++ b/frontend/src/plugins/layout/carousel.css
@@ -0,0 +1,24 @@
+.swiper-button-prev,
+.swiper-button-next {
+ height: 100%;
+ top: 0;
+ bottom: 0;
+ background-color: rgba(0, 0, 0, 0.1);
+ opacity: 0;
+ padding: 0 15px;
+ transition: opacity 0.3s;
+ font-weight: 900;
+ margin-top: 0;
+
+ --swiper-navigation-sides-offset: 0;
+ --swiper-navigation-size: 20px;
+ --swiper-theme-color: var(--text);
+
+ &.swiper-button-disabled {
+ opacity: 0;
+ }
+
+ &:hover:not(.swiper-button-disabled) {
+ opacity: 1;
+ }
+}
diff --git a/frontend/src/plugins/plugins.ts b/frontend/src/plugins/plugins.ts
index 982eecf506..6846b25783 100644
--- a/frontend/src/plugins/plugins.ts
+++ b/frontend/src/plugins/plugins.ts
@@ -23,6 +23,7 @@ import { AccordionPlugin } from "./layout/AccordionPlugin";
import { CalloutPlugin } from "./layout/CalloutPlugin";
import { JsonOutputPlugin } from "./layout/JsonOutputPlugin";
import { TabsPlugin } from "./impl/TabsPlugin";
+import { CarouselPlugin } from "./layout/CarouselPlugin";
import { TexPlugin } from "./layout/TexPlugin";
import { RefreshPlugin } from "./impl/RefreshPlugin";
import { MicrophonePlugin } from "./impl/MicrophonePlugin";
@@ -67,6 +68,7 @@ export const UI_PLUGINS: Array<IPlugin<any, unknown>> = [
const LAYOUT_PLUGINS: Array<IStatelessPlugin<unknown>> = [
new AccordionPlugin(),
new CalloutPlugin(),
+ new CarouselPlugin(),
new DownloadPlugin(),
new JsonOutputPlugin(),
new ProgressPlugin(),
diff --git a/marimo/__init__.py b/marimo/__init__.py
index 03c04e686c..29ef786223 100644
--- a/marimo/__init__.py
+++ b/marimo/__init__.py
@@ -17,6 +17,7 @@ __all__ = [
"Cell",
"MarimoStopError",
"accordion",
+ "carousel",
"as_html",
"audio",
"callout",
@@ -66,6 +67,7 @@ from marimo._plugins.stateless import mpl, status
from marimo._plugins.stateless.accordion import accordion
from marimo._plugins.stateless.audio import audio
from marimo._plugins.stateless.callout import callout
+from marimo._plugins.stateless.carousel import carousel
from marimo._plugins.stateless.download import download
from marimo._plugins.stateless.flex import hstack, vstack
from marimo._plugins.stateless.icon import icon
diff --git a/marimo/_plugins/stateless/carousel.py b/marimo/_plugins/stateless/carousel.py
new file mode 100644
index 0000000000..97878784fa
--- /dev/null
+++ b/marimo/_plugins/stateless/carousel.py
@@ -0,0 +1,46 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+from typing import Sequence
+
+from marimo._output.formatting import as_html
+from marimo._output.hypertext import Html
+from marimo._output.md import md
+from marimo._output.rich_help import mddoc
+from marimo._plugins.core.web_component import build_stateless_plugin
+
+
+@mddoc
+def carousel(
+ items: Sequence[object],
+) -> Html:
+ """Create a carousel of items.
+
+ **Example.**
+
+ ```python3
+ mo.carousel([mo.md("..."), mo.ui.text_area()])
+ ```
+
+ **Args.**
+
+ - `items`: A list of items.
+
+ **Returns.**
+
+ - An `Html` object.
+ """
+ item_content = "".join(
+ [
+ (md(item).text if isinstance(item, str) else as_html(item).text)
+ for item in items
+ ]
+ )
+
+ return Html(
+ build_stateless_plugin(
+ component_name="marimo-carousel",
+ args={},
+ slotted_html=item_content,
+ )
+ )
diff --git a/marimo/_smoke_tests/carousel.py b/marimo/_smoke_tests/carousel.py
new file mode 100644
index 0000000000..64a3963744
--- /dev/null
+++ b/marimo/_smoke_tests/carousel.py
@@ -0,0 +1,55 @@
+import marimo
+
+__generated_with = "0.2.13"
+app = marimo.App()
+
+
+def __():
+ import marimo as mo
+ import pandas as pd
+ import altair as alt
+ return alt, mo, pd
+
+
+def __(pd):
+ df = pd.read_csv(
+ "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"
+ )
+ return df,
+
+
+def __(df, mo):
+ table = mo.ui.table(df, label="Iris Data in a table")
+ return table,
+
+
+def __(alt, df, mo):
+ chart = mo.ui.altair_chart(
+ alt.Chart(df)
+ .mark_point()
+ .encode(x="sepal_length", y="sepal_width", color="species"),
+ label="Iris Data in chart",
+ )
+ return chart,
+
+
+def __(chart, mo, table):
+ mo.carousel(
+ [
+ mo.md("# A Presentation on Iris Data"),
+ "By the marimo team",
+ table,
+ chart,
+ mo.md("# Thank you!"),
+ ]
+ )
+ return
+
+
+if __name__ == "__main__":
+ app.run()