I have just started exploring the streamlit, I liked the simplicity of this but one thing I am struggling right now with this is: any help is appreciated ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/google/slight_smile.png?v=9)
I have initialized a variable(list), and trying to update the list using the button.
> wt = ['initialised text']
> with watchlist:
> st.header('Watchlist')
> #create 2 columns inside watchlist header
> col1 = st.beta_columns()
> with col1:
> container1 = st.beta_container()
> sym = container1.text_input('for adding')
> add_button = container2.button('add')
> if add_button:
> wt.append(sym)
> st.write(wt)
when I click add after giving a text input for example: âsecond textâ
the output is:
[âInitialisedâ, âsecond textâ]
When I add some more text (Third text) into the list undesirable output is coming :
[âInitialisedâ, âThird textâ]
while my desirable output is :
[âInitialisedâ, âsecond textâ, âThird textâ]
2 Likes
Hey @Prakhar_Gupta ,
Try using extend() instead of append(), like the following.
wt.extend(sym)
Using wt.extend() did not gage the result
Instead it seperated letters and put it in the list,
and the next string overrided previous one.
I want my initialised list wt to get updated, do you know any other possible solution?
Hey @Prakhar_Gupta ,
So this took me some time but I have found the solution, itâs a session state problem, basically when the page refreshes we lose what we appended into our list (wt), to solve that follow these steps:
- Copy this SessionState code (SessionState.py) into a .py page and name it SessionState.py in your project folder
- Hereâs the code
import streamlit as st
import SessionState
wt = ['initialised text']
ss = SessionState.get(wt=wt)
st.header('Watchlist')
container1 = st.beta_container()
sym = container1.text_input('for adding')
container2 = st.beta_container()
add_button = container2.button('add')
if add_button:
ss.wt.append(sym)
st.write(ss.wt)
Here is how it supposed to work.
Let us know if it worked perfectly for you!
1 Like
Hi @nainiayoub ,
I have tried the solution given by you and it worked.
But it is not working when I am trying to make two session state variables.
import streamlit as st
import SessionState
select_symbol = ['FIRST','SECOND','THIRD','FOURTH','FIVETH']
#used as options for symbol_input container's selectbox
#------------Initializing Two Session states------------
current_list = []
ss1 = SessionState.get(current_list=current_list)
#use ss1.current_list everywhere now
final_list = []
ss2 = SessionState.get(final_list=final_list)
#use ss2.final_list everywhere now
#------------ Create Containers----------------
watchlist_container = st.beta_container()
order_container = st.beta_container()
with watchlist_container:
st.header('Watchlist')
symbol_input = st.beta_container()
sym = symbol_input.selectbox('Choose Symbols', options=select_symbol)
buttons = st.beta_container()
with buttons:
# three buttons to add, delete, done
add_button, delete_button, done_button = st.beta_columns(3)
add = add_button.button('add')
delete = delete_button.button('delete')
done = done_button.button('done')
if add:
ss1.current_list.append(sym)
if delete:
ss1.current_list.remove(sym)
st.write(ss1.current_list)
#Click on done saves current list to final list
if done:
ss2.final_list.append(ss1.current_list)
with order_container:
st.header('Order Here')
# select a value from ss2.final_list
selected = order_container.selectbox('order symbol', options=ss2.final_list)
#Show final_list
st.write(ss2.final_list)
#Show selected value from final_list
st.write(selected)
The above code is giving an attribute error:
AttributeError: âSessionStateâ object has no attribute âfinal_listâ
Hey @Prakhar_Gupta ,
I have answered you on your topic, hope that helps.
1 Like