Hello,
How can I create new text boxes (text_input) by clicking a button. As shown in the reference image, a new text box should get created every time I click the button (something like click to add new row functionality)
Hello,
How can I create new text boxes (text_input) by clicking a button. As shown in the reference image, a new text box should get created every time I click the button (something like click to add new row functionality)
@Prabhat_Ratnala
Hi, I just made it come true:
here is the code:
import streamlit as st
import random
import string
if 'count' not in st.session_state:
st.session_state.count = 0
def add_new_row():
st.text_input("Please input something",key=random.choice(string.ascii_uppercase)+str(random.randint(0,999999)))
if st.button("Add new row"):
st.session_state.count += 1
add_new_row()
if st.session_state.count>1:
for i in range(st.session_state.count-1):
add_new_row()
the most import thing is how to generate different key for the newest text_input
I used the
key=random.choice(string.ascii_uppercase)+str(random.randint(0,999999))
to ensue an unique key will be genreated when button be clicked every time.
How can I save state of each input in the above scenario?
This worked to generate the controls, but I couldnt save each of these inputs to an array or so. This is what worked for me (made some minor changes to your code, I have not tested the below code, but did something similar for myself)
import streamlit as st
import random
import string
if 'input_keys' not in st.session_state:
st.session_state.input_keys= []
if st.button("Add new row"):
st.session_state.input_keys.append(random.choice(string.ascii_uppercase)+str(random.randint(0,999999)))
input_values = []
for input_key in st.session_state.input_keys:
input_value = st.text_input("Please input something", key=input_key)
input_values.append(input_value)
Thanks @najeem for the update. @Prabhat_Ratnala I have worked on a new method if thatβll help you.
import streamlit as st
import random
if 'champs_dict' not in st.session_state:
st.session_state['champs_dict'] = {}
def add_new_row(new_champ):
st.session_state['champs_dict'][new_champ] = st.text_input(new_champ+'...',key=new_champ+str(random.randint(0,999999)))
new_champ = st.text_input("Please enter the new champ...", key = 'new_champ')
add_new = st.button("Add new row")
if add_new:
if new_champ not in st.session_state['champs_dict']:
st.session_state['champs_dict'][new_champ] = ''
for key in st.session_state['champs_dict']:
add_new_row(key)
else:
st.warning(str(new_champ)+" exists already!")
for key in st.session_state['champs_dict']:
add_new_row(key)
This is how Iβve done it, as the above replies were not working exactly for me.
features_list = []
# Display the text areas for each feature
for i in range(st.session_state.num_features):
feature = st.text_area(
f"What is the description of the feature {i + 1} and what type of users are involved in it?",
placeholder=f"Feature {i + 1}:\n...",
height=100,
key=f'new_feature_{i}'
)
features_list.append(feature)
# Button to add more features
if st.button("Add Feature"):
st.session_state.num_features += 1
# joining the input of features_list into a string.
answer3 = '\n'.join(features_list)
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.
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.
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.
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.