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
docs: cli arguments (#4304)
Akshay Agrawalcd3ddbf898b0
diff --git a/docs/guides/scripts.md b/docs/guides/scripts.mdindex e0c9bf3bfd..5fa748dbb9 100644--- a/docs/guides/scripts.md+++ b/docs/guides/scripts.md@@ -11,8 +11,6 @@ Running a notebook as a script is useful when your notebook has side-effects,like writing to disk. Print statements and other console outputs will showup in your terminal.-You can pass arguments to your notebook at the command-line: see-the [docs page on CLI args](../api/cli_args.md) to learn more.!!! note "Producing notebook outputs"@@ -21,3 +19,41 @@ the [docs page on CLI args](../api/cli_args.md) to learn more.```bashmarimo export html notebook.py -o notebook.html```++ Separate CLI args for your notebook with two dashes:++ ```bash+ marimo export html notebook.py -o notebook.html -- -arg value+ ```++## Command-line arguments++When run as a script, you can access your notebook's command-line arguments+through `sys.argv`, just like any other Python program. This also+means you can declare your notebook's command-line arguments using Python+libraries like [`argparse`](https://docs.python.org/3/library/argparse.html)+and [`simple-parsing`](https://github.com/lebrice/SimpleParsing).++These examples shows how to conditionally assign values to variables based on+command-line arguments when running as a script, and use default values when+running as a notebook.++### argparse++/// marimo-embed-file+ filepath: examples/running_as_a_script/with_argparse.py+///++### simpler-parsing++/// marimo-embed-file+ filepath: examples/running_as_a_script/with_simple_parsing.py+///+++### Parsing extra command-line arguments passed to marimo edit++You can also parametrize `marimo edit` and `marimo run` sessions with command-line+arguments; however, tools like argparse don't work out of the box, since they are+expecting the first argument to be the notebook executable. For these cases,+see the [docs page on CLI args](../api/cli_args.md) to learn more.diff --git a/examples/running_as_a_script/with_argparse.py b/examples/running_as_a_script/with_argparse.pynew file mode 100644index 0000000000..67bb097daf--- /dev/null+++ b/examples/running_as_a_script/with_argparse.py@@ -0,0 +1,55 @@+import marimo++__generated_with = "0.11.31"+app = marimo.App(width="medium")+++def _():+ import marimo as mo+ return (mo,)+++def _():+ import argparse+ return (argparse,)+++def _(argparse):+ parser = argparse.ArgumentParser(+ description="This notebook shows how to use argparse with marimo",+ )++ parser.add_argument("filename")+ parser.add_argument("-c", "--count", type=int)+ parser.add_argument("-v", "--verbose", action="store_true")+ return (parser,)+++def _(mo, parser):+ def parse_args():+ if mo.running_in_notebook():+ # set default values for the command-line arguments when running as a notebook+ filename = "your default value"+ count = 42+ verbose = True+ else:+ args = parser.parse_args()+ filename = args.filename+ count = args.count+ verbose = args.verbose+ return filename, count, verbose+ return (parse_args,)+++def _(parse_args):+ print(parse_args())+ return+++if __name__ == "__main__":+ app.run()diff --git a/examples/running_as_a_script/with_simple_parsing.py b/examples/running_as_a_script/with_simple_parsing.pynew file mode 100644index 0000000000..7d7cd6cfe6--- /dev/null+++ b/examples/running_as_a_script/with_simple_parsing.py@@ -0,0 +1,69 @@+# /// script+# requires-python = ">=3.12"+# dependencies = [+# "marimo",+# "simple-parsing==0.1.7",+# ]+# ///++import marimo++__generated_with = "0.11.31"+app = marimo.App(width="medium")+++def _():+ import marimo as mo+ return (mo,)+++def _():+ import simple_parsing+ return (simple_parsing,)+++def _():+ from dataclasses import dataclass+ from simple_parsing import ArgumentParser++ parser = ArgumentParser()+ parser.add_argument("--foo", type=int, default=123, help="foo help")+++ @dataclass+ class Options:+ """Help string for this group of command-line arguments."""++ log_dir: str # Help string for a required str argument+ learning_rate: float = 1e-4 # Help string for a float argument++ parser.add_arguments(Options, dest="options")+ return ArgumentParser, Options, dataclass, parser+++def _(Options, mo, parser):+ from dataclasses import fields++ def parse_args():+ if mo.running_in_notebook():+ # set default values for the command-line arguments when running as a notebook+ return "foo default", Options("logs/", 1e-4)+ else:+ args = parser.parse_args()+ return args.foo, args.options+ return fields, parse_args+++def _(parse_args):+ foo, options = parse_args()+ print(foo, options)+ return foo, options+++if __name__ == "__main__":+ app.run()