It turns out, that the first instance of the selectbox doesnât work well. It always gets the default value. Why is that? How can I write a function wraps a selectbox or any other widget. Note that ultimately, I will have more logic in the function preparing the widget. One this is for sure, I need to have the same element⊠Thatâs why Iâm using a uuid for the key. What do you think?
good morning @drorata
maybe I have not understood entirely the problem, but I think you need to set a value to the select list in the bar function using the index parameter. I also think you need a list to generate and store the results. Not sure the uuid function is required or even useful as you donât return the value and as such donât know what it was named. It is ok if you simply want to prevent that duplicate selectboxes are generated. However, I would use a more simply generated one such as sel1, sel2 etc as shown in the example below.
import streamlit as st
import uuid
ELEMENTS = ["foo", "bar", "hello", "world"]
def bar(sel, key):
selected = st.selectbox("Yo Yo:", ELEMENTS, index=sel, key=key)
return selected
st.write("# ABC")
boxes = [0, 1, 2, 1, 1, 0]
results = [0, 0, 0, 0, 0, 0]
for i in range(4):
results[i] = bar(boxes[i], 'sel' + str(i))
st.write(results[i])
I used it because I want to have multiple widgets with the same caption and therefore, they need different keys. This way I can guarantee that each widget has a unique key. Indeed, it seems like something else is broken due to this.
Let me elaborate on my objective. Assume I have a multi-pages application (following the pattern used by @Marc). Next, assume that I want to have in each page a selectbox saying Yo Yo. In the page the selected value of is used differently.
I hope this explains better my thoughts. @godot63 Iâm not sure that your suggestion aligned with my objective. Please clarify if Iâm wrong.
I still think my solution should be close to what your describe above. I added some code to align it more. let me know how it should behave differently.
import streamlit as st
ELEMENTS = ["foo", "bar", "hello", "world"]
def bar(sel, key):
selected = st.selectbox("Yo Yo:", ELEMENTS, index=sel, key=key)
return selected
st.write("# ABC")
boxes = [0, 1, 2, 1, 1, 0]
results = [0, 0, 0, 0, 0, 0]
for i in range(4):
st.markdown("## Page {}".format(i + 1))
results[i] = bar(boxes[i], 'sel' + str(i))
st.write("this is page {} and here is what I do if the user selects the yoyo selectbox {}".format(i, results[i]))
st.write("do other stuff on this page ")
@godot63 code does help, and here is a simple multi page example:
# app.py
import streamlit as st
import rat_data.dashboard.foo2
import rat_data.dashboard.foo3
PAGES = {"foo2": rat_data.dashboard.foo2, "foo3": rat_data.dashboard.foo3}
selection = st.sidebar.radio("Select the key", list(PAGES.keys()))
page = PAGES[selection]
with st.spinner(f"Loading {selection} ..."):
page.write()
# foo2.py
import streamlit as st
from rat_data.dashboard import foo_util
def write():
st.write("# Page 2")
p2_selected = foo_util.bar(key="p2")
st.write(p2_selected)
# foo3.py
import streamlit as st
from rat_data.dashboard import foo_util
def write():
st.write("# Page 3")
p3_selected = foo_util.bar(key="p3")
st.write(p3_selected)
# foo_util.py
import streamlit as st
ELEMENTS = ["foo", "bar", "hello", "world"]
def bar(key, sel=0):
selected = st.selectbox("Yo Yo:", ELEMENTS, index=sel, key=key)
return selected
This works rather as expected. I do have two concerns/questions
If I select something in one page, move to the other and come back to the first one, then the selection is reset. Is there a way to keep the selection?
The key argument of the widget is still cryptic for me. Who uses it? Why doesnât streamlit take care of that?
hi @drorata, glad it works better. I could not run your example because I am missing the rat_data module, however I suspect that you will have to use the SessionState module we discussed earlier to prevent the set value to switch back to its original.
As for the key argument: in most cases you donât have to set it at all, it will just initialize to the variable to set it to, e.g. selected = st.selectbox(âYo Yo:â, ELEMENTS, index=sel) the key would be selected. its just in the case where you would create another selectbox with the same variable selected, that you have to set the key. I use the key rarely, and if so, it is in the case, where at the moment I create the selectbox, I do not know yet what label or options it has, because that will be determined by a later selectbox. In this case I create the selectbox first as empty, then later changed it e.g. as key.label = âxxxâ.
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.