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.

Make app title configurable (#1264)

fuenfundachtzig5886681eb6df
diff --git a/frontend/src/components/app-config/app-config-form.tsx b/frontend/src/components/app-config/app-config-form.tsx
index fd5b516c67..70ebf759c7 100644
--- a/frontend/src/components/app-config/app-config-form.tsx
+++ b/frontend/src/components/app-config/app-config-form.tsx
@@ -4,6 +4,7 @@ import { useForm } from "react-hook-form";
import {
Form,
FormControl,
+ FormDescription,
FormField,
FormItem,
FormLabel,
@@ -13,7 +14,9 @@ import {
AppConfig,
AppConfigSchema,
APP_WIDTHS,
+ AppTitleSchema,
} from "../../core/config/config-schema";
+import { Input } from "../ui/input";
import { NativeSelect } from "../ui/native-select";
import { useAppConfig } from "@/core/config/config";
import { saveAppConfig } from "@/core/network/requests";
@@ -81,6 +84,34 @@ export const AppConfigForm: React.FC = () => {
</FormItem>
)}
/>
+ <FormField
+ control={form.control}
+ name="app_title"
+ render={({ field }) => (
+ <div className="flex flex-col gap-y-1">
+ <FormItem className="mb-2">
+ <FormLabel>Application title</FormLabel>
+ <FormControl>
+ <Input
+ {...field}
+ value={field.value}
+ onChange={(e) => {
+ field.onChange(e.target.value);
+ if (AppTitleSchema.safeParse(e.target.value).success) {
+ document.title = e.target.value;
+ }
+ }}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ <FormDescription>
+ The application title is put in the title tag in the HTML code
+ and typically displayed in the title bar of the browser window.
+ </FormDescription>
+ </div>
+ )}
+ />
</form>
</Form>
);
diff --git a/frontend/src/core/config/config-schema.ts b/frontend/src/core/config/config-schema.ts
index c141c29573..1311ce1309 100644
--- a/frontend/src/core/config/config-schema.ts
+++ b/frontend/src/core/config/config-schema.ts
@@ -99,9 +99,15 @@ export type CompletionConfig = UserConfig["completion"];
export type KeymapConfig = UserConfig["keymap"];
export const APP_WIDTHS = ["normal", "medium", "full"] as const;
+export const AppTitleSchema = z
+ .string()
+ .regex(new RegExp("^[A-Za-z0-9-_' ]*$"), {
+ message: "Invalid application title",
+ });
export const AppConfigSchema = z
.object({
width: z.enum(APP_WIDTHS).default("normal"),
+ app_title: AppTitleSchema.optional(),
})
.default({ width: "normal" });
export type AppConfig = z.infer<typeof AppConfigSchema>;
diff --git a/marimo/_ast/app.py b/marimo/_ast/app.py
index 9c949e1075..331c63c7fb 100644
--- a/marimo/_ast/app.py
+++ b/marimo/_ast/app.py
@@ -55,6 +55,7 @@ class _AppConfig:
"""
width: Literal["normal", "medium", "full"] = "normal"
+ app_title: Optional[str] = None
# The file path of the layout file, relative to the app file.
layout_file: Optional[str] = None
diff --git a/marimo/_server/templates/templates.py b/marimo/_server/templates/templates.py
index 72b83ef3c3..42ffb40c1b 100644
--- a/marimo/_server/templates/templates.py
+++ b/marimo/_server/templates/templates.py
@@ -48,7 +48,12 @@ def notebook_page_template(
html = html.replace("{{ server_token }}", server_token)
html = html.replace("{{ version }}", __version__)
- html = html.replace("{{ title }}", parse_title(filename))
+ html = html.replace(
+ "{{ title }}",
+ parse_title(filename)
+ if app_config.app_title is None
+ else app_config.app_title,
+ )
html = html.replace("{{ app_config }}", json.dumps(app_config.asdict()))
html = html.replace("{{ filename }}", filename or "")
html = html.replace(
@@ -85,7 +90,12 @@ def static_notebook_template(
html = html.replace("{{ server_token }}", server_token)
html = html.replace("{{ version }}", __version__)
- html = html.replace("{{ title }}", parse_title(filename))
+ html = html.replace(
+ "{{ title }}",
+ parse_title(filename)
+ if app_config.app_title is None
+ else app_config.app_title,
+ )
html = html.replace(
"{{ app_config }}", json.dumps(app_config.asdict(), sort_keys=True)
)
diff --git a/tests/_ast/codegen_data/test_generate_filecontents_empty.py b/tests/_ast/codegen_data/test_generate_filecontents_empty.py
new file mode 100644
index 0000000000..de721e2ad7
--- /dev/null
+++ b/tests/_ast/codegen_data/test_generate_filecontents_empty.py
@@ -0,0 +1,11 @@
+import marimo
+
+__generated_with = "0.4.6"
+app = marimo.App()
+
+
+
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/codegen_data/test_generate_filecontents_empty_with_config.py b/tests/_ast/codegen_data/test_generate_filecontents_empty_with_config.py
new file mode 100644
index 0000000000..627f51c608
--- /dev/null
+++ b/tests/_ast/codegen_data/test_generate_filecontents_empty_with_config.py
@@ -0,0 +1,11 @@
+import marimo
+
+__generated_with = "0.4.6"
+app = marimo.App(width="full", app_title="test_title")
+
+
+
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/test_app.py b/tests/_ast/test_app.py
index dd0df659d0..9368d5bf9a 100644
--- a/tests/_ast/test_app.py
+++ b/tests/_ast/test_app.py
@@ -283,7 +283,11 @@ class TestApp:
@staticmethod
def test_app_config_extra_args_ignored() -> None:
app = App(width="full", fake_config="foo")
- assert app._config.asdict() == {"width": "full", "layout_file": None}
+ assert app._config.asdict() == {
+ "app_title": None,
+ "width": "full",
+ "layout_file": None,
+ }
@staticmethod
def test_cell_config() -> None:
@@ -435,7 +439,11 @@ def test_app_config() -> None:
config = _AppConfig.from_untrusted_dict({"width": "full"})
assert config.width == "full"
assert config.layout_file is None
- assert config.asdict() == {"width": "full", "layout_file": None}
+ assert config.asdict() == {
+ "app_title": None,
+ "width": "full",
+ "layout_file": None,
+ }
def test_app_config_extra_args_ignored() -> None:
@@ -444,7 +452,11 @@ def test_app_config_extra_args_ignored() -> None:
)
assert config.width == "full"
assert config.layout_file is None
- assert config.asdict() == {"width": "full", "layout_file": None}
+ assert config.asdict() == {
+ "app_title": None,
+ "width": "full",
+ "layout_file": None,
+ }
def test_cli_args(tmp_path: pathlib.Path) -> None:
diff --git a/tests/_ast/test_codegen.py b/tests/_ast/test_codegen.py
index 2c997d05a1..5b646d83c5 100644
--- a/tests/_ast/test_codegen.py
+++ b/tests/_ast/test_codegen.py
@@ -8,8 +8,12 @@ from functools import partial
from inspect import cleandoc
from typing import Optional
+import codegen_data.test_main as mod
+import pytest
+
from marimo import __version__
from marimo._ast import codegen, compiler
+from marimo._ast.app import App, InternalApp, _AppConfig
from marimo._ast.cell import CellConfig
compile_cell = partial(compiler.compile_cell, cell_id="0")
@@ -38,21 +42,38 @@ def get_filepath(name: str) -> str:
return os.path.join(DIR_PATH, f"codegen_data/{name}.py")
-def generate_filecontents(
+def wrap_generate_filecontents(
codes: list[str],
names: list[str],
- configs: Optional[list[CellConfig]] = None,
+ cell_configs: Optional[list[CellConfig]] = None,
+ config: Optional[_AppConfig] = None,
) -> str:
- if configs is None:
+ """Wraps codegen.generate_filecontents to make the cell_configs argument optional."""
+ if cell_configs is None:
resolved_configs = [CellConfig() for _ in range(len(codes))]
else:
- resolved_configs = configs
+ resolved_configs = cell_configs
return codegen.generate_filecontents(
- codes, names, cell_configs=resolved_configs
+ codes, names, cell_configs=resolved_configs, config=config
)
class TestGeneration:
+ @staticmethod
+ def test_generate_filecontents_empty() -> None:
+ contents = wrap_generate_filecontents([], [])
+ assert contents == get_expected_filecontents(
+ "test_generate_filecontents_empty"
+ )
+
+ @staticmethod
+ def test_generate_filecontents_empty_with_config() -> None:
+ config = _AppConfig(app_title="test_title", width="full")
+ contents = wrap_generate_filecontents([], [], config=config)
+ assert contents == get_expected_filecontents(
+ "test_generate_filecontents_empty_with_config"
+ )
+
@staticmethod
def test_generate_filecontents() -> None:
cell_one = "import numpy as np"
@@ -62,7 +83,7 @@ class TestGeneration:
cell_five = "# just a comment"
codes = [cell_one, cell_two, cell_three, cell_four, cell_five]
names = ["one", "two", "three", "four", "five"]
- contents = generate_filecontents(codes, names)
+ contents = wrap_generate_filecontents(codes, names)
assert contents == get_expected_filecontents(
"test_generate_filecontents"
)
@@ -74,7 +95,7 @@ class TestGeneration:
cell_three = "async def _():\n await asyncio.sleep(x)"
codes = [cell_one, cell_two, cell_three]
names = ["one", "two", "three"]
- contents = generate_filecontents(codes, names)
+ contents = wrap_generate_filecontents(codes, names)
assert contents == get_expected_filecontents(
"test_generate_filecontents_async"
)
@@ -110,7 +131,7 @@ class TestGeneration:
)
codes = [cell_one, cell_two]
names = ["one", "two"]
- contents = generate_filecontents(codes, names)
+ contents = wrap_generate_filecontents(codes, names)
assert contents == get_expected_filecontents(
"test_generate_filecontents_async_long_signature"
)
@@ -120,7 +141,7 @@ class TestGeneration:
cell_one = "import numpy as np"
codes = [cell_one]
names = ["one"]
- contents = generate_filecontents(codes, names)
+ contents = wrap_generate_filecontents(codes, names)
assert contents == get_expected_filecontents(
"test_generate_filecontents_single_cell"
)
@@ -133,7 +154,7 @@ class TestGeneration:
cell_four = '_ another_error\n_ and """another"""\n\n \\t'
codes = [cell_one, cell_two, cell_three, cell_four]
names = ["one", "two", "__", "__"]
- contents = generate_filecontents(codes, names)
+ contents = wrap_generate_filecontents(codes, names)
assert contents == get_expected_filecontents(
"test_generate_filecontents_with_syntax_error"
)
@@ -180,7 +201,9 @@ class TestGeneration:
+ "i_am_another_very_long_name + "
+ "yet_another_very_long_name"
)
- contents = generate_filecontents([cell_one, cell_two], ["one", "two"])
+ contents = wrap_generate_filecontents(
+ [cell_one, cell_two], ["one", "two"]
+ )
assert contents == get_expected_filecontents("test_long_line_in_main")
@staticmethod
@@ -188,7 +211,7 @@ class TestGeneration:
cell_one = "type"
codes = [cell_one]
names = ["one"]
- contents = generate_filecontents(codes, names)
+ contents = wrap_generate_filecontents(codes, names)
assert contents == get_expected_filecontents(
"test_generate_filecontents_unshadowed_builtin"
)
@@ -199,7 +222,7 @@ class TestGeneration:
cell_two = "type"
codes = [cell_one, cell_two]
names = ["one", "two"]
- contents = generate_filecontents(codes, names)
+ contents = wrap_generate_filecontents(codes, names)
assert contents == get_expected_filecontents(
"test_generate_filecontents_shadowed_builtin"
)
@@ -344,15 +367,27 @@ class TestGetCodes:
]
+def marimo_app() -> App:
+ return mod.app
+
+
class TestApp:
@staticmethod
- def test_run() -> None:
- import codegen_data.test_main as mod
-
- outputs, defs = mod.app.run()
+ def test_run(marimo_app: App) -> None:
+ outputs, defs = marimo_app.run()
assert outputs == (None, "z", None)
assert defs == {"x": 0, "y": 1, "z": 2, "a": 1}
+ @staticmethod
+ def test_app_with_title(marimo_app: App) -> None:
+ """Update title in app config"""
+ NEW_TITLE = "test_title"
+ marimo_internal_app = InternalApp(marimo_app)
+ assert marimo_internal_app.config.app_title is None
+ marimo_internal_app.update_config({"app_title": NEW_TITLE})
+ assert marimo_internal_app.config.app_title == "test_title"
+
class TestToFunctionDef:
def test_tofunctiondef_one_def(self) -> None:
@@ -499,7 +534,7 @@ def test_recover() -> None:
]
names = ["a", "b", "c"]
- expected = generate_filecontents(codes, names)
+ expected = wrap_generate_filecontents(codes, names)
assert recovered == expected
diff --git a/tests/_server/templates/snapshots/export1.txt b/tests/_server/templates/snapshots/export1.txt
index d3ce5a46bc..ac6aea047d 100644
--- a/tests/_server/templates/snapshots/export1.txt
+++ b/tests/_server/templates/snapshots/export1.txt
@@ -57,7 +57,7 @@
<marimo-mode data-mode='read' hidden></marimo-mode>
<marimo-version data-version='0.0.0' hidden></marimo-version>
<marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"preset": "default"}, "package_management": {"manager": "pip"}, "runtime": {"auto_instantiate": true, "auto_reload": "off"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default"}}' hidden></marimo-user-config>
- <marimo-app-config data-config='{"layout_file": null, "width": "normal"}' hidden></marimo-app-config>
+ <marimo-app-config data-config='{"app_title": null, "layout_file": null, "width": "normal"}' hidden></marimo-app-config>
<marimo-server-token data-token='token' hidden></marimo-server-token>
<title>notebook</title>
<script type="module" crossorigin crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/@marimo-team/[email protected]/dist/assets/index.js""></script>
diff --git a/tests/_server/templates/snapshots/export2.txt b/tests/_server/templates/snapshots/export2.txt
index df6f09a459..9fe1b34ad9 100644
--- a/tests/_server/templates/snapshots/export2.txt
+++ b/tests/_server/templates/snapshots/export2.txt
@@ -57,7 +57,7 @@
<marimo-mode data-mode='read' hidden></marimo-mode>
<marimo-version data-version='0.0.0' hidden></marimo-version>
<marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"preset": "default"}, "package_management": {"manager": "pip"}, "runtime": {"auto_instantiate": true, "auto_reload": "off"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default"}}' hidden></marimo-user-config>
- <marimo-app-config data-config='{"layout_file": null, "width": "normal"}' hidden></marimo-app-config>
+ <marimo-app-config data-config='{"app_title": null, "layout_file": null, "width": "normal"}' hidden></marimo-app-config>
<marimo-server-token data-token='token' hidden></marimo-server-token>
<title>marimo</title>
<script type="module" crossorigin crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/@marimo-team/[email protected]/dist/assets/index.js""></script>
diff --git a/tests/_server/templates/snapshots/export3.txt b/tests/_server/templates/snapshots/export3.txt
index 6abdb270b2..a8bc94c78f 100644
--- a/tests/_server/templates/snapshots/export3.txt
+++ b/tests/_server/templates/snapshots/export3.txt
@@ -57,7 +57,7 @@
<marimo-mode data-mode='read' hidden></marimo-mode>
<marimo-version data-version='0.0.0' hidden></marimo-version>
<marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"preset": "default"}, "package_management": {"manager": "pip"}, "runtime": {"auto_instantiate": true, "auto_reload": "off"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default"}}' hidden></marimo-user-config>
- <marimo-app-config data-config='{"layout_file": null, "width": "normal"}' hidden></marimo-app-config>
+ <marimo-app-config data-config='{"app_title": null, "layout_file": null, "width": "normal"}' hidden></marimo-app-config>
<marimo-server-token data-token='token' hidden></marimo-server-token>
<title>notebook</title>
<script type="module" crossorigin crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/@marimo-team/[email protected]/dist/assets/index.js""></script>