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 QueryParamsProxy to streamlit api #7772

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
c916991
Add query params class as it doesn't seem like session state internal…
willhuang1997 Nov 9, 2023
93739c9
Add query params to session_state
willhuang1997 Nov 9, 2023
9f29c94
Add QueryParamsProxy with tests
willhuang1997 Nov 9, 2023
019e2dc
Add st.query_params to api
willhuang1997 Nov 9, 2023
6d32245
Add test fixes and add query params to state folder's init
willhuang1997 Nov 9, 2023
bb95ee0
Revert init py as I should add it to another PR
willhuang1997 Nov 10, 2023
3d25628
Add New Classes to state's init
willhuang1997 Nov 10, 2023
f6db72a
Fix typing errors
willhuang1997 Nov 10, 2023
07fe0c4
Add no deadline setting as copying takes longer now
willhuang1997 Nov 10, 2023
9d156f1
Fix tests and remove the parameter in init
willhuang1997 Nov 10, 2023
048e221
Merge branch 'feature/session_state_query_params' into feature/QueryP…
willhuang1997 Nov 10, 2023
ab85db0
Move init declarations around to be consistent
willhuang1997 Nov 10, 2023
578a593
Minor nit fix
willhuang1997 Nov 10, 2023
990d2c4
Add doc string for now. it can change
willhuang1997 Nov 10, 2023
8339c64
Move QueryParamsProxy to its own class
willhuang1997 Nov 10, 2023
e24bee3
Move import around
willhuang1997 Nov 10, 2023
596e05b
Merge branch 'feature/QueryParamsProxy' into feature/add_query_params…
willhuang1997 Nov 10, 2023
a359377
Fix typing errors
willhuang1997 Nov 10, 2023
f6da4e2
Add to_dict to query_params_proxy
willhuang1997 Nov 10, 2023
a5a60c3
Fix typing error
willhuang1997 Nov 10, 2023
1381bc6
Fix typing error
willhuang1997 Nov 10, 2023
8ca2b94
Remove mutable mapping to get rid of type ignores
willhuang1997 Nov 13, 2023
971e27f
Remove part of comment
willhuang1997 Nov 13, 2023
461e445
Merge branch 'feature/QueryParamsProxy' into feature/add_query_params…
willhuang1997 Nov 13, 2023
c93ec1a
Add test for write.py
willhuang1997 Nov 13, 2023
d3f622e
Move query_params in commands folder to experimental_query_params.py
willhuang1997 Nov 14, 2023
f9c8e86
Merge branch 'feature/session_state_query_params' into feature/QueryP…
willhuang1997 Nov 14, 2023
5835aee
Merge branch 'feature/QueryParamsProxy' into feature/add_query_params…
willhuang1997 Nov 14, 2023
6a63fd4
Add embed exception tests]
willhuang1997 Nov 14, 2023
1174338
Merge branch 'feature/session_state_query_params' into feature/QueryP…
willhuang1997 Nov 14, 2023
9273f14
Merge branch 'feature/QueryParamsProxy' into feature/add_query_params…
willhuang1997 Nov 14, 2023
9e483cb
Adjust for comments and also change query_params_test in commands fol…
willhuang1997 Nov 15, 2023
59212c7
Fix typing error
willhuang1997 Nov 15, 2023
109d012
Fix names of tests
willhuang1997 Nov 15, 2023
7db65d1
Change query_param variable name to d
willhuang1997 Nov 15, 2023
660dcfb
Fix test as I refactored some code
willhuang1997 Nov 15, 2023
b4e01a5
Fix comment
willhuang1997 Nov 15, 2023
f5c1477
Address comments
willhuang1997 Nov 15, 2023
fe3e40c
Fix typing errors and remove unused imports
willhuang1997 Nov 15, 2023
fb60587
Fix threading problem as we should make sure we lock
willhuang1997 Nov 15, 2023
9c640ba
Fix typing errors
willhuang1997 Nov 15, 2023
2d47695
Merge branch 'feature/session_state_query_params' into feature/QueryP…
willhuang1997 Nov 15, 2023
fb2bd42
Minor fixes with typing
willhuang1997 Nov 15, 2023
fcdc169
Merge branch 'feature/QueryParamsProxy' into feature/add_query_params…
willhuang1997 Nov 15, 2023
14b7eb7
Remove underscore from query params
willhuang1997 Nov 17, 2023
8f5bc1e
Fix typing mistakes, remove unnecessary methods, and fix comments
willhuang1997 Nov 17, 2023
24228b0
Remove unnecessary tests and fix function definitions of tests
willhuang1997 Nov 17, 2023
9c57c14
Fix test function name
willhuang1997 Nov 17, 2023
c413887
Fix comment
willhuang1997 Nov 17, 2023
b3084d3
Attempt to make comments better
willhuang1997 Nov 17, 2023
2378b8a
Merge branch 'feature/session_state_query_params' into feature/QueryP…
willhuang1997 Nov 17, 2023
bdfce4a
Minor fixes to comments and tests
willhuang1997 Nov 17, 2023
e2f9963
Remove attr methods as that causes a lot of hackiness
willhuang1997 Nov 17, 2023
1f909ae
Modify test as we used a hack
willhuang1997 Nov 17, 2023
7bce541
Add comments for get item
willhuang1997 Nov 20, 2023
4d2a491
Fix to_dict to only contain last element
willhuang1997 Nov 20, 2023
5a878e0
Merge branch 'feature/session_state_query_params' into feature/QueryP…
willhuang1997 Nov 21, 2023
88ad341
Fix threading problem and add tests
willhuang1997 Nov 25, 2023
ec15178
Fix typing problems
willhuang1997 Nov 25, 2023
ba5c0bc
Fix typing for contextmanager method
willhuang1997 Nov 27, 2023
ce2f7d9
Merge branch 'feature/QueryParamsProxy' into feature/add_query_params…
willhuang1997 Nov 27, 2023
18c75e1
Add comment
willhuang1997 Nov 28, 2023
f51692d
Merge branch 'develop' into feature/session_state_query_params
willhuang1997 Nov 28, 2023
359ee6e
fix comments
willhuang1997 Nov 29, 2023
385b14a
Address comments
willhuang1997 Nov 29, 2023
3572673
Merge branch 'feature/session_state_query_params' into feature/QueryP…
willhuang1997 Nov 29, 2023
6529f30
address comments
willhuang1997 Nov 30, 2023
9090315
Merge branch 'feature/st_query_params_2' into feature/QueryParamsProxy
willhuang1997 Nov 30, 2023
b52d85a
Change import as i forgot to commit this file
willhuang1997 Nov 30, 2023
ad5b692
Merge branch 'feature/QueryParamsProxy' into feature/add_query_params…
willhuang1997 Nov 30, 2023
dfc3172
Merge branch 'feature/st_query_params_2' into feature/add_query_param…
willhuang1997 Nov 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/streamlit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@
)
from streamlit.runtime.metrics_util import gather_metrics as _gather_metrics
from streamlit.runtime.secrets import secrets_singleton as _secrets_singleton
from streamlit.runtime.state import SessionStateProxy as _SessionStateProxy
from streamlit.runtime.state import (
SessionStateProxy as _SessionStateProxy,
QueryParamsProxy as _QueryParamsProxy,
)
from streamlit.user_info import UserInfoProxy as _UserInfoProxy
from streamlit.commands.experimental_query_params import (
get_query_params as _get_query_params,
Expand Down Expand Up @@ -192,6 +195,8 @@ def _update_logger() -> None:
# Session State
session_state = _SessionStateProxy()

query_params = _QueryParamsProxy()

# Caching
cache_data = _cache_data
cache_resource = _cache_resource
Expand Down
4 changes: 2 additions & 2 deletions lib/streamlit/elements/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from streamlit.proto.Json_pb2 import Json as JsonProto
from streamlit.runtime.metrics_util import gather_metrics
from streamlit.runtime.state import SessionStateProxy
from streamlit.runtime.state import QueryParamsProxy, SessionStateProxy
from streamlit.user_info import UserInfoProxy

if TYPE_CHECKING:
Expand Down Expand Up @@ -74,7 +74,7 @@ def json(
"""
import streamlit as st

if isinstance(body, (SessionStateProxy, UserInfoProxy)):
if isinstance(body, (SessionStateProxy, UserInfoProxy, QueryParamsProxy)):
body = body.to_dict()

if not isinstance(body, str):
Expand Down
6 changes: 4 additions & 2 deletions lib/streamlit/elements/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from streamlit.errors import StreamlitAPIException
from streamlit.logger import get_logger
from streamlit.runtime.metrics_util import gather_metrics
from streamlit.runtime.state import SessionStateProxy
from streamlit.runtime.state import QueryParamsProxy, SessionStateProxy
from streamlit.string_util import is_mem_address_str, probably_contains_html_tags
from streamlit.user_info import UserInfoProxy

Expand Down Expand Up @@ -234,7 +234,9 @@ def flush_buffer():
flush_buffer()
dot = vis_utils.model_to_dot(arg)
self.dg.graphviz_chart(dot.to_string())
elif isinstance(arg, (dict, list, SessionStateProxy, UserInfoProxy)):
elif isinstance(
arg, (dict, list, SessionStateProxy, UserInfoProxy, QueryParamsProxy)
):
flush_buffer()
self.dg.json(arg)
elif type_util.is_namedtuple(arg):
Expand Down
5 changes: 3 additions & 2 deletions lib/streamlit/runtime/state/query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ def clear(self) -> None:
self._query_params.clear()
self._send_query_param_msg()

def to_dict(self) -> Dict[str, Union[List[str], str]]:
return self._query_params
def to_dict(self) -> Dict[str, str]:

Choose a reason for hiding this comment

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

Could we mimick the to_dict method as Flask provides it?

For flat=True it would do what it currently does. For flat=False it would make a list for every key, regardless of whether it's only one value. I like this because if the flat parameter is not available the user of to_dict will have to do a lot of isinstance checking.

Or is there already a different way of predictably getting a list of values (even if there is only one value)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should be solved with get_all() support.

Choose a reason for hiding this comment

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

Should be solved with get_all() support.

Thanks! I'll check it out

# return the last query param if multiple keys are set
return {key: self[key] for key in self._query_params}


def missing_key_error_message(key: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion lib/streamlit/runtime/state/query_params_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ def clear(self) -> None:
with get_session_state().query_params() as qp:
qp.clear()

def to_dict(self) -> Dict[str, Union[List[str], str]]:
def to_dict(self) -> Dict[str, str]:
with get_session_state().query_params() as qp:
return qp.to_dict()
5 changes: 5 additions & 0 deletions lib/tests/streamlit/runtime/state/query_params_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,8 @@ def test__setitem__raises_exception_for_embed_key(self):
def test__setitem__raises_exception_for_embed_options_key(self):
with pytest.raises(StreamlitAPIException):
self.query_params["embed_options"] = "show_toolbar"

def test_to_dict(self):
self.query_params["baz"] = ""
result_dict = {"foo": "bar", "two": "y", "baz": ""}
assert self.query_params.to_dict() == result_dict
1 change: 1 addition & 0 deletions lib/tests/streamlit/streamlit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def test_public_api(self):
"cache",
"secrets",
"session_state",
"query_params",
"cache_data",
"cache_resource",
# Experimental APIs:
Expand Down
9 changes: 8 additions & 1 deletion lib/tests/streamlit/write_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from streamlit.elements import write
from streamlit.error_util import handle_uncaught_app_exception
from streamlit.errors import StreamlitAPIException
from streamlit.runtime.state import SessionStateProxy
from streamlit.runtime.state import QueryParamsProxy, SessionStateProxy


class StreamlitWriteTest(unittest.TestCase):
Expand Down Expand Up @@ -191,6 +191,13 @@ def test_session_state(self):

p.assert_called_once()

def test_query_params(self):
"""Test st.write with st.query_params."""
with patch("streamlit.delta_generator.DeltaGenerator.json") as p:
st.write(QueryParamsProxy())

p.assert_called_once()

def test_snowpark_dataframe_write(self):
"""Test st.write with snowflake.snowpark.dataframe.DataFrame."""
# Import package inside the test so the test suite still runs even if you don't
Expand Down