Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to populate a streamlit selectbox with vlaues from a SQL query

The goal is to have a streamlit selectbox show a list of pairs coming from the symbol column from the database.

cursor.execute("""
SELECT symbol FROM pair LIMIT 3
""")

pairs = cursor.fetchall()

st.write(pairs)

The output is

[
  0:[
   0:"ETHBTC"
  ]
  1:[
   0:"LTCBTC"
  ]
  2:[
   0:"BNBBTC"
  ]
]

I have a nested for loop to retrieve the symbol of each pair

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for list in pairs:
    for pair in list:
        st.write(pair)

The output is:

ETHBTC
LTCBTC
BNBBTC

So far so good. But when I load it into the selectbox the behavior gets weird. This is just one example but I have many variants of unwanted result.

st.sidebar.selectbox('Select symbol', pair)
B
N
B
B
T
C

I’m stuck for a few hours now. Would be great if someone could help me out. Thank you.

>Solution :

You have to pass a sequence to a selectbox and each element of that sequence will be shown as a possible element to select. I assume what you are passing is pair = ‘BNBBTC’. This means every character becomes one value you could choose. However, what you have to pass to your select box would be
choices = ['ETHBTC', 'LTCBTC', 'BNBBTC'].

As fetchall returns a list of tuples, you can get that as
options = [row[0] for row in cursor.fetchall()] and finally do
st.sidebar.selectbox('Select symbol', options)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading