-
Notifications
You must be signed in to change notification settings - Fork 540
[wip] feat: add CLI args to control UI elements when running notebook as script #4097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
All contributors have signed the CLA ✍️ ✅ |
I have read the CLA Document and I hereby sign the CLA |
test.py
Outdated
|
||
@app.cell | ||
def _(mo): | ||
b = mo.ui.number(cli_name="test") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we've actually talked about adding key
s to UI elements, as it will have other use cases.
another option that you can do today, why not initialize this with mo.ui.number(value=mo.cli_args().get("test"))
?
I like `key`, will use that.
Which other use cases does it have? Didn't find any issue or discussion regarding this yet.
The current `cli_args()` parsing does not support lists as values, which I would need to support cli-defaults for `mo.ui.range_slider`.
Hence my workaround was to have an unparsed version `raw_cli_args()`. However, that does not need to be exposed all the way, could keep it private to `runtime` for example.
…On March 14, 2025 12:17:20 AM GMT+01:00, Myles Scolnick ***@***.***> wrote:
@mscolnick commented on this pull request.
> +
+__generated_with = "0.11.19"
+app = marimo.App(width="medium")
+
+
***@***.***
+def _():
+ import marimo as mo
+
+ mo.raw_cli_args()
+ return (mo,)
+
+
***@***.***
+def _(mo):
+ b = mo.ui.number(cli_name="test")
we've actually talked about adding `key`s to UI elements, as it will have other use cases.
another option that you can do today, why not initialize this with `mo.ui.number(value=mo.cli_args().get("test"))`?
--
Reply to this email directly or view it on GitHub:
#4097 (review)
You are receiving this because you authored the thread.
Message ID: ***@***.***>
|
start proof of concept for triggering file saving from cli through download button
@eckp, CLI args does support lists. |
Example file import marimo as mo
print(mo.cli_args())
print(mo.cli_args().get_all('range_slider')) yields $ python test.py --range_slider 5 6 -v
{'range_slider': '5 6', 'v': ''}
['5 6'] so While we can parse the string into two separate ints before setting them as default value on the user side: print(raw_value := mo.cli_args().get_all("range_slider")[0])
print(parsed_value := [int(v) for v in raw_value.split(" ")])
mo.ui.range_slider(0, 10, value=parsed_value) $ python test.py --range_slider 5 6 -v
5 6
[5, 6] I thought it would be nice to encapsulate this logic directly inside the UI element, and rely on argparse to return the list of values. Additionally, we could use argparse to generate a help message for the cli from the UI element's label, limits, etc. If this sounds useful, I'll proceed with expanding this to other UI elements and adding tests. |
only write to file if not running in notebook to avoid side effects when opening it with UI in browser
to allow the ui elements in the notebook to all add their help texts to the parser, instead of the first ui element's parse_known_args printing only its own help text
You need to use the arg twice to the get the list values. |
TODO: code_editor and file_browser might accept a file path like file? button trigger on_change, but how to track value?
Thanks, that works! I still see value in integrating a cli option in the ui in- and output elements themselves, as it fits the idea of deploying marimo notebooks as a pure python script in a production pipeline, without any boilerplate code on user side. Do you see a way forward for this idea? If so, feedback on the current draft would be very welcome. |
@eckp, by doing it in user code, the user can validate or transform the CLI inputs. also it's readable by the end-user what is going on. i don't there is us much more boiler plate: mo.ui.text(key="input")
# vs
mo.ui.text(value=mo.cli_args().get("input")) Another idea for
|
📝 Summary
WIP: Add parameter to ui element instantiation, to set default value from command line interface argument.
To give control over ui element values when running notebook as script, solving this question.
🔍 Description of Changes
mo.raw_cli_args()
similar to mo.cli_args(), but keep them aslist of str
instead of pre-parsing them into adict
to preserve lists as values (circumventingparse_args
limitations).argparse.ArgumentParser
object inmo.ui._impl.input.py
, to which each ui element can add its argument ifcli_name
is given.📋 Checklist
mo.raw_cli_args()
?📜 Reviewers
@mscolnick