Button callback string to list - when pressed


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


symbols_list = [] 

def add_symbols(tckr):

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')


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.


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.


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):

# 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)

Thank you @edsaac and @Shawn_Pereira !!

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

start = datetime(2017, 1, 1) ####

         'EUROB.AT', 'ETE.AT', 'ALPHA.AT', 'MOH.AT', 'BELA.AT', 'TENERGY.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