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: enable toggling to md mode automatically via mod-shift-m (#1021)
Riya Sinha81bdddb1e72b
diff --git a/frontend/src/components/editor/actions/useCellActionButton.tsx b/frontend/src/components/editor/actions/useCellActionButton.tsxindex db167703c2..277148e889 100644--- a/frontend/src/components/editor/actions/useCellActionButton.tsx+++ b/frontend/src/components/editor/actions/useCellActionButton.tsx@@ -2,7 +2,12 @@import { sendDeleteCell } from "@/core/network/requests";import { downloadCellOutput } from "@/components/export/export-output-button";import { Switch } from "@/components/ui/switch";-import { formatEditorViews } from "@/core/codemirror/format";+import {+ canToggleMarkdown,+ formatEditorViews,+ getEditorViewMode,+ toggleMarkdown,+} from "@/core/codemirror/format";import { useCellActions } from "@/core/cells/cells";import {ImageIcon,@@ -39,6 +44,7 @@ import {DialogHeader,} from "@/components/ui/dialog";import { Label } from "@/components/ui/label";+import { MarkdownIcon, PythonIcon } from "../cell/code/icons";export interface CellActionButtonPropsextends Pick<CellData, "name" | "config"> {@@ -178,6 +184,28 @@ export function useCellActionButtons({ cell }: Props) {formatEditorViews({ [cellId]: editorView }, updateCellCode);},},+ {+ icon:+ getEditorViewMode(editorView) === "python" ? (+ <MarkdownIcon />+ ) : (+ <PythonIcon />+ ),+ label:+ getEditorViewMode(editorView) === "python"+ ? "View as Markdown"+ : "View as Python",+ hotkey: "cell.viewAsMarkdown",+ hidden:+ !canToggleMarkdown(editorView) &&+ getEditorViewMode(editorView) !== "markdown",+ handle: () => {+ if (!editorView) {+ return;+ }+ toggleMarkdown(cellId, editorView, updateCellCode);+ },+ },{icon: config.hide_code ? (<EyeIcon size={13} strokeWidth={1.5} />diff --git a/frontend/src/core/codemirror/extensions.ts b/frontend/src/core/codemirror/extensions.tsindex 3b9c1fcf86..b7a5ec2c17 100644--- a/frontend/src/core/codemirror/extensions.ts+++ b/frontend/src/core/codemirror/extensions.ts@@ -1,7 +1,7 @@/* Copyright 2024 Marimo. All rights reserved. */import { EditorView, keymap, placeholder } from "@codemirror/view";import { CellId } from "../cells/ids";-import { formatEditorViews } from "./format";+import { formatEditorViews, toggleMarkdown } from "./format";import { smartScrollIntoView } from "../../utils/scroll";import { HOTKEYS } from "@/core/hotkeys/hotkeys";import { CellActions } from "../cells/cells";@@ -67,6 +67,14 @@ export function formatKeymapExtension(return true;},},+ {+ key: HOTKEYS.getHotkey("cell.viewAsMarkdown").key,+ preventDefault: true,+ run: (ev) => {+ toggleMarkdown(cellId, ev, updateCellCode);+ return true;+ },+ },]);}diff --git a/frontend/src/core/codemirror/format.ts b/frontend/src/core/codemirror/format.tsindex 31c3553695..0f3b12468c 100644--- a/frontend/src/core/codemirror/format.ts+++ b/frontend/src/core/codemirror/format.ts@@ -14,6 +14,12 @@ import {} from "./language/utils";import { StateEffect } from "@codemirror/state";import { getUserConfig } from "../config/config";+import {+ LanguageAdapters,+ languageAdapterState,+ switchLanguage,+} from "./language/extension";+import { LanguageAdapter } from "./language/types";export const formattingChangeEffect = StateEffect.define<boolean>();@@ -58,3 +64,49 @@ export function formatAll(updateCellCode: CellActions["updateCellCode"]) {const views = notebookCellEditorViews(getNotebook());return formatEditorViews(views, updateCellCode);}++export function getEditorViewMode(+ editorView: EditorView | null,+): LanguageAdapter["type"] {+ if (!editorView) {+ return "python";+ }+ return editorView.state.field(languageAdapterState).type;+}++export function canToggleMarkdown(editorView: EditorView | null) {+ if (!editorView || getEditorViewMode(editorView) === "markdown") {+ return false;+ }+ return (+ LanguageAdapters.markdown().isSupported(+ getEditorCodeAsPython(editorView),+ ) || getEditorCodeAsPython(editorView).trim() === ""+ );+}++export function toggleMarkdown(+ cellId: CellId,+ editorView: EditorView,+ updateCellCode: CellActions["updateCellCode"],+) {+ // If already in markdown mode, switch to python+ if (getEditorViewMode(editorView) === "markdown") {+ switchLanguage(editorView, "python");+ return;+ }++ if (!canToggleMarkdown(editorView)) {+ return;+ }+ if (getEditorCodeAsPython(editorView).trim() === "") {+ const blankMd = 'mo.md(rf"")';+ updateCellCode({+ cellId,+ code: blankMd,+ formattingChange: true,+ });+ updateEditorCodeFromPython(editorView, blankMd);+ }+ switchLanguage(editorView, "markdown");+}diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.tsindex a1eb1c222b..2cf214cfc2 100644--- a/frontend/src/core/hotkeys/hotkeys.ts+++ b/frontend/src/core/hotkeys/hotkeys.ts@@ -104,6 +104,11 @@ const DEFAULT_HOT_KEY = {group: "Editing",key: "Mod-b",},+ "cell.viewAsMarkdown": {+ name: "View as Markdown",+ group: "Editing",+ key: "Mod-Shift-m",+ },"cell.complete": {name: "Code completion",group: "Editing",