forked from victoryhb/streamlit-option-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request victoryhb#39 from fgdvir/fix_dynamic_item_add
Fix dynamic item add
- Loading branch information
Showing
4 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from streamlit import session_state as _state | ||
from streamlit.components.v1 import components as _components | ||
|
||
|
||
def _patch_register_widget(register_widget): | ||
def wrapper_register_widget(*args, **kwargs): | ||
user_key = kwargs.get("user_key", None) | ||
callbacks = _state.get("_components_callbacks", None) | ||
|
||
# Check if a callback was registered for that user_key. | ||
if user_key and callbacks and user_key in callbacks: | ||
callback = callbacks[user_key] | ||
|
||
# Add callback-specific args for the real register_widget function. | ||
kwargs["on_change_handler"] = callback[0] | ||
kwargs["args"] = callback[1] | ||
kwargs["kwargs"] = callback[2] | ||
|
||
# Call the original function with updated kwargs. | ||
return register_widget(*args, **kwargs) | ||
|
||
return wrapper_register_widget | ||
|
||
|
||
# Patch function only once. | ||
if not hasattr(_components.register_widget, "__callbacks_patched__"): | ||
setattr(_components.register_widget, "__callbacks_patched__", True) | ||
_components.register_widget = _patch_register_widget(_components.register_widget) | ||
|
||
|
||
def register_callback(element_key, callback, *callback_args, **callback_kwargs): | ||
# Initialize callbacks store. | ||
if "_components_callbacks" not in _state: | ||
_state._components_callbacks = {} | ||
|
||
# Register a callback for a given element_key. | ||
_state._components_callbacks[element_key] = (callback, callback_args, callback_kwargs) |