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: mo.video (#234)
Myles Scolnick6c62bbcabbb3
diff --git a/docs/api/media/index.md b/docs/api/media/index.mdindex 07ac90e0d3..70eb3b6707 100644--- a/docs/api/media/index.md+++ b/docs/api/media/index.md@@ -7,6 +7,7 @@imageaudio+ videodownloadplain_text@@ -20,6 +21,7 @@ Use these functions to embed media in your outputs.marimo.imagemarimo.audio+ marimo.videomarimo.pdfmarimo.downloadmarimo.plain_textdiff --git a/docs/api/media/video.md b/docs/api/media/video.mdnew file mode 100644index 0000000000..78e78a2709--- /dev/null+++ b/docs/api/media/video.md@@ -0,0 +1,7 @@+# Video++<!-- <iframe class="demo medium" src="https://components.marimo.io/?component=video" frameborder="no"></iframe> -->++```{eval-rst}+.. autofunction:: marimo.video+```diff --git a/marimo/__init__.py b/marimo/__init__.pyindex 66a52ea8fe..ce61e9c0f8 100644--- a/marimo/__init__.py+++ b/marimo/__init__.py@@ -46,6 +46,7 @@ __all__ = ["tabs","tree","ui",+ "video","vstack",]__version__ = "0.1.33"@@ -70,6 +71,7 @@ from marimo._plugins.stateless.stat import statfrom marimo._plugins.stateless.style import stylefrom marimo._plugins.stateless.tabs import tabsfrom marimo._plugins.stateless.tree import tree+from marimo._plugins.stateless.video import videofrom marimo._runtime import outputfrom marimo._runtime.capture import (capture_stderr,diff --git a/marimo/_output/builder.py b/marimo/_output/builder.pyindex 4bd11998ae..225a81e843 100644--- a/marimo/_output/builder.py+++ b/marimo/_output/builder.py@@ -41,6 +41,34 @@ class _HTMLBuilder:else:return f"<img {_join_params(params)} />"+ @staticmethod+ def video(+ src: Optional[str] = None,+ controls: bool = True,+ muted: bool = False,+ autoplay: bool = False,+ loop: bool = False,+ style: Optional[str] = None,+ ) -> str:+ params: List[Tuple[str, str]] = []+ if src:+ params.append(("src", src))+ if controls:+ params.append(("controls", ""))+ if style:+ params.append(("style", style))+ if muted:+ params.append(("muted", ""))+ if autoplay:+ params.append(("autoplay", ""))+ if loop:+ params.append(("loop", ""))++ if len(params) == 0:+ return "<video />"+ else:+ return f"<video {_join_params(params)} />"+@staticmethoddef audio(src: Optional[str] = None,diff --git a/marimo/_output/test_builder.py b/marimo/_output/test_builder.pyindex 305a336e8f..d23995d20c 100644--- a/marimo/_output/test_builder.py+++ b/marimo/_output/test_builder.py@@ -16,6 +16,13 @@ def test_img() -> None:)+def test_video() -> None:+ assert h.video() == "<video controls />"+ assert (+ h.video(src="video.mp4", controls=False) == "<video src='video.mp4' />"+ )++def test_audio() -> None:assert h.audio() == "<audio controls />"assert (diff --git a/marimo/_plugins/stateless/video.py b/marimo/_plugins/stateless/video.pynew file mode 100644index 0000000000..274aa7fc0f--- /dev/null+++ b/marimo/_plugins/stateless/video.py@@ -0,0 +1,74 @@+# Copyright 2023 Marimo. All rights reserved.+from __future__ import annotations++import io+from typing import Optional, Union++from marimo._output.builder import h+from marimo._output.hypertext import Html+from marimo._output.rich_help import mddoc+from marimo._output.utils import create_style+from marimo._plugins.core.media import io_to_data_url+++@mddoc+def video(+ src: Union[str, bytes, io.BytesIO, io.BufferedReader],+ controls: bool = True,+ muted: bool = False,+ autoplay: bool = False,+ loop: bool = False,+ width: Optional[int] = None,+ height: Optional[int] = None,+ rounded: bool = False,+) -> Html:+ """Render an video as HTML.++ **Example.**++ ```python3+ mo.video(+ src="https://v3.cdnpk.net/videvo_files/video/free/2013-08/large_watermarked/hd0992_preview.mp4",+ controls=False,+ )+ ```++ **Args.**++ - `src`: the URL of the video or a file-like object+ - `controls`: whether to show the controls+ - `muted`: whether to mute the video+ - `autoplay`: whether to autoplay the video.+ the video will only autoplay if `muted` is `True`+ - `loop`: whether to loop the video+ - `width`: the width of the video+ - `height`: the height of the video+ - `rounded`: whether to round the corners of the video++ **Returns.**++ `Html` object+ """+ # Convert to bytes right away since can only be read once+ if isinstance(src, io.BufferedReader):+ src.seek(0)+ src = src.read()++ resolved_src = io_to_data_url(src, fallback_mime_type="video/mp4")+ styles = create_style(+ {+ "width": f"{width}px" if width is not None else None,+ "height": f"{height}px" if height is not None else None,+ "border-radius": "4px" if rounded else None,+ }+ )+ return Html(+ h.video(+ src=resolved_src,+ controls=controls,+ style=styles,+ muted=muted,+ autoplay=autoplay,+ loop=loop,+ )+ )diff --git a/marimo/_smoke_tests/media.py b/marimo/_smoke_tests/media.pyindex 5fd18e51bd..b546635856 100644--- a/marimo/_smoke_tests/media.py+++ b/marimo/_smoke_tests/media.py@@ -1,7 +1,7 @@# Copyright 2023 Marimo. All rights reserved.import marimo-__generated_with = "0.1.22"+__generated_with = "0.1.33"app = marimo.App()@@ -90,5 +90,27 @@ def __(mo):return f, os+def __(mo):+ mo.video(+ src="https://v3.cdnpk.net/videvo_files/video/free/2013-08/large_watermarked/hd0992_preview.mp4",+ rounded=True,+ )+ return+++def __(mo):+ mo.video(+ src="https://v3.cdnpk.net/videvo_files/video/free/2013-08/large_watermarked/hd0992_preview.mp4",+ rounded=True,+ autoplay=True,+ muted=True,+ controls=False,+ width=300,+ )+ return++if __name__ == "__main__":app.run()