Skip to content

Commit

Permalink
Fix standalone custom-component execution (streamlit#8620)
Browse files Browse the repository at this point in the history
## Describe your changes

Closes streamlit#8606

The PR allows custom component modules to be executed standalone by not
registering them with Streamlit at all, which makes sense since
Streamlit is not started.

It's a cherry-pick of this broader PR
streamlit#8610, which also adds tests.
However, we want to discuss our testing strategy first, so we decouple
it from adding the fix (see for example also this PR
streamlit#8616).

## GitHub Issue Link (if applicable)

- streamlit#8606
- NathanChen198/streamlit-rsa-auth-ui#1
- randyzwitch/streamlit-folium#186

## Testing Plan

- E2E Tests: tests to ensure this behavior will come in a separate PR,
see streamlit#8616 and
streamlit#8610

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
  • Loading branch information
raethlein authored and benjamin-awd committed Sep 29, 2024
1 parent c9525ee commit c5c6a5d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def test_extra_streamlit_components(app: Page):
def test_folium(app: Page):
"""Test that the folium component renders"""
_select_component(app, "folium")
_expect_no_exception(app)
_expect_iframe_attached(app)


Expand Down
11 changes: 8 additions & 3 deletions lib/streamlit/components/v1/component_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from streamlit.components.types.base_component_registry import BaseComponentRegistry
from streamlit.components.v1.custom_component import CustomComponent
from streamlit.runtime import get_instance
from streamlit.runtime.scriptrunner import get_script_run_ctx


def _get_module_name(caller_frame: FrameType) -> str:
Expand All @@ -47,7 +48,9 @@ def declare_component(
path: str | None = None,
url: str | None = None,
) -> CustomComponent:
"""Create and register a custom component.
"""Create a custom component and register it if there is a ScriptRun context.
The component is not registered when there is no ScriptRun context; this can happen when CustomComponents are executed as standalone commands, e.g. for testing.
Parameters
----------
Expand Down Expand Up @@ -84,8 +87,10 @@ def declare_component(
component = CustomComponent(
name=component_name, path=path, url=url, module_name=module_name
)
get_instance().component_registry.register_component(component)

# the ctx can be None if a custom component script is run outside of Streamlit, e.g. via 'python ...'
ctx = get_script_run_ctx()
if ctx is not None:
get_instance().component_registry.register_component(component)
return component


Expand Down

0 comments on commit c5c6a5d

Please sign in to comment.