-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade streamlit as a dev dep to 1.12.0 and fix imports (#1005)
* Upgrade streamlit as a dev dep to 1.12.0 and fix imports * Update CHANGELOG.md * Fix to use packaging.version for version comparison * Fix import * Fix import * Add a test matrix item * Fix initialization of SessionInfo in a test
- Loading branch information
Showing
10 changed files
with
131 additions
and
42 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import logging | ||
|
||
import streamlit as st | ||
from packaging import version | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
_server = None | ||
|
||
VERSION_1_12_0 = version.parse("1.12.0") | ||
|
||
|
||
def _is_modern_architecture() -> bool: | ||
"""Returns if the imported streamlit package version is >=1.12.0. | ||
It is because since that version, streamlit has changed its internal architecture | ||
making `web` and `runtime` submodules to which some files have been moved | ||
decoupling the web server-related files and the core runtime, | ||
e.g. https://github.com/streamlit/streamlit/pull/4956. | ||
During this a huge refactoring, `Server._singleton` and | ||
its accessor `Server.get_current()` have been removed | ||
(https://github.com/streamlit/streamlit/pull/4966) | ||
that we have been using as a server-wide global object, | ||
so we have to change the way to access it. | ||
""" | ||
return version.parse(st.__version__) >= VERSION_1_12_0 | ||
|
||
|
||
def get_current_server(): | ||
global _server | ||
if _server: | ||
return _server | ||
|
||
if _is_modern_architecture(): | ||
logger.debug( | ||
"The running Streamlit version is gte 1.12.0. " | ||
"Try to get the server instance" | ||
) | ||
|
||
import gc | ||
|
||
from streamlit.web.server.server import Server | ||
|
||
servers = [obj for obj in gc.get_objects() if isinstance(obj, Server)] | ||
|
||
if len(servers) == 0: | ||
raise RuntimeError("Unexpectedly no server exists") | ||
if len(servers) > 1: | ||
logger.warning( | ||
"Unexpectedly multiple server instances exist. Use the first one." | ||
) | ||
|
||
_server = servers[0] | ||
else: | ||
logger.debug( | ||
"The running Streamlit version is less than 1.12.0. " | ||
"Call Server.get_current()" | ||
) | ||
try: | ||
from streamlit.web.server.server import Server | ||
except ModuleNotFoundError: | ||
# streamlit < 1.12.0 | ||
from streamlit.server.server import Server | ||
|
||
_server = Server.get_current() | ||
|
||
return _server |
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
from unittest.mock import Mock | ||
|
||
from streamlit.server.server import SessionInfo | ||
|
||
from streamlit_webrtc.session_info import get_script_run_count | ||
from streamlit_webrtc.server import _is_modern_architecture | ||
from streamlit_webrtc.session_info import SessionInfo, get_script_run_count | ||
|
||
|
||
def test_get_script_run_count(): | ||
session_info = SessionInfo(ws=Mock(), session=Mock()) | ||
if _is_modern_architecture(): | ||
session_info = SessionInfo(client=Mock(), session=Mock()) | ||
else: | ||
session_info = SessionInfo(ws=Mock(), session=Mock()) | ||
assert get_script_run_count(session_info) == 0 |