First of all, thanks for a wonderful application. It is a joy to use.
I am trying to create a dynamical list which can be extended when you push a button (to build a pipeline in a later stadium). I accomplished this with the use of session state.
Each row of the list is outputted to the dashboard together with a button to remove this row, see figure and code:
import streamlit as st
# Work around to store states across reruns
import SessionState
state = SessionState.get(rerun = False, prepro_steps=[])
if st.button('Add step'):
state.prepro_steps += ['Step added {}'.format(len(state.prepro_steps))]
for i,c in enumerate(state.prepro_steps):
if st.button('Remove step {}'.format(i)):
state.prepro_steps.pop(i)
st.text('Step {}: {}'.format(i, c))
st.write('len prepro = {}'.format(len(state.prepro_steps)))
st.write(state.prepro_steps)
However when I push a remove button something unwanted is happening: the code of the remove button is only activated when the rerun reaches the button again. So in my case the row below is removed because I use the position in the list (or the remove is postponed if you remove the last row).
You can see this behavior in the figure. Here I pressed the Add step button 4 times and then pressed the Remove step 2 button. In the rerun the for loop uses the intact list. On the third run of the loop, when reaching the third element of the list (second step), the row is first outputted to the dashboard before removing the third element (second step) of the list. The third step (fourth element) is not outputted because the for loop terminates because it has reached the length of the now shortened list. However outside the for loop the second element has been removed and the third element has become the second, as is outputted by the last two lines in the code. Thus the third element (second row) has been removed, however this is not outputted to the dashboard as I was expecting.
Iβve modified your code to the following, seems to work well!
import streamlit as st
from collections import OrderedDict
# Work around to store states across reruns
import SessionState
state = SessionState.get(rerun = False, prepro_steps=OrderedDict())
if st.button('Add step'):
try:
next_key = next(reversed(state.prepro_steps)) + 1
except StopIteration:
next_key = 0
state.prepro_steps[next_key] = 'Step added {}'.format(next_key)
for i,c in state.prepro_steps.copy().items():
ph = st.empty()
if ph.button('Remove step {}'.format(i)):
state.prepro_steps.pop(i)
ph = ph.empty()
else:
st.text('Step {}: {}'.format(i, c))
st.write('len prepro = {}'.format(len(state.prepro_steps)))
st.write(list(state.prepro_steps.values()))
Sorry to have wasted your time and once again many thanks for your solution.
One more question for the detectives out there (stop reading if you have something better to do):
I first tried @Jonathan_Rhone s code by making changes to mine, which somehow did not work. I just tried the code by copy pasting and it works just fine. I do not know what I did wrong the first time and that bothers me. I can not find the difference which causes the wrong behaviour, can you?
from collections import OrderedDict
# Work around to store states across reruns
import SessionState
state = SessionState.get(prepro_steps=OrderedDict())
if st.button('Add step'):
try:
next_key = next(reversed(state.prepro_steps)) + 1
except StopIteration:
next_key = 0
state.prepro_steps[next_key] = 'Step added {}'.format(next_key)
for i,c in state.prepro_steps.copy().items():
ph = st.empty()
if st.button('Remove step {}'.format(i),'btn_rstep{}'.format(i)):
state.prepro_steps.pop(i)
ph = ph.empty()
else:
st.text('Step {}: {}'.format(i, c))
st.text('\n')
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking βAccept allβ, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.