Skip to content

Commit

Permalink
Add support for converting query params to string (streamlit#8030)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmasuch authored Jan 29, 2024
1 parent e8f5fa9 commit 5bf922f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion e2e_playwright/st_query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

import streamlit as st

st.write(st.query_params)
st.markdown(str(st.query_params))
13 changes: 3 additions & 10 deletions e2e_playwright/st_query_params_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from playwright.sync_api import Page
from playwright.sync_api import Page, expect

test_dicts = [{"x": "y"}, {"x": "y", "a": "b"}, {"x": ("y", 1, 2.34)}, {"x": ""}]
test_dicts = [{"x": "y"}, {"x": "y", "a": "b"}, {"x": ["y", "1", "2.34"]}, {"x": ""}]


@pytest.mark.parametrize("app_with_query_params", test_dicts, indirect=True)
def test_app_with_query_params(app_with_query_params: Page):
page, test_dict = app_with_query_params
for key, value in test_dict.items():
assert page.get_by_text(key) is not None

if isinstance(value, (list, tuple)):
for item in value:
assert page.get_by_text(item) is not None
else:
assert page.get_by_text(value) is not None
expect(page.get_by_test_id("stMarkdownContainer")).to_have_text(str(test_dict))
4 changes: 4 additions & 0 deletions lib/streamlit/runtime/state/query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def __len__(self) -> int:
{key for key in self._query_params if key not in EMBED_QUERY_PARAMS_KEYS}
)

def __str__(self) -> str:
self._ensure_single_query_api_used()
return str(self._query_params)

def _send_query_param_msg(self) -> None:
# Avoid circular imports
from streamlit.runtime.scriptrunner import get_script_run_ctx
Expand Down
4 changes: 4 additions & 0 deletions lib/streamlit/runtime/state/query_params_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def __len__(self) -> int:
with get_session_state().query_params() as qp:
return len(qp)

def __str__(self) -> str:
with get_session_state().query_params() as qp:
return str(qp)

@gather_metrics("query_params.get_item")
def __getitem__(self, key: str) -> str:
with get_session_state().query_params() as qp:
Expand Down
3 changes: 3 additions & 0 deletions lib/tests/streamlit/runtime/state/query_params_proxy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def test__delitem__deletes_entry(self):
def test__len__returns_correct_len(self):
assert len(self.query_params_proxy) == 1

def test__str__returns_correct_str(self):
assert str(self.query_params_proxy) == "{'test': 'value'}"

def test__iter__returns_correct_iter(self):
keys = list(iter(self.query_params_proxy))
assert keys == ["test"]
Expand Down

0 comments on commit 5bf922f

Please sign in to comment.