Button callback string to list - when pressed

Hello,

I am trying to code (newbie) a page in streamlit that has a number of buttons that display stock tickers. When the user clicks each button I would like to store the ticker string to a list. I dont know what I am doing wrong… please see my code below. I also dont understand the use of key in st.button

input:

symbols_list = [] 


def add_symbols(tckr):
    
    symbols_list.append(tckr)
    st.write(symbols_list);

st.button(label='MYTIL',  key=None,  on_click=add_symbols('MYTIL.AT'), args='MYTIL.AT')   
st.button(label='OPAP',   key=None,  on_click=add_symbols('OPAP.AT'),  args='OPAP.AT')
st.button(label='ADMIE',  key=None,  on_click=add_symbols('ADMIE.AT'), args='ADMIE.AT')  
st.button(label='EYDAP',  key=None,  on_click=add_symbols('EYDAP.AT'), args='EYDAP.AT')
st.button(label='ELPE',   key=None,  on_click=add_symbols('ELPE.AT'),  args='ELPE.AT')
st.button(label='HTO',    key=None,  on_click=add_symbols('HTO.AT'),   args='HTO.AT')

output:

any help would be great :smiley:
thank you

Hi @Yannis_Antypas, please read up on session state: Session State - Streamlit Docs

You should be able to persist your previous list values and incrementally add to it with button clicks.

Cheers

On top of the session management, the on_click parameter that you need to pass to the button is the function per se (i.e., add_symbols), not to call the function (i.e., add_symbols(xxx)). In this case, you are executing the function which at the end returns None, and that None is then passed to the on_click of the button. Also, the args must be contained within a list, even if it’s only one.

buttonsAndState

import streamlit as st

col1, col2 = st.columns(2)

# Keep the list in the session state
if 'myList' not in st.session_state: 
    st.session_state['myList'] = []

symbols_list = st.session_state['myList']

def add_symbols(tckr):
    symbols_list.append(tckr)
    col2.write(symbols_list)

# Reset the session state 
def empty_list(): del st.session_state['myList']

with col1:
    st.button(label="MYTIL", on_click=add_symbols, args=["MYTIL.AT"])
    st.button(label="OPAP", on_click=add_symbols, args=["OPAP.AT"])
    st.button(label="ADMIE", on_click=add_symbols, args=["ADMIE.AT"])
    st.button(label="EYDAP", on_click=add_symbols, args=["EYDAP.AT"])
    st.button(label="ELPE", on_click=add_symbols, args=["ELPE.AT"])
    st.button(label="HTO", on_click=add_symbols, args=["HTO.AT"])

    st.button(label="❌", on_click=empty_list)
2 Likes

Thank you @edsaac and @Shawn_Pereira !!

I’ve found a solution in one project I found online github here

symbols=[]
start = datetime(2017, 1, 1) ####
st.set_page_config(layout='centered')

 
tckrs = ['MYTIL.AT','OPAP.AT','ADMIE.AT','EYDAP.AT', 'ELPE.AT', 'HTO.AT', \
         'EUROB.AT', 'ETE.AT', 'ALPHA.AT', 'MOH.AT', 'BELA.AT', 'TENERGY.AT', \
         'TPEIR.AT','VIO.AT', 'LAMDA.AT', 'GEKTERNA.AT', 'SAR.AT', 'CENER.AT', \
         'AEGN.AT', 'KAMP.AT', 'LYK.AT', 'ASCO.AT']
multiselection = st.multiselect("Please Select Stocks:", \
                    tckrs, default= ['MYTIL.AT','OPAP.AT',\
                                     'ADMIE.AT','EYDAP.AT', 'ELPE.AT',  \
                                     'EUROB.AT', 'ETE.AT', 'ALPHA.AT',  \
                                     'MOH.AT',  'TENERGY.AT', 'VIO.AT'])



symbols_list = multiselection