Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'Runtime not created' issue for custom components executed without Streamlit #8610

Closed

Conversation

raethlein
Copy link
Collaborator

@raethlein raethlein commented May 5, 2024

Describe your changes

Closes #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.

Also, adds tests to the e2e_playwright/custom-components suite to ensure this behavior for the future.

Playwright Test Output With the Fix
components_without_runtime_test.py ..

====================================================================== fixture duration top ======================================================================
total          name                                num avg            min           
0:00:00.000590                         grand total   7 0:00:00.000018 0:00:00.000014
===================================================================== test call duration top =====================================================================
total          name                                num avg            min           
0:00:01.054403         test_folium_runs_standalone   1 0:00:01.054403 0:00:01.054403
0:00:00.309108 test_navigation_bar_runs_standalone   1 0:00:00.309108 0:00:00.309108
0:00:01.363511                         grand total   2 0:00:00.681756 0:00:00.309108
==================================================================== test setup duration top =====================================================================
total          name                                num avg            min           
0:00:00.000622                         grand total   2 0:00:00.000311 0:00:00.000153
=================================================================== test teardown duration top ===================================================================
total          name                                num avg            min           
0:00:00.000082                         grand total   2 0:00:00.000041 0:00:00.000024
======================================================================= 2 passed in 1.54s ========================================================================
Playwright Test Output Without the Fix
components_without_runtime_test.py:30: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../lib/venv/lib/python3.8/site-packages/streamlit_folium/__init__.py:28: in <module>
    _component_func = components.declare_component("st_folium", path=build_dir)
../../lib/streamlit/components/v1/component_registry.py:93: in declare_component
    get_instance().component_registry.register_component(component)
../../lib/streamlit/runtime/__init__.py:28: in get_instance
    return Runtime.instance()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'streamlit.runtime.runtime.Runtime'>

    @classmethod
    def instance(cls) -> Runtime:
        """Return the singleton Runtime instance. Raise an Error if the
        Runtime hasn't been created yet.
        """
        if cls._instance is None:
>           raise RuntimeError("Runtime hasn't been created!")
E           RuntimeError: Runtime hasn't been created!

../../lib/streamlit/runtime/runtime.py:163: RuntimeError
______________________________________________________________ test_navigation_bar_runs_standalone ______________________________________________________________

    def test_navigation_bar_runs_standalone():
        import subprocess
    
        result = subprocess.run(
            ["streamlit-navigation-bar"],
            capture_output=True,  # Python >= 3.7 only
            text=True,  # Python >= 3.7 only
        )
>       assert result.stdout
E       assert ''
E        +  where '' = CompletedProcess(args=['streamlit-navigation-bar'], returncode=1, stdout='', stderr='Traceback (most recent call last)..., in instance\n    raise RuntimeError("Runtime hasn\'t been created!")\nRuntimeError: Runtime hasn\'t been created!\n').stdout

components_without_runtime_test.py:43: AssertionError
===================================================================== fixture duration top ======================================================================
total          name                                num avg            min           
0:00:00.000462                         grand total   7 0:00:00.000021 0:00:00.000013
==================================================================== test call duration top =====================================================================
total          name                                num avg            min           
0:00:01.051296         test_folium_runs_standalone   1 0:00:01.051296 0:00:01.051296
0:00:00.336194 test_navigation_bar_runs_standalone   1 0:00:00.336194 0:00:00.336194
0:00:01.387490                         grand total   2 0:00:00.693745 0:00:00.336194
==================================================================== test setup duration top ====================================================================
total          name                                num avg            min           
0:00:00.000485                         grand total   2 0:00:00.000243 0:00:00.000131
================================================================== test teardown duration top ===================================================================
total          name                                num avg            min           
0:00:00.000078                         grand total   2 0:00:00.000039 0:00:00.000023
==================================================================== short test summary info ====================================================================
FAILED components_without_runtime_test.py::test_folium_runs_standalone - RuntimeError: Runtime hasn't been created!
FAILED components_without_runtime_test.py::test_navigation_bar_runs_standalone - assert ''
======================================================================= 2 failed in 1.60s =======================================================================

GitHub Issue Link

Testing Plan

  • 馃捁 E2E Tests
    • Extend the e2e_playwright/custom-components suite by adding tests that were reported in the issue
  • Any manual testing needed?

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@raethlein raethlein requested a review from LukasMasuch May 5, 2024 09:11
@raethlein raethlein force-pushed the 8606-fix-running-customcomponents-without-runtime branch 3 times, most recently from 5436157 to 948676b Compare May 5, 2024 13:30
Makefile Outdated Show resolved Hide resolved
@raethlein raethlein marked this pull request as ready for review May 5, 2024 13:45
@raethlein raethlein changed the title Fix 'Runtime not created' issue for custom components Fix 'Runtime not created' issue for custom components executed without Streamlit May 5, 2024
@raethlein raethlein force-pushed the 8606-fix-running-customcomponents-without-runtime branch from 291a87e to dfb54a4 Compare May 5, 2024 18:17

def _select_component(app: Page, component: str):
selectbox_input = app.get_by_test_id("stSelectbox").locator("input")

# Type an option (defined in the test app):
selectbox_input.type(component)
selectbox_input.press("Enter")
wait_for_app_run(app)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this wait as without it the exception we are looking for was usually not detected :/

raethlein added a commit that referenced this pull request May 6, 2024
## Describe your changes

The `Makefile` step for the e2e_playwright custom component step was
broken since #8437 and is
fixed here.
Also, the popular_custom_components test did not wait for an app run,
which means that an exception triggered by using a custom component
would not have been caught properly.
Extracts these fixes from the PR
#8610

## GitHub Issue Link (if applicable)

## Testing Plan

- fixes an existing e2e test

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
@raethlein raethlein force-pushed the 8606-fix-running-customcomponents-without-runtime branch from ab7e133 to 4eb4672 Compare May 6, 2024 16:25
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's good in this case to create a dedicated PR with only the changes from this file, which makes it super easy to cherry-pick. And do a fast-follow PR with a testing setup for this situation because it might take another discussion on how best to test this scenario

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, I have opened a dedicated PR here: #8620

raethlein added a commit that referenced this pull request May 6, 2024
## Describe your changes

Closes #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
#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
#8616).

## GitHub Issue Link (if applicable)

- #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 #8616 and
#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.
@raethlein
Copy link
Collaborator Author

raethlein commented May 7, 2024

Update: we have split the PR into a couple of minor PRs to fix the Makefile, tests (#8617) and the actual fix itself (#8620). The PR #8616 migrated our bare-run test to run against the playwright tests, which would have caught the issue. Hence, we won't add the very specific tests in this PR for now and rather rely on the existing-and-now-migrated bare-execution test.

@raethlein raethlein closed this May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Console script returning RuntimeError
2 participants