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
improvement: include instructions for how to run static notebooks (#427)
Myles Scolnick1aa70a5f3d23
diff --git a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsxindex 1f8ae697a2..d3c56a34e9 100644--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx@@ -19,7 +19,8 @@ export const VerticalLayoutWrapper: React.FC<PropsWithChildren<Props>> = ({<div className={cn("sm:px-16 md:px-32", className)}><divclassName={cn(- "m-auto pb-12",+ // Large mobile bottom padding due to mobile browser navigation bar+ "m-auto pb-24 sm:pb-12",appConfig.width !== "full" && "max-w-contentWidth min-w-[400px]",// Hide the cells for a fake loading effect, to avoid flickeringinvisible && "invisible"diff --git a/frontend/src/components/static-html/static-banner.tsx b/frontend/src/components/static-html/static-banner.tsxindex 5e2d4c7582..6867a4262e 100644--- a/frontend/src/components/static-html/static-banner.tsx+++ b/frontend/src/components/static-html/static-banner.tsx@@ -1,4 +1,7 @@/* Copyright 2023 Marimo. All rights reserved. */+/* eslint-disable react/jsx-no-comment-textnodes */+/* eslint-disable react/jsx-no-target-blank */+import { isStaticNotebook } from "@/core/static/static-state";import React from "react";import { Button } from "../ui/button";@@ -6,6 +9,14 @@ import { toast } from "../ui/use-toast";import { getMarimoCode } from "@/core/dom/marimo-tag";import { downloadBlob } from "@/utils/download";import { getFilenameFromDOM } from "@/core/dom/htmlUtils";+import {+ DialogHeader,+ Dialog,+ DialogContent,+ DialogDescription,+ DialogTitle,+ DialogTrigger,+} from "../ui/dialog";import { CopyIcon, DownloadIcon } from "lucide-react";export const StaticBanner: React.FC = () => {@@ -14,41 +25,99 @@ export const StaticBanner: React.FC = () => {}return (- <div className="px-4 py-2 bg-[var(--sky-2)] border-b border-[var(--sky-7)] text-md text-[var(--sky-11)] font-semibold flex justify-between items-center">+ <div className="px-4 py-2 bg-[var(--sky-2)] border-b border-[var(--sky-7)] text-md text-[var(--sky-11)] font-semibold flex justify-between items-center gap-4"><span>- This is a static notebook. Some interactive features will not work,- however you can download the code and run it locally.- </span>- <span className="flex flex-wrap justify-end">- <Button- className="ml-2 flex-shrink-0"- variant="secondary"- size="xs"- onClick={() => {- const code = getMarimoCode();- window.navigator.clipboard.writeText(code);- toast({ title: "Copied to clipboard" });- }}- >- <CopyIcon className="w-4 h-4 mr-1" />- Copy code- </Button>- <Button- className="ml-2 flex-shrink-0"- variant="secondary"- size="xs"- onClick={() => {- const code = getMarimoCode();- downloadBlob(- new Blob([code], { type: "text/plain" }),- getFilenameFromDOM() || "app.py"- );- }}+ This is a static Python notebook built using{" "}+ <a+ href="https://github.com/marimo-team/marimo"+ target="_blank"+ className="underline">- <DownloadIcon className="w-4 h-4 mr-1" />- Download code- </Button>+ marimo+ </a>+ .+ <br />+ Some interactive features may not work, however you can download the+ code and run it locally.+ </span>+ <span className="flex-shrink-0">+ <StaticBannerDialog /></span></div>);};++const StaticBannerDialog = () => {+ let filename = getFilenameFromDOM() || "app.py";+ // Trim the path+ const lastSlash = filename.lastIndexOf("/");+ if (lastSlash !== -1) {+ filename = filename.slice(lastSlash + 1);+ }++ const href = window.location.href;++ return (+ <Dialog>+ <DialogTrigger asChild={true}>+ <Button variant="secondary">Run or edit this notebook</Button>+ </DialogTrigger>+ <DialogContent>+ <DialogHeader>+ <DialogTitle>{filename}</DialogTitle>+ <DialogDescription className="pt-4 text-md text-left">+ This is a static notebook built using{" "}+ <a+ href="https://github.com/marimo-team/marimo"+ target="_blank"+ className="text-link hover:underline"+ >+ marimo+ </a>+ . marimo is an open-source next-generation Python notebook.+ <hr className="my-3" />+ In order to edit this notebook, you will need to download the code+ locally, install marimo, and run the following in your terminal:+ <div className="font-mono text-sm bg-[var(--sky-2)] rounded-md my-3 p-2 border border-[var(--sky-7)]">+ pip install marimo+ <br />+ marimo edit {filename}+ </div>+ {!href.endsWith(".html") && (+ <>+ <hr className="my-3" />+ You may also run:+ <div className="font-mono text-sm bg-[var(--sky-2)] rounded-md my-3 p-2 border border-[var(--sky-7)] break-all">+ marimo edit {window.location.href}+ </div>+ </>+ )}+ </DialogDescription>+ </DialogHeader>+ <div className="flex gap-4 flex-wrap">+ <Button+ variant="secondary"+ onClick={() => {+ const code = getMarimoCode();+ window.navigator.clipboard.writeText(code);+ toast({ title: "Copied to clipboard" });+ }}+ >+ <CopyIcon className="w-4 h-4 mr-1" />+ Copy code+ </Button>+ <Button+ variant="secondary"+ onClick={() => {+ const code = getMarimoCode();+ downloadBlob(new Blob([code], { type: "text/plain" }), filename);+ }}+ >+ <DownloadIcon className="w-4 h-4 mr-1" />+ Download code+ </Button>+ </div>+ </DialogContent>+ </Dialog>+ );+};diff --git a/frontend/src/core/static/download-html.ts b/frontend/src/core/static/download-html.tsindex f993d09f93..e61ac1a9da 100644--- a/frontend/src/core/static/download-html.ts+++ b/frontend/src/core/static/download-html.ts@@ -38,11 +38,15 @@ export async function downloadAsHTML(opts: { filename: string }) {throw error;});+ const filenameWithoutPath = filename.split("/").pop() ?? "app.py";+ const filenameWithoutExtension =+ filenameWithoutPath.split(".").shift() ?? "app";+const html = constructHTML({notebookState: notebook,version: version,assetUrl: assetUrl,- filename: filename || "notebook",+ filename: filenameWithoutPath,existingDocument: document,files: await downloadVirtualFiles(),code: codeResponse.contents,@@ -50,7 +54,7 @@ export async function downloadAsHTML(opts: { filename: string }) {downloadBlob(new Blob([html], { type: "text/html" }),- filename ? `${filename}.html` : "notebook.html"+ `${filenameWithoutExtension}.html`);}diff --git a/marimo/_cli/file_path.py b/marimo/_cli/file_path.pyindex 05f81153b2..a8b864cedb 100644--- a/marimo/_cli/file_path.py+++ b/marimo/_cli/file_path.py@@ -15,6 +15,11 @@ import clickfrom marimo._cli.print import greenfrom marimo._utils.url import is_url+STATIC_HTML_CODE_PREFIX = '<marimo-code hidden="">'+STATIC_HTML_CODE_SUFFIX = "</marimo-code>"+STATIC_HTML_FILENAME_PREFIX = '<marimo-filename hidden="">'+STATIC_HTML_FILENAME_SUFFIX = "</marimo-filename>"+def is_github_src(url: str, ext: str) -> bool:if not is_url(url):@@ -59,6 +64,18 @@ def validate_name(temp_dir = TemporaryDirectory()return _handle_github_issue(name, temp_dir), temp_dir+ is_static_notebook, static_notebook_html = _is_static_marimo_notebook_url(+ name+ )+ if is_static_notebook:+ temp_dir = TemporaryDirectory()+ return (+ _create_tmp_file_from_static_notebook(+ static_notebook_html, temp_dir+ ),+ temp_dir,+ )+path = pathlib.Path(name)if path.suffix == ".ipynb":prefix = str(path)[: -len(".ipynb")]@@ -94,6 +111,19 @@ def _is_github_issue_url(url: str) -> bool:)+def _is_static_marimo_notebook_url(url: str) -> tuple[bool, str]:+ if not is_url(url) or not url.endswith(".html"):+ return False, ""+ logging.info("Downloading %s", url)+ request = urllib.request.Request(+ url,+ # User agent to avoid 403 Forbidden some bot protection+ headers={"User-Agent": "Mozilla/5.0"},+ )+ file_contents = urllib.request.urlopen(request).read().decode("utf-8")+ return STATIC_HTML_CODE_PREFIX in file_contents, str(file_contents)++def _handle_github_issue(url: str, temp_dir: TemporaryDirectory[str]) -> str:issue_number = url.split("/")[-1]api_url = f"https://api.github.com/repos/marimo-team/marimo/issues/{issue_number}"@@ -127,6 +157,20 @@ def _handle_github_src(url: str, temp_dir: TemporaryDirectory[str]) -> str:return path_to_app+def _create_tmp_file_from_static_notebook(+ file_contents: str, temp_dir: TemporaryDirectory[str]+) -> str:+ def between(prefix: str, suffix: str) -> str:+ return file_contents.split(prefix)[1].split(suffix)[0]++ code = between(STATIC_HTML_CODE_PREFIX, STATIC_HTML_CODE_SUFFIX)+ filename = between(+ STATIC_HTML_FILENAME_PREFIX, STATIC_HTML_FILENAME_SUFFIX+ )+ decoded_code = urllib.parse.unquote(code).strip()+ return _create_tmp_file_from_content(decoded_code, filename, temp_dir)++def _create_tmp_file_from_url(url: str, temp_dir: TemporaryDirectory[str]) -> str: