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
A callback on a custom component #3977
Comments
Thanks for filing an issue for this @whitphx, we'll do some investigation to try to gauge how difficult it would be to implement this. |
Hello @whitphx, I managed to get callbacks working on custom components with some hacks. Let me know if that works with you: https://gist.github.com/okld/1a2b2fd2cb9f85fc8c4e92e26c6597d5 |
@okld For the core developers: Through this development, I found okld's implementation was sufficient for my needs
Now I think the design of my library Then, please ignore that part of my feature request. |
I have a query regarding the removal of args and kwargs. In my scenario, I create one function and mount 7 versions of a single custom component. Then I create one on_change function and include in the An example:
I tried implementing this using the current changes you have suggested and it says this:
It seems the on_change is triggering before the component mounts. Taking out the args and kwargs would mean I would have to create 7 different versions of the on_change function and hard code their session_state keys and then pass it to the on_change_handler. |
@Socvest I believe the way how this should work would be like the following: from functools import partial
keys = ["one_", "two_", ...]
for key in keys:
custom_component(value=list_of_dicts, on_change=partial(onChangeFunction, key), key=key) Does this work for you? |
@raethlein it kind of does but it triggers as though I have clicked on all components at once instead of just one at a time. The aim was to change the session_state based on the click of one of the components kind of like a filter component (dynamic filter). I did end up refactoring my function where it loops through all and manually searches for which component changed before making changes to the |
@Socvest This sounds like its not related to the new API addition and whether we pass through |
@raethlein Yeah, don't worry. It was a coding issue on my end. I pretty much had to add an if else statement to capture the component with a specific key that was interacted with. All is good. Thanks! |
@Socvest I see, thanks for spending time on testing it and the feedback! |
## Describe your changes In the past, some custom components have used a patch (https://gist.github.com/okld/1a2b2fd2cb9f85fc8c4e92e26c6597d5) 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 the `args` and `kwargs`. The `register_widget` function today uses `args` and `kwargs` as keywords to pass to the `on_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. ## GitHub Issue Link (if applicable) Closes #3977 Related to victoryhb/streamlit-option-menu#70 ## Testing Plan - Explanation of why no additional tests are needed - Unit Tests (JS and/or Python) - A new unit test is added to make sure the on_change callback is called when the value changes during a ScriptRun - E2E Tests - prepare `on_change` callback test in the `option_menu` function - Any manual testing needed? - I manually tested it on the example of [streamlit-option-menu](https://github.com/victoryhb/streamlit-option-menu) --- **Contribution License Agreement** By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.
Problem
I am a developer of a custom component, streamlit-webrtc, which manages its state in Python code, and there is a request to make it possible to detect state changes through the
on_change
callback.However, currently a callback on a custom component is not supported.
Solution
_component_func
orsession_state[key]
changes.WidgetMetadata.callback
,WidgetMetadata.callback_args
,WidgetMetadata.callback_kwargs
, and to forcefully callcall_callback(wid)
for a specific component.Additional context
Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.
If you'd like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.
The text was updated successfully, but these errors were encountered: