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
Allow passing on_change_callback for CustomComponents #8633
Conversation
@@ -215,7 +215,7 @@ def user_key_from_widget_id(widget_id: str) -> str | None: | |||
"None" as a key, but we can't avoid this kind of problem while storing the | |||
string representation of the no-user-key sentinel as part of the widget id. | |||
""" | |||
user_key = widget_id.split("-", maxsplit=2)[-1] | |||
user_key: str | None = widget_id.split("-", maxsplit=2)[-1] |
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.
this is an unrelated drive-by change since the linter complained about wrong typing
# declare_component needs a script_run_ctx to be set | ||
self.script_run_ctx = MagicMock(spec=ScriptRunContext) | ||
add_script_run_ctx(threading.current_thread(), self.script_run_ctx) |
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.
This has to be added if you want to run the tests here standalone like pytest lib/tests/streamlit/components_test.py
. In the CI it does not fail due to pure luck as apparently the order of execution of the tests ensures that a runtime exists 🫠
Hi, this PR looks great, |
@whitphx great, thanks for trying it out! Each PR generates a wheel file in the CI pipeline, here is the one for this 😊: https://core-previews.s3-us-west-2.amazonaws.com/pr-8633/streamlit-1.34.0-py2.py3-none-any.whl |
Thanks! |
@@ -102,6 +112,7 @@ def create_instance( | |||
*args, | |||
default: Any = None, | |||
key: str | None = None, | |||
on_change_handler: WidgetCallback | None, |
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.
@sfc-gh-jcarroll @jrieke Lukas' point makes sense to me. Could you have a quick look and confirm this, please?
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.
LGTM 👍 But I think we probably want to call this on_change
and not on_change_handler
to better align with APIs of other commands.
025ef65
to
6a86336
Compare
6a86336
to
31e7b91
Compare
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.
LGTM 👍 Another way to test it could be to update our component-template tests and update a component to use this callback.
Describe your changes
In the past, some custom components have used a patch to register an on_change callback. Recently, we have done some refactoring that broke this workaround. This PR is a suggestion to extend our official API to make the patch redundant.
Note that we only want to pass the
on_change_callback
and not theargs
andkwargs
.The
register_widget
function today usesargs
andkwargs
as keywords to pass to theon_change callback
. Besides the unfortunate naming - these are special keywords meant for functions themselves and not for pass-through arguments - we are thinking about deprecating them entirely, since you can wrap the callback easily to pass the arguments.The way you can use it then looks like the following:
Without the partial-function
GitHub Issue Link (if applicable)
Closes #3977
Related to victoryhb/streamlit-option-menu#70
Testing Plan
on_change
callback test in theoption_menu
functionContribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.