-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
002e493
Allow passing on_change_callback natively
raethlein 4a859f6
Fix linter issue
raethlein 8c0a5bf
Add unit test for callback
raethlein 4f11d20
Add missing script run context
raethlein 0425946
Rename on_change_handler to on_change
raethlein 31e7b91
Prepare e2e test for on_change callback
raethlein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
import inspect | ||
import json | ||
import os | ||
import threading | ||
import unittest | ||
from typing import Any | ||
from unittest import mock | ||
|
@@ -38,9 +39,11 @@ | |
from streamlit.components.v1.custom_component import CustomComponent | ||
from streamlit.errors import DuplicateWidgetID, StreamlitAPIException | ||
from streamlit.proto.Components_pb2 import SpecialArg | ||
from streamlit.proto.WidgetStates_pb2 import WidgetState, WidgetStates | ||
from streamlit.runtime import Runtime, RuntimeConfig | ||
from streamlit.runtime.memory_media_file_storage import MemoryMediaFileStorage | ||
from streamlit.runtime.memory_uploaded_file_manager import MemoryUploadedFileManager | ||
from streamlit.runtime.scriptrunner import ScriptRunContext, add_script_run_ctx | ||
from streamlit.type_util import to_bytes | ||
from tests.delta_generator_test_case import DeltaGeneratorTestCase | ||
|
||
|
@@ -75,6 +78,10 @@ def setUp(self) -> None: | |
) | ||
self.runtime = Runtime(config) | ||
|
||
# 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) | ||
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
def tearDown(self) -> None: | ||
Runtime._instance = None | ||
|
||
|
@@ -484,6 +491,34 @@ def test_df_default(self): | |
proto.special_args[0], | ||
) | ||
|
||
def test_on_change_handler(self): | ||
"""Test the 'on_change' callback param.""" | ||
|
||
# we use a list here so that we can update it in the lambda; we cannot assign a variable there. | ||
callback_call_value = [] | ||
expected_element_value = "Called with foo" | ||
|
||
def create_on_change_handler(some_arg: str): | ||
return lambda: callback_call_value.append("Called with " + some_arg) | ||
|
||
return_value = self.test_component( | ||
key="key", default="baz", on_change=create_on_change_handler("foo") | ||
) | ||
self.assertEqual("baz", return_value) | ||
|
||
proto = self.get_delta_from_queue().new_element.component_instance | ||
self.assertJSONEqual({"key": "key", "default": "baz"}, proto.json_args) | ||
current_widget_states = self.script_run_ctx.session_state.get_widget_states() | ||
new_widget_state = WidgetState() | ||
# copy the custom components state and update the value | ||
new_widget_state.CopyFrom(current_widget_states[0]) | ||
# update the widget's value so that the rerun will execute the callback | ||
new_widget_state.json_value = '{"key": "key", "default": "baz2"}' | ||
self.script_run_ctx.session_state.on_script_will_rerun( | ||
WidgetStates(widgets=[new_widget_state]) | ||
) | ||
self.assertEqual(callback_call_value[0], expected_element_value) | ||
|
||
def assertJSONEqual(self, a, b): | ||
"""Asserts that two JSON dicts are equal. If either arg is a string, | ||
it will be first converted to a dict with json.loads().""" | ||
|
@@ -559,6 +594,9 @@ def get_component_path(self, name: str) -> str | None: | |
def get_module_name(self, name: str) -> str | None: | ||
return None | ||
|
||
def get_component(self, name: str) -> BaseCustomComponent | None: | ||
return None | ||
|
||
def get_components(self) -> list[BaseCustomComponent]: | ||
return [] | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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