Skip to content

[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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

eckp
Copy link

@eckp eckp commented Mar 13, 2025

📝 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

  1. Expose mo.raw_cli_args() similar to mo.cli_args(), but keep them as list of str instead of pre-parsing them into a dict to preserve lists as values (circumventing parse_args limitations).
  2. Create a global argparse.ArgumentParser object in mo.ui._impl.input.py, to which each ui element can add its argument if cli_name is given.
  3. If the argument has a value, update the default ui element value.

📋 Checklist

  • I have read the contributor guidelines.
  • For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on Discord, or the community discussions (Please provide a link if applicable).
    • Applicable to mo.raw_cli_args()?
  • I have added tests for the changes made.
  • I have run the code and verified that it works as expected.

📜 Reviewers

@mscolnick

Copy link

vercel bot commented Mar 13, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
marimo-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 16, 2025 9:50pm
marimo-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 16, 2025 9:50pm

Copy link

github-actions bot commented Mar 13, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@eckp
Copy link
Author

eckp commented Mar 13, 2025

I have read the CLA Document and I hereby sign the CLA

@eckp eckp changed the title Fill UI default values with CLI args [wip] feat: fill UI default values with CLI args Mar 13, 2025
test.py Outdated

@app.cell
def _(mo):
b = mo.ui.number(cli_name="test")
Copy link
Contributor

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 keys 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"))?

@eckp
Copy link
Author

eckp commented Mar 14, 2025 via email

start proof of concept for triggering file saving from cli through download button
@mscolnick
Copy link
Contributor

@eckp, CLI args does support lists. mo.cli_args().get_all("key"). if you find it is broken, can you try to come up with a test case / repro?

@eckp
Copy link
Author

eckp commented Mar 14, 2025

Example file test.py:

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 cli_args() does not support lists as values (as indicated here).

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.

eckp added 2 commits March 16, 2025 13:11
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
@mscolnick
Copy link
Contributor

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?
@eckp
Copy link
Author

eckp commented Mar 16, 2025

You need to use the arg twice to the get the list values.

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.
Also, I'd be curious about the other uses of the key arg for compatibility considerations.

@eckp eckp changed the title [wip] feat: fill UI default values with CLI args [wip] feat: add CLI args to control UI elements when running notebook as script Mar 16, 2025
@mscolnick
Copy link
Contributor

@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 key i thought of was a way to keep values in sync

mo.ui.number(key="age")
mo.ui.slider(key="age")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants