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
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.
CodePudding user response:
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)
CodePudding user response:
Assuming you are adding the selectbox inside of the for loop, what is happening is that Python treats a string as an iterable set of characters. st.sidebar.selectbox
expects an iterable (something list-like), and thus takes each character and makes it an option in the selectbox.
I'm not quite sure I'm understanding the data structure you show as JSON (?), but I would use a list-comprehension to just pull the values out you want. Something to the effect of
pairs = [x[0][0] for x in cursor.fetchall()]