Skip to content

Commit

Permalink
Add a top-level example to st.query_params
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-tteixeira authored Apr 18, 2024
1 parent 5292ce5 commit 25c9c7e
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions content/develop/api-reference/caching-and-state/query_params.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,40 @@ description: st.query_params reads and manipulates query parameters in the brows

## st.query_params

`st.query_params` provides a dictionary-like interface to access query parameters in your app's URL and is available as of Streamlit 1.30.0. It behaves similarly to `st.session_state` with the notable exception that keys may be repeated in an app's URL. Handling of repeated keys requires special consideration as explained below. `st.query_params` can be used with both key and attribute notation. For example, `st.query_params.my_key` and `st.query_params["my_key"]`.
`st.query_params` provides a dictionary-like interface to access query parameters in your app's URL and is available as of Streamlit 1.30.0. It behaves similarly to `st.session_state` with the notable exception that keys may be repeated in an app's URL. Handling of repeated keys requires special consideration as explained below.

Query parameters can be entered into your app's URL or programatically added through `st.query_params`. For example, the following URL and dictionary show the same key-value pairs:
`st.query_params` can be used with both key and attribute notation. For example, `st.query_params.my_key` and `st.query_params["my_key"]`. All keys and values will be set and returned as strings. When you write to `st.query_params`, key-value pair prefixed with `?` is added to the end of your app's URL. Each additional pair is prefixed with `&` instead of `?`. Query parameters are cleared when navigating between pages in a multipage app.

For example, consider the following URL:

```javascript
https://your_app.streamlit.app/?first_key=1&second_key=two&third_key=true
```

The parameters in the URL above will be accessible in `st.query_params` as:

```python
{
"first_key" : "1",
"second_key" : "two",
"third_key" : "true"
}

```

A key-value pair prefixed with `?` is added to the end of your app's URL. Additional key-value pairs can be added. Each additional pair is prefixed with `&` instead of `?`. All keys and values will be set and returned as strings. Query parameters are cleared when navigating between pages in a multipage app.
This means you can use those parameters in your app like this:

```python
# You can read query params using key notation
if st.query_params["first_key"] == "1":
do_something()

# ...or using attribute notation
if st.query_params.second_key == "two":
do_something_else()

# And you can change a param by just writing to it
st.query_params.first_key = 2 # This gets converted to str automatically
```

### Repeated keys

Expand Down

0 comments on commit 25c9c7e

Please sign in to comment.