Hi all, an FYI that we’re working on an upgrade to Streamlit’s query params API. I’m sharing about it today to get feedback on the new API and help assess whether to release it as experimental_ or go directly to a stable API. Take a look and let us know what you think!
TL;DR: Make query_params work more like session state. Return only the latest value (str) by default for a given key. get_all() method to return a list version with all keys.
st.query_params["show_map"] = True
st.query_params["selected"] = ["asia", "america"]
# URL: localhost:8501/?show_map=True&selected=asia&selected=america
# Note we convert everything to str return type
st.query_params["show_map"]
st.query_params.show_map
# both return "True"
# In case of multiple values, by default we just return the last
st.query_params["selected"]
# "america"
# Use get_all() to return a list with all values
st.query_params.get_all("selected")
# ["asia", "america"]
st.query_params.get_all("show_map")
# ["True"]
I think this feature is really useful and I’m happy someone is working on it.
TL;DR I would suggest to add the possibility to configure custom functions for value conversion.
In my opinion, if upon writing the framework converts automatically all values to string, the framework should perform also the opposite and parse values (restoring”0” to 0, etc.) upon reading.
The actual “asymmetrical” behavior (one-way conversion to string) is not consistent and may be confusing for newbies.
I propose 2 possible behaviors to integrate:
A. upon writing accept only string values (and raise TypeError if trying to assign other types). Users should explicitly convert parameter values to string. This makes them aware of the limitation of using only strings in urls, protecting them from gotchas.
B. upon writing accept all primitive types, and convert them to string (actual behavior) but upon reading try to parse to int, float, bool, None, etc. and return that
I think the default behavior should be A, since it is “safer”, but it would be cool to have an option to configure behavior B, setting custom functions for conversions. We may have an option to set functions for all parameters, or only some of them.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.