-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
329 additions
and
255 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
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,102 @@ | ||
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024) | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any | ||
|
||
import streamlit | ||
Check notice Code scanning / CodeQL Module is imported with 'import' and 'import from' Note
Module 'streamlit' is imported with both 'import' and 'import from'.
|
||
from streamlit import config, runtime | ||
from streamlit.elements.form import is_in_form | ||
from streamlit.errors import StreamlitAPIException, StreamlitAPIWarning | ||
from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx | ||
Check notice Code scanning / CodeQL Unused import Note
Import of 'get_script_run_ctx' is not used.
|
||
from streamlit.runtime.state import WidgetCallback, get_session_state | ||
|
||
if TYPE_CHECKING: | ||
from streamlit.delta_generator import DeltaGenerator | ||
|
||
|
||
def check_callback_rules(dg: DeltaGenerator, on_change: WidgetCallback | None) -> None: | ||
if runtime.exists() and is_in_form(dg) and on_change is not None: | ||
raise StreamlitAPIException( | ||
"With forms, callbacks can only be defined on the `st.form_submit_button`." | ||
" Defining callbacks on other widgets inside a form is not allowed." | ||
) | ||
|
||
|
||
_shown_default_value_warning: bool = False | ||
|
||
|
||
def check_session_state_rules( | ||
default_value: Any, key: str | None, writes_allowed: bool = True | ||
) -> None: | ||
global _shown_default_value_warning | ||
|
||
if key is None or not runtime.exists(): | ||
return | ||
|
||
session_state = get_session_state() | ||
if not session_state.is_new_state_value(key): | ||
return | ||
|
||
if not writes_allowed: | ||
raise StreamlitAPIException( | ||
f'Values for the widget with key "{key}" cannot be set using `st.session_state`.' | ||
) | ||
|
||
if ( | ||
default_value is not None | ||
and not _shown_default_value_warning | ||
and not config.get_option("global.disableWidgetStateDuplicationWarning") | ||
): | ||
streamlit.warning( | ||
f'The widget with key "{key}" was created with a default value but' | ||
" also had its value set via the Session State API." | ||
) | ||
_shown_default_value_warning = True | ||
|
||
|
||
class CachedWidgetWarning(StreamlitAPIWarning): | ||
def __init__(self): | ||
super().__init__( | ||
""" | ||
Your script uses a widget command in a cached function | ||
(function decorated with `@st.cache_data` or `@st.cache_resource`). | ||
This code will only be called when we detect a cache "miss", | ||
which can lead to unexpected results. | ||
How to fix this: | ||
* Move all widget commands outside the cached function. | ||
* Or, if you know what you're doing, use `experimental_allow_widgets=True` | ||
in the cache decorator to enable widget replay and suppress this warning. | ||
""" | ||
) | ||
|
||
|
||
def check_cache_replay_rules() -> None: | ||
"""Check if a widget is allowed to be used in the current context. | ||
More specifically, this checks if the current context is inside a | ||
cached function that disallows widget usage. If so, it raises a warning. | ||
If there are other similar checks in the future, we could extend this | ||
function to check for those as well. And rename it to check_widget_usage_rules. | ||
""" | ||
if runtime.exists(): | ||
from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx | ||
|
||
ctx = get_script_run_ctx() | ||
if ctx and ctx.disallow_cached_widget_usage: | ||
# We use an exception here to show a proper stack trace | ||
# that indicates to the user where the issue is. | ||
streamlit.exception(CachedWidgetWarning()) |
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
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
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
Oops, something went wrong.