How to check if query string is empty

How can I check if there is a query string? I want to do something if there is and something else if there is not a query string.

You can do that with st.query_params – here is a simple example

import streamlit as st

if st.checkbox("Add foo to query params"):
    st.query_params["foo"] = "bar"
    if "foo" in st.query_params:
        del st.query_params["foo"]

if "foo" in st.query_params:
    st.write("Foo:", st.query_params["foo"])
    st.write("No foo in query params")
1 Like

That works quite nice. Thank you!

Bonus question, any thoughts on how to avoid that querystring being overloaded? Like someone just fills it with pages of nonsense to crash the app?

You cannot control what other people write in their browser’s address bar.You can control what you do with the query parameters.

1 Like

Okay, this is what I will use for now. I do not need to keep the value longer than the first time I use it so I clear it after taking it. the :300 cuts off anything after character #300.

if "a" in st.query_params: 
     qstring = st.query_params["a"][:300]

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.