Skip to content
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

feat: run_button #1514

Merged
merged 4 commits into from
May 29, 2024
Merged

feat: run_button #1514

merged 4 commits into from
May 29, 2024

Conversation

akshayka
Copy link
Contributor

📝 Summary

Adds mo.ui.run_button(), which can be used to trigger computations when a button is pressed and ONLY when a button is pressed.

🔍 Description of Changes

When the button is pressed, its value is set to True and its descendants are run. After its descendants have run, marimo automatically sets the button's value back to False. In this way, cells can gate computations on run_button.value -- updates to other variables won't spuriously cause the cell to run.

Caveat: The button's value is only reset to False when the runtime is configured to autorun cells, since the lazy runtime doesn't know when the button's descendants will/have been run. This should be fine in practice, since ui.run_button has no real use in lazy kernels anyway.

The implementation reuses the frontend plugin for mo.ui.button().

📋 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).
  • I have added tests for the changes made.
  • I have run the code and verified that it works as expected.

📜 Reviewers

@mscolnick

Adds `mo.ui.run_button()`, which can be used to trigger computations
when a button is pressed and ONLY when a button is pressed.

When the button is pressed, its value is set to `True` and its
descendants are run. After its descendants have run, marimo
automatically sets the button's value back to False. In this way,
cells can gate computations on `run_button.value` -- updates to other
variables won't spuriously cause the cell to run.

Caveat: The button's value is only reset to `False` when the runtime is
configured to autorun cells, since the lazy runtime doesn't know when
the button's descendants will/have been run. This should be fine in
practice, since `ui.run_button` has no real use in lazy kernels anyway.
Copy link

vercel bot commented May 28, 2024

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 May 29, 2024 4:06am
marimo-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 29, 2024 4:06am

Comment on lines +382 to +384
def _on_update_completion(self) -> None:
"""Callback to run after the kernel has processed a value update."""
return
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method acts as a post execution hook; the runtime calls it after setting UI element values.

Comment on lines +107 to +115
if isinstance(ctx, KernelRuntimeContext) and ctx.lazy:
# Resetting to False in lazy kernels makes the button pointless,
# since its value hasn't been read by downstream cells on update
# completion.
#
# The right thing to do would be to somehow set to False after
# all cells that were marked stale because of the update were run,
# but that's too complicated.
return
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Special case for lazy kernels, since we don't when descendants will run


@app.cell
def __():
mo.stop(b.value, "Click `run` to submit the slider's value")
Copy link
Contributor

@mscolnick mscolnick May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be mo.stop(not b.value).

not sure if this hides to much, but could put a stop on the button too?

b.stop(fallback=None)

# Run thing

or maybe b.gate(), b.clicked()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops yes it should.

Something like that could be nice — maybe b.gate() ... might add in a follow-up

mscolnick
mscolnick previously approved these changes May 29, 2024
@mscolnick mscolnick merged commit 09f01b9 into main May 29, 2024
30 checks passed
@mscolnick mscolnick deleted the aka/run-button branch May 29, 2024 16:22
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.

None yet

2 participants