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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an import layer both for st.rerun and st.experimental_rerun for different Streamlit versions #1393

Merged
merged 5 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 13 additions & 24 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions streamlit_webrtc/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -96,4 +103,5 @@
"AppSessionState",
"SessionInfo",
"get_script_run_ctx",
"rerun",
]
16 changes: 8 additions & 8 deletions streamlit_webrtc/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -512,21 +513,20 @@ 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:
restored_component_value_snapshot = context._component_value_snapshot
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)
Expand All @@ -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:
Expand Down Expand Up @@ -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