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.

experimental: add config block for experimental, support feature-flags on the FE, add theming (#99)

Myles Scolnick261c3aa75105
diff --git a/frontend/package.json b/frontend/package.json
index 5e43be62bb..f95debf484 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -54,6 +54,7 @@
"@storybook/addon-styling": "^1.3.7",
"@tanstack/react-table": "^8.9.8",
"@textea/json-viewer": "^3.1.1",
+ "@types/js-cookie": "^3.0.3",
"class-variance-authority": "^0.7.0",
"clsx": "^1.2.1",
"cmdk": "^0.2.0",
@@ -65,6 +66,7 @@
"html-react-parser": "^4.2.2",
"html-to-image": "^1.11.11",
"jotai": "^2.4.2",
+ "js-cookie": "^3.0.5",
"katex": "^0.16.8",
"lucide-react": "^0.276.0",
"partysocket": "0.0.4",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index fae94b75ee..c29d5f7e29 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -134,6 +134,9 @@ dependencies:
'@textea/json-viewer':
specifier: ^3.1.1
+ '@types/js-cookie':
+ specifier: ^3.0.3
+ version: 3.0.3
class-variance-authority:
specifier: ^0.7.0
version: 0.7.0
@@ -167,6 +170,9 @@ dependencies:
jotai:
specifier: ^2.4.2
version: 2.4.2(@types/[email protected])([email protected])
+ js-cookie:
+ specifier: ^3.0.5
+ version: 3.0.5
katex:
specifier: ^0.16.8
version: 0.16.8
@@ -5616,6 +5622,10 @@ packages:
pretty-format: 27.5.1
dev: true
+ resolution: {integrity: sha512-Xe7IImK09HP1sv2M/aI+48a20VX+TdRJucfq4vfRVy6nWN8PYPOEnlMRSgxJAgYQIXJVL8dZ4/ilAM7dWNaOww==}
+ dev: false
+
resolution: {integrity: sha512-qC4bCqYGy1y/NP7dDVr7KJarn+PbX1nSpwA7JXdu0HxT3QYjO8MJ+cntENtHFVy2dRAyBV23OZ6MxsW1AM1L8g==}
dev: true
@@ -9640,6 +9650,11 @@ packages:
react: 18.2.0
dev: false
+ resolution: {integrity: sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==}
+ engines: {node: '>=14'}
+ dev: false
+
resolution: {integrity: sha512-X2BB11YZtrRqY4EnQcLX5Rh373zbK4alC1FW7D7MBhL2gtcC17cTnr6DmfHZeS0s2rTHjUTMMHfG7gO8SSdw+g==}
engines: {node: '>=0.10.0'}
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index f782a98398..71f88bd154 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -22,7 +22,7 @@ import { useMarimoWebSocket } from "./core/websocket/useMarimoWebSocket";
import { useCellActions, useCells } from "./core/state/cells";
import { Disconnected } from "./editor/Disconnected";
import { derefNotNull } from "./utils/dereference";
-import { AppConfig, UserConfig } from "./core/config";
+import { AppConfig, UserConfig } from "./core/config/config";
import { toggleAppMode, viewStateAtom } from "./core/mode";
import { useHotkey } from "./hooks/useHotkey";
import { Tooltip } from "./components/ui/tooltip";
diff --git a/frontend/src/components/app-config/app-config-form.tsx b/frontend/src/components/app-config/app-config-form.tsx
index b45ad4d993..75c3dc7040 100644
--- a/frontend/src/components/app-config/app-config-form.tsx
+++ b/frontend/src/components/app-config/app-config-form.tsx
@@ -8,7 +8,7 @@ import {
FormItem,
FormLabel,
} from "@/components/ui/form";
-import { AppConfig, AppConfigSchema } from "../../core/config";
+import { AppConfig, AppConfigSchema } from "../../core/config/config";
import { toast } from "../ui/use-toast";
import { useAppConfig } from "@/core/state/config";
import { Switch } from "@/components/ui/switch";
@@ -36,7 +36,7 @@ export const AppConfigForm: React.FC = () => {
return (
<Form {...form}>
- <form onChange={form.handleSubmit(onSubmit)} className="space-y-4">
+ <form onChange={form.handleSubmit(onSubmit)}>
<FormField
control={form.control}
name="width"
diff --git a/frontend/src/components/app-config/theme-toggle.tsx b/frontend/src/components/app-config/theme-toggle.tsx
index cbc279be81..9c15467cbf 100644
--- a/frontend/src/components/app-config/theme-toggle.tsx
+++ b/frontend/src/components/app-config/theme-toggle.tsx
@@ -1,4 +1,5 @@
/* Copyright 2023 Marimo. All rights reserved. */
+import { getFeatureFlag } from "@/core/config/feature-flag";
import { useTheme } from "../../theme/useTheme";
import { Checkbox } from "../ui/checkbox";
import { FormItem, FormLabel, FormControl } from "../ui/form";
@@ -6,12 +7,12 @@ import { FormItem, FormLabel, FormControl } from "../ui/form";
export const ThemeToggle: React.FC = () => {
const { theme, setTheme } = useTheme();
- if (process.env.NODE_ENV === "production") {
+ if (!getFeatureFlag("theming")) {
return null;
}
return (
- <FormItem className="flex flex-row items-start space-x-2 space-y-0 pt-2">
+ <FormItem className="flex flex-row items-start space-x-2 space-y-0">
<FormControl>
<Checkbox
checked={theme === "dark"}
diff --git a/frontend/src/components/app-config/user-config-form.tsx b/frontend/src/components/app-config/user-config-form.tsx
index 1160d98117..ed47ad97d5 100644
--- a/frontend/src/components/app-config/user-config-form.tsx
+++ b/frontend/src/components/app-config/user-config-form.tsx
@@ -1,6 +1,6 @@
/* Copyright 2023 Marimo. All rights reserved. */
import { zodResolver } from "@hookform/resolvers/zod";
-import { useForm } from "react-hook-form";
+import { DefaultValues, useForm } from "react-hook-form";
import {
Form,
FormControl,
@@ -10,7 +10,7 @@ import {
FormLabel,
FormMessage,
} from "@/components/ui/form";
-import { UserConfig, UserConfigSchema } from "../../core/config";
+import { UserConfig, UserConfigSchema } from "../../core/config/config";
import { Checkbox } from "../ui/checkbox";
import { Input } from "../ui/input";
import { toast } from "../ui/use-toast";
@@ -26,7 +26,7 @@ export const UserConfigForm: React.FC = () => {
// Create form
const form = useForm<UserConfig>({
resolver: zodResolver(UserConfigSchema),
- defaultValues: config,
+ defaultValues: config as DefaultValues<UserConfig>,
});
const onSubmit = async (values: UserConfig) => {
@@ -41,7 +41,10 @@ export const UserConfigForm: React.FC = () => {
return (
<Form {...form}>
- <form onChange={form.handleSubmit(onSubmit)} className="space-y-4">
+ <form
+ onChange={form.handleSubmit(onSubmit)}
+ className="flex flex-col gap-4"
+ >
<FormField
control={form.control}
name="save.autosave"
@@ -130,8 +133,8 @@ export const UserConfigForm: React.FC = () => {
</FormItem>
)}
/>
+ <ThemeToggle />
</form>
- <ThemeToggle />
</Form>
);
};
diff --git a/frontend/src/components/ui/form.tsx b/frontend/src/components/ui/form.tsx
index e8178fdce9..27d82d347c 100644
--- a/frontend/src/components/ui/form.tsx
+++ b/frontend/src/components/ui/form.tsx
@@ -79,7 +79,7 @@ const FormItem = React.forwardRef<
return (
<FormItemContext.Provider value={{ id }}>
- <div ref={ref} className={cn("space-y-2", className)} {...props} />
+ <div ref={ref} className={cn("space-y-1", className)} {...props} />
</FormItemContext.Provider>
);
});
diff --git a/frontend/src/core/codemirror/cm.ts b/frontend/src/core/codemirror/cm.ts
index a0cf29c89c..055c3f0837 100644
--- a/frontend/src/core/codemirror/cm.ts
+++ b/frontend/src/core/codemirror/cm.ts
@@ -45,7 +45,7 @@ import {
import { EditorState, Extension, Prec } from "@codemirror/state";
import { oneDark } from "@codemirror/theme-one-dark";
-import { CompletionConfig, KeymapConfig } from "../config";
+import { CompletionConfig, KeymapConfig } from "../config/config";
import { Theme } from "../../theme/useTheme";
import { completer } from "@/core/codemirror/completion/completer";
diff --git a/frontend/src/core/codemirror/keymaps/keymaps.ts b/frontend/src/core/codemirror/keymaps/keymaps.ts
index b79999e8c5..4dab4a5a69 100644
--- a/frontend/src/core/codemirror/keymaps/keymaps.ts
+++ b/frontend/src/core/codemirror/keymaps/keymaps.ts
@@ -1,5 +1,5 @@
/* Copyright 2023 Marimo. All rights reserved. */
-import { KeymapConfig } from "@/core/config";
+import { KeymapConfig } from "@/core/config/config";
import { logNever } from "@/utils/assertNever";
import { defaultKeymap } from "@codemirror/commands";
import { Extension, Prec } from "@codemirror/state";
diff --git a/frontend/src/core/config.ts b/frontend/src/core/config/config.ts
similarity index 56%
rename from frontend/src/core/config.ts
rename to frontend/src/core/config/config.ts
index 07eb5ccd16..b15861413c 100644
--- a/frontend/src/core/config.ts
+++ b/frontend/src/core/config/config.ts
@@ -1,24 +1,34 @@
/* Copyright 2023 Marimo. All rights reserved. */
import { z } from "zod";
-import { assertExists } from "../utils/assertExists";
+import { assertExists } from "../../utils/assertExists";
-export const UserConfigSchema = z.object({
- completion: z.object({
- activate_on_typing: z.boolean(),
- }),
- save: z.object({
- autosave: z.enum(["off", "after_delay"]).default("after_delay"),
- autosave_delay: z
- .number()
- .nonnegative()
- // Ensure that the delay is at least 1 second
- .transform((millis) => Math.max(millis, 1000))
- .default(1000),
- }),
- keymap: z.object({
- preset: z.enum(["default", "vim"]).default("default"),
- }),
-});
+export const UserConfigSchema = z
+ .object({
+ completion: z.object({
+ activate_on_typing: z.boolean(),
+ }),
+ save: z.object({
+ autosave: z.enum(["off", "after_delay"]).default("after_delay"),
+ autosave_delay: z
+ .number()
+ .nonnegative()
+ // Ensure that the delay is at least 1 second
+ .transform((millis) => Math.max(millis, 1000))
+ .default(1000),
+ }),
+ keymap: z.object({
+ preset: z.enum(["default", "vim"]).default("default"),
+ }),
+ experimental: z
+ .object({
+ theming: z.boolean().optional(),
+ })
+ // Pass through so that we don't remove any extra keys that the user has added.
+ .passthrough()
+ .default({}),
+ })
+ // Pass through so that we don't remove any extra keys that the user has added.
+ .passthrough();
export type UserConfig = z.infer<typeof UserConfigSchema>;
export type SaveConfig = UserConfig["save"];
export type CompletionConfig = UserConfig["completion"];
@@ -29,7 +39,7 @@ export const AppConfigSchema = z.object({
});
export type AppConfig = z.infer<typeof AppConfigSchema>;
-export function getAppConfig() {
+export function parseAppConfig() {
try {
return AppConfigSchema.parse(JSON.parse(getConfig("app")));
} catch (error) {
@@ -39,7 +49,7 @@ export function getAppConfig() {
}
}
-export function getUserConfig() {
+export function parseUserConfig() {
try {
return UserConfigSchema.parse(JSON.parse(getConfig("user")));
} catch (error) {
diff --git a/frontend/src/core/config/feature-flag.ts b/frontend/src/core/config/feature-flag.ts
new file mode 100644
index 0000000000..3dbb94fe1e
--- /dev/null
+++ b/frontend/src/core/config/feature-flag.ts
@@ -0,0 +1,20 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+
+import { getUserConfig } from "../state/config";
+
+export interface ExperimentalFeatures {
+ /**
+ * Allows the user to switch between light and dark themes.
+ */
+ theming: boolean;
+}
+
+const defaultValues: ExperimentalFeatures = {
+ theming: false,
+};
+
+export function getFeatureFlag<T extends keyof ExperimentalFeatures>(
+ feature: T
+): ExperimentalFeatures[T] {
+ return getUserConfig().experimental?.[feature] ?? defaultValues[feature];
+}
diff --git a/frontend/src/core/network/types.ts b/frontend/src/core/network/types.ts
index ab7b984911..6f1155959f 100644
--- a/frontend/src/core/network/types.ts
+++ b/frontend/src/core/network/types.ts
@@ -1,5 +1,5 @@
/* Copyright 2023 Marimo. All rights reserved. */
-import { AppConfig, UserConfig } from "../../core/config";
+import { AppConfig, UserConfig } from "../config/config";
import { CellId } from "../../core/model/ids";
export interface DeleteRequest {
diff --git a/frontend/src/core/saving/useAutoSave.ts b/frontend/src/core/saving/useAutoSave.ts
index 0bdadc92da..9d06d0e719 100644
--- a/frontend/src/core/saving/useAutoSave.ts
+++ b/frontend/src/core/saving/useAutoSave.ts
@@ -1,7 +1,7 @@
/* Copyright 2023 Marimo. All rights reserved. */
import { useEffect, useRef } from "react";
import { ConnectionStatus, WebSocketState } from "../websocket/types";
-import { UserConfig } from "../config";
+import { UserConfig } from "../config/config";
export function useAutoSave(opts: {
codes: string[];
diff --git a/frontend/src/core/state/config.ts b/frontend/src/core/state/config.ts
index 611b4987b4..408baa310f 100644
--- a/frontend/src/core/state/config.ts
+++ b/frontend/src/core/state/config.ts
@@ -1,11 +1,17 @@
/* Copyright 2023 Marimo. All rights reserved. */
import { atom, useAtom } from "jotai";
-import { AppConfig, UserConfig, getAppConfig, getUserConfig } from "../config";
+import {
+ AppConfig,
+ UserConfig,
+ parseAppConfig,
+ parseUserConfig,
+} from "../config/config";
+import { store } from "./jotai";
/**
* Atom for storing the user config.
*/
-const userConfigAtom = atom<UserConfig>(getUserConfig());
+const userConfigAtom = atom<UserConfig>(parseUserConfig());
/**
* Returns the user config.
@@ -14,10 +20,14 @@ export function useUserConfig() {
return useAtom(userConfigAtom);
}
+export function getUserConfig() {
+ return store.get(userConfigAtom);
+}
+
/**
* Atom for storing the app config.
*/
-const appConfigAtom = atom<AppConfig>(getAppConfig());
+const appConfigAtom = atom<AppConfig>(parseAppConfig());
/**
* Returns the app config.
diff --git a/frontend/src/editor/Cell.tsx b/frontend/src/editor/Cell.tsx
index 1eabead265..28d0216713 100644
--- a/frontend/src/editor/Cell.tsx
+++ b/frontend/src/editor/Cell.tsx
@@ -18,7 +18,7 @@ import {
import { sendRun } from "@/core/network/requests";
import { autocompletionKeymap, setupCodeMirror } from "@/core/codemirror/cm";
-import { UserConfig } from "../core/config";
+import { UserConfig } from "../core/config/config";
import { CellState } from "../core/model/cells";
import { CellActions, useCellActions } from "../core/state/cells";
import { derefNotNull } from "../utils/dereference";
diff --git a/frontend/src/editor/cell/cell-actions.tsx b/frontend/src/editor/cell/cell-actions.tsx
index 5cb038069e..4a6e3b749e 100644
--- a/frontend/src/editor/cell/cell-actions.tsx
+++ b/frontend/src/editor/cell/cell-actions.tsx
@@ -1,5 +1,5 @@
/* Copyright 2023 Marimo. All rights reserved. */
-import { PropsWithChildren, useState } from "react";
+import { Fragment, PropsWithChildren, useState } from "react";
import {
Command,
@@ -42,6 +42,7 @@ import { cn } from "@/lib/utils";
import { renderMinimalShortcut } from "@/components/shortcuts/renderShortcut";
import { CellConfig } from "@/core/model/cells";
import { Switch } from "@/components/ui/switch";
+import React from "react";
interface Props {
editorView: EditorView | null;
@@ -240,7 +241,7 @@ export const CellActionsDropdown = ({
/>
<CommandEmpty>No results</CommandEmpty>
{actions.map((group, i) => (
- <>
+ <Fragment key={i}>
<CommandGroup key={i}>
{group.map((action) => {
if (action.hidden) {
@@ -273,7 +274,7 @@ export const CellActionsDropdown = ({
})}
</CommandGroup>
{i < actions.length - 1 && <CommandSeparator />}
- </>
+ </Fragment>
))}
</Command>
</PopoverContent>
diff --git a/frontend/src/editor/renderers/CellArray.tsx b/frontend/src/editor/renderers/CellArray.tsx
index c893e605a2..b41a591da1 100644
--- a/frontend/src/editor/renderers/CellArray.tsx
+++ b/frontend/src/editor/renderers/CellArray.tsx
@@ -6,7 +6,7 @@ import { Cell } from "editor/Cell";
import { RuntimeState } from "../../core/RuntimeState";
import { ConnectionStatus, WebSocketState } from "../../core/websocket/types";
import { CellsAndHistory, useCellActions } from "../../core/state/cells";
-import { AppConfig, UserConfig } from "../../core/config";
+import { AppConfig, UserConfig } from "../../core/config/config";
import { AppMode } from "../../core/mode";
import { useHotkey } from "../../hooks/useHotkey";
import { useEvent } from "../../hooks/useEvent";
diff --git a/frontend/src/stories/cell.stories.tsx b/frontend/src/stories/cell.stories.tsx
index 7a79ff9586..95b9993059 100644
--- a/frontend/src/stories/cell.stories.tsx
+++ b/frontend/src/stories/cell.stories.tsx
@@ -51,6 +51,7 @@ const props: CellProps = {
keymap: {
preset: "default",
},
+ experimental: {},
},
};
diff --git a/frontend/src/theme/useTheme.ts b/frontend/src/theme/useTheme.ts
index ef52beac34..fc1d31dd80 100644
--- a/frontend/src/theme/useTheme.ts
+++ b/frontend/src/theme/useTheme.ts
@@ -1,11 +1,35 @@
/* Copyright 2023 Marimo. All rights reserved. */
import { store } from "@/core/state/jotai";
+import { getFeatureFlag } from "@/core/config/feature-flag";
import { useAtom } from "jotai";
+import Cookies from "js-cookie";
import { atomWithStorage } from "jotai/utils";
+import { SyncStorage } from "jotai/vanilla/utils/atomWithStorage";
export type Theme = "light" | "dark";
-const themeAtom = atomWithStorage<Theme>("marimo:theme", "light");
+const createCookieStorage = <T>(): SyncStorage<T> => {
+ const typed = Cookies.withConverter<T>({});
+ return {
+ getItem: (key: string, initialValue: T) => {
+ return (typed.get(key) as T) ?? initialValue;
+ },
+ setItem: (key: string, value: T) => {
+ typed.set(key, value);
+ },
+ removeItem: (key: string) => {
+ typed.remove(key);
+ },
+ };
+};
+
+const themeAtom = atomWithStorage<Theme>(
+ "marimo:theme",
+ "light",
+ // We use cookies instead of localStorage, so that the theme persists
+ // between different ports on localhost.
+ createCookieStorage<Theme>()
+);
/**
* React hook to manage the theme of the app.
@@ -14,7 +38,7 @@ const themeAtom = atomWithStorage<Theme>("marimo:theme", "light");
export function useTheme() {
const [theme, setTheme] = useAtom(themeAtom);
- if (process.env.NODE_ENV === "development") {
+ if (getFeatureFlag("theming")) {
return {
theme,
setTheme,
@@ -26,7 +50,7 @@ export function useTheme() {
}
export function getTheme(): Theme {
- if (process.env.NODE_ENV === "development") {
+ if (getFeatureFlag("theming")) {
return store.get(themeAtom);
}
return "light";
diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts
index e54058fac3..f39b6fc2db 100644
--- a/frontend/vite.config.ts
+++ b/frontend/vite.config.ts
@@ -24,6 +24,9 @@ const htmlDevPlugin = (): Plugin => {
keymap: {
preset: "default",
},
+ experimental: {
+ theming: true,
+ },
})
)
.replace(`{{ app_config }}`, JSON.stringify({}))
diff --git a/marimo/_config/config.py b/marimo/_config/config.py
index e58112dea4..43105e4147 100644
--- a/marimo/_config/config.py
+++ b/marimo/_config/config.py
@@ -80,11 +80,13 @@ class MarimoConfig(TypedDict, total=False):
- `completion`: a `CompletionConfig`
- `save`: a `SaveConfig`
- `keymap`: a `KeymapConfig`
+ - `experimental`: a `dict` of experimental features
"""
completion: CompletionConfig
save: SaveConfig
keymap: KeymapConfig
+ experimental: dict[str, Any]
DEFAULT_CONFIG: MarimoConfig = {