From 904d21fa19e6c7a689d0700e5047fa3e5c0c9225 Mon Sep 17 00:00:00 2001 From: "Yuichiro Tachibana (Tsuchiya)" Date: Tue, 26 Sep 2023 13:15:37 +0800 Subject: [PATCH] Add an import layer both for st.rerun and st.experimental_rerun for different Streamlit versions (#1393) * Add an import layer both for st.rerun and st.experimental_rerun for different Streamlit versions * Update streamlit to 1.27.0 * Fix type * Update CI * Fix type --- .github/workflows/main.yml | 3 +++ poetry.lock | 37 ++++++++++++----------------------- streamlit_webrtc/_compat.py | 8 ++++++++ streamlit_webrtc/component.py | 16 +++++++-------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d241f7c3..bd5003ad 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -47,6 +47,9 @@ jobs: # Test with streamlit >=1.18.0. See https://github.com/whitphx/streamlit-webrtc/issues/1187 - python-version: 3.9 streamlit-version: 1.18.0 + # Test with streamlit >=1.27.0. See https://github.com/whitphx/streamlit-webrtc/pull/1393 + - python-version: 3.9 + streamlit-version: 1.27.0 steps: - uses: actions/checkout@v4 diff --git a/poetry.lock b/poetry.lock index 27a74dc8..2a795ef6 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.5.1 and should not be changed by hand. [[package]] name = "aiohttp" @@ -1629,12 +1629,13 @@ files = [ [package.dependencies] numpy = [ - {version = ">=1.21.0", markers = "python_version <= \"3.9\" and platform_system == \"Darwin\" and platform_machine == \"arm64\" and python_version >= \"3.8\""}, + {version = ">=1.21.0", markers = "python_version <= \"3.9\" and platform_system == \"Darwin\" and platform_machine == \"arm64\""}, + {version = ">=1.21.2", markers = "python_version >= \"3.10\""}, + {version = ">=1.21.4", markers = "python_version >= \"3.10\" and platform_system == \"Darwin\""}, {version = ">=1.23.5", markers = "python_version >= \"3.11\""}, - {version = ">=1.21.4", markers = "python_version >= \"3.10\" and platform_system == \"Darwin\" and python_version < \"3.11\""}, - {version = ">=1.21.2", markers = "platform_system != \"Darwin\" and python_version >= \"3.10\" and python_version < \"3.11\""}, - {version = ">=1.19.3", markers = "platform_system == \"Linux\" and platform_machine == \"aarch64\" and python_version >= \"3.8\" and python_version < \"3.10\" or python_version > \"3.9\" and python_version < \"3.10\" or python_version >= \"3.9\" and platform_system != \"Darwin\" and python_version < \"3.10\" or python_version >= \"3.9\" and platform_machine != \"arm64\" and python_version < \"3.10\""}, - {version = ">=1.17.3", markers = "(platform_system != \"Darwin\" and platform_system != \"Linux\") and python_version >= \"3.8\" and python_version < \"3.9\" or platform_system != \"Darwin\" and python_version >= \"3.8\" and python_version < \"3.9\" and platform_machine != \"aarch64\" or platform_machine != \"arm64\" and python_version >= \"3.8\" and python_version < \"3.9\" and platform_system != \"Linux\" or (platform_machine != \"arm64\" and platform_machine != \"aarch64\") and python_version >= \"3.8\" and python_version < \"3.9\""}, + {version = ">=1.19.3", markers = "python_version >= \"3.6\" and platform_system == \"Linux\" and platform_machine == \"aarch64\" or python_version >= \"3.9\""}, + {version = ">=1.17.0", markers = "python_version >= \"3.7\""}, + {version = ">=1.17.3", markers = "python_version >= \"3.8\""}, ] [[package]] @@ -1687,7 +1688,7 @@ files = [ [package.dependencies] numpy = [ {version = ">=1.20.3", markers = "python_version < \"3.10\""}, - {version = ">=1.21.0", markers = "python_version >= \"3.10\" and python_version < \"3.11\""}, + {version = ">=1.21.0", markers = "python_version >= \"3.10\""}, ] python-dateutil = ">=2.8.1" pytz = ">=2020.1" @@ -2075,17 +2076,6 @@ files = [ [package.dependencies] cffi = ">=1.0.0" -[[package]] -name = "pympler" -version = "1.0.1" -description = "A development tool to measure, monitor and analyze the memory behavior of Python objects." -optional = false -python-versions = ">=3.6" -files = [ - {file = "Pympler-1.0.1-py3-none-any.whl", hash = "sha256:d260dda9ae781e1eab6ea15bacb84015849833ba5555f141d2d9b7b7473b307d"}, - {file = "Pympler-1.0.1.tar.gz", hash = "sha256:993f1a3599ca3f4fcd7160c7545ad06310c9e12f70174ae7ae8d4e25f6c5d3fa"}, -] - [[package]] name = "pyopenssl" version = "23.2.0" @@ -2317,13 +2307,13 @@ files = [ [[package]] name = "streamlit" -version = "1.26.0" +version = "1.27.0" description = "A faster way to build and share data apps" optional = false python-versions = ">=3.8, !=3.9.7" files = [ - {file = "streamlit-1.26.0-py2.py3-none-any.whl", hash = "sha256:2bfdac041816e2e1ba27f061d40112afe61e0d4e72d25f354b38ba81107b4cb3"}, - {file = "streamlit-1.26.0.tar.gz", hash = "sha256:25475fb15a3cc9fb184945f3fc936f011998bd8386e0c892febe14c9625bf47a"}, + {file = "streamlit-1.27.0-py2.py3-none-any.whl", hash = "sha256:7488d4e22689d04f40449a1de521ba252ae95a5fbb5f5c606df16a4e16048b47"}, + {file = "streamlit-1.27.0.tar.gz", hash = "sha256:59a704195bbc669c794ddfcc3818480d9b9c2a282c02b48aa6e6de4b1c38d0a0"}, ] [package.dependencies] @@ -2339,8 +2329,7 @@ pandas = ">=1.3.0,<3" pillow = ">=7.1.0,<10" protobuf = ">=3.20,<5" pyarrow = ">=6.0" -pydeck = ">=0.8,<1" -pympler = ">=0.9,<2" +pydeck = ">=0.8.0b4,<1" python-dateutil = ">=2.7.3,<3" requests = ">=2.18,<3" rich = ">=10.14.0,<14" @@ -2348,7 +2337,7 @@ tenacity = ">=8.1.0,<9" toml = ">=0.10.1,<2" tornado = ">=6.0.3,<7" typing-extensions = ">=4.1.0,<5" -tzlocal = ">=1.1,<5" +tzlocal = ">=1.1,<6" validators = ">=0.2,<1" watchdog = {version = ">=2.1.5", markers = "platform_system != \"Darwin\""} diff --git a/streamlit_webrtc/_compat.py b/streamlit_webrtc/_compat.py index af9c9367..0e1ecc76 100644 --- a/streamlit_webrtc/_compat.py +++ b/streamlit_webrtc/_compat.py @@ -87,6 +87,13 @@ ) +try: + from streamlit import rerun # type: ignore +except ImportError: + # streamlit < 1.27.0 + from streamlit import experimental_rerun as rerun # type: ignore + + __all__ = [ "VER_GTE_1_12_0", "VER_GTE_1_12_1", @@ -96,4 +103,5 @@ "AppSessionState", "SessionInfo", "get_script_run_ctx", + "rerun", ] diff --git a/streamlit_webrtc/component.py b/streamlit_webrtc/component.py index 727d6261..d02bd76f 100644 --- a/streamlit_webrtc/component.py +++ b/streamlit_webrtc/component.py @@ -34,6 +34,7 @@ import streamlit as st import streamlit.components.v1 as components +from ._compat import rerun from .components_callbacks import register_callback from .config import ( DEFAULT_AUDIO_HTML_ATTRS, @@ -85,7 +86,7 @@ class WebRtcStreamerState(NamedTuple): signalling: bool -# To restore component value after `streamlit.experimental_rerun()`. +# To restore component value after `rerun()`. class ComponentValueSnapshot(NamedTuple): component_value: Union[Dict, None] run_count: int @@ -512,12 +513,12 @@ def callback(): # HACK: Save the component value in this run to the session state # to be restored in the next run because the component values of - # component instances behind the one which calls `streamlit.experimental_rerun()` + # component instances behind the one which calls `rerun()` # will not be held but be reset to the initial value in the next run. # For example, when there are two `webrtc_streamer()` component instances - # in a script and `streamlit.experimental_rerun()` in the first one is called, + # in a script and `rerun()` in the first one is called, # the component value of the second instance will be None in the next run - # after `streamlit.experimental_rerun()`. + # after `rerun()`. session_info = get_this_session_info() run_count = get_script_run_count(session_info) if session_info else None if component_value is None: @@ -525,8 +526,7 @@ def callback(): if ( restored_component_value_snapshot # Only the component value saved in the previous run is restored - # so that this workaround is only effective in the case of - # `streamlit.experimental_rerun()`. + # so that this workaround is only effective in the case of `rerun()`. and run_count == restored_component_value_snapshot.run_count + 1 ): LOGGER.debug("Restore the component value (key=%s)", key) @@ -550,7 +550,7 @@ def callback(): context._set_worker(None) webrtc_worker = None # Rerun to unset the SDP answer from the frontend args - st.experimental_rerun() + rerun() if webrtc_worker: if video_frame_callback or queued_video_frames_callback or on_video_ended: @@ -596,7 +596,7 @@ def callback(): webrtc_worker.process_offer(sdp_offer["sdp"], sdp_offer["type"]) context._set_worker(webrtc_worker) # Rerun to send the SDP answer to frontend - st.experimental_rerun() + rerun() context._set_worker(webrtc_worker) return context