Home > database >  allow the user to select NULL from dropdownlist in streamlit
allow the user to select NULL from dropdownlist in streamlit

Time:12-08

I have a streamlit app that is connected to the SQL database where there is a insert form that take user input in this form i had includes selectbox that display the data extracted from the database in order to allow the user to select one of these options.

What i want is to allow the user in selectbox to select NULL or leave it empty .

The problem is that once it display the data extracted from the database it doesn't contains NULL and it add the first value as default.

code:

pattern = r"(?<=')[^,] ?(?=')"
query_loc =(
               r"select distinct location "
               r"from dbo.info"
               )    
rows_loc = cursor.execute(query_loc).fetchall()
rows_loc = re.findall(pattern, " ".join([str(s) for s in rows_loc]))

loc__stb =  st.selectbox("Select location",rows_loc)

CodePudding user response:

If I get it right, this

query_loc =(
           r"select distinct location "
           r"from dbo.info"
           ) 

is where the select box gets populated. And the reason why there is no null value is just because there is no location value with null in dbo.info table.

If so, you need add a null value to that select manually. So, it needs to be looking like that

r"select ' ' location union all"
r"select distinct location "
r"from dbo.info"

not sure about python-syntax though. Check it out please

UPD: you may use any other string instead of a space symbol.

  • Related