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.

Ask for confirmation if file name already exist instead of overriding it (#3987)

xenium95a290be9e30
diff --git a/marimo/_cli/convert/commands.py b/marimo/_cli/convert/commands.py
index da845f688d..a81a667e2b 100644
--- a/marimo/_cli/convert/commands.py
+++ b/marimo/_cli/convert/commands.py
@@ -8,6 +8,7 @@ import click
from marimo._cli.convert.markdown import convert_from_md
from marimo._cli.convert.utils import load_external_file
from marimo._cli.print import echo
+from marimo._cli.utils import prompt_to_overwrite
from marimo._convert.ipynb import convert_from_ipynb
from marimo._utils.paths import maybe_make_dirs
@@ -63,9 +64,11 @@ def convert(
notebook = convert_from_md(text)
if output:
- # Make dirs if needed
- maybe_make_dirs(output)
- Path(output).write_text(notebook, encoding="utf-8")
- echo(f"Converted notebook saved to {output}")
+ output_path = Path(output)
+ if prompt_to_overwrite(output_path):
+ # Make dirs if needed
+ maybe_make_dirs(output)
+ Path(output).write_text(notebook, encoding="utf-8")
+ echo(f"Converted notebook saved to {output}")
else:
echo(notebook)
diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.py
index 68dec110ce..da4e64e560 100644
--- a/marimo/_cli/export/commands.py
+++ b/marimo/_cli/export/commands.py
@@ -9,6 +9,7 @@ import click
from marimo._cli.parse_args import parse_args
from marimo._cli.print import echo, green
+from marimo._cli.utils import prompt_to_overwrite
from marimo._dependencies.dependencies import DependencyManager
from marimo._server.export import (
ExportResult,
@@ -63,6 +64,11 @@ def watch_and_export(
echo(data)
return
+ if output:
+ output_path = Path(output)
+ if not prompt_to_overwrite(output_path):
+ return
+
# No watch, just run once
if not watch:
result = export_callback(marimo_path)
diff --git a/marimo/_cli/utils.py b/marimo/_cli/utils.py
new file mode 100644
index 0000000000..13152b3456
--- /dev/null
+++ b/marimo/_cli/utils.py
@@ -0,0 +1,26 @@
+# Copyright 2024 Marimo. All rights reserved.
+
+from pathlib import Path
+from sys import stdout
+
+import click
+
+from marimo._config.settings import GLOBAL_SETTINGS
+
+
+def prompt_to_overwrite(path: Path) -> bool:
+ if GLOBAL_SETTINGS.YES:
+ return True
+
+ # Check if not in an interactive terminal
+ # default to False
+ if not stdout.isatty():
+ return True
+
+ if path.exists():
+ return click.confirm(
+ f"Warning: The file '{path}' already exists. Overwrite?",
+ default=False,
+ )
+
+ return True