get a list of values from user in streamlit app
Question:
I want to get a list of values from user, but I have no idea how to perform.
I tried with code as follows but this is not the correct way.
import streamlit as st
collect_numbers = lambda x : [str(x)]
numbers = st.text_input("PLease enter numbers")
st.write(collect_numbers(numbers))
Answers:
you can use, c variable is numbers to input, you can change:
list = []
c= 5
while c > 0:
x = input("PLease enter numbers")
list.append(x)
print(x)
c = c - 1
list
As my understanding from your problem you want a list of inputs in output.
So for that I have done some code for you. You can also try this lines of code for get similar type of output as list.
Code :
numbers = []
while True:
num = input("Enter a number (or press Enter to stop): ")
if num == "":
break
numbers.append(num)
print(numbers)
Execution Code :
Enter a number (or press Enter to stop): 1
Enter a number (or press Enter to stop): 2
Enter a number (or press Enter to stop): 3
Enter a number (or press Enter to stop): 6
Enter a number (or press Enter to stop): 4
Enter a number (or press Enter to stop): // here I stop the program and get output in next line
['1', '2', '3', '6', '4']
Need more info about what you are trying to do. If you need some specified values you can try using diferent text inputs as columns. You can display as many columns as you want, the following code is an easy example.
col1, col2 = st.columns(2)
with col1:
val1 = st.text_input("Enter first value")
with col2:
val2 = st.text_input("Enter second value")
list_val = [val1,val2]
Also you can request the user to enter the values as coma separated, then use that to make the list that you want
I’d suggest using a tuple, list or iterable. For example,
import streamlit as st # 1.18.1
numbers = [st.number_input(f"Enter number {i}") for i in range(4)]
st.write(numbers)
Or text:
prompts = "Name", "Age", "Favorite animal", "Birth date", "Favorite color"
answers = [st.text_input(x) for x in prompts]
for prompt, answer in zip(prompts, answers):
st.write(f"Your {prompt.lower()} is {answer}")
Or a mix:
prompts = "Name", "Age", "Favorite animal", "Birth date", "Favorite color"
inputs = (
st.text_input,
lambda p: st.number_input(p, value=0),
st.text_input,
st.date_input,
st.color_picker
)
answers = [fn(p) for p, fn in zip(prompts, inputs)]
for prompt, answer in zip(prompts, answers):
st.write(f"Your {prompt.lower()} is {answer}")
You could put this in a form if you want to handle a final submission after all inputs are filled:
prompts = {
"Name": st.text_input,
"Age": lambda p: st.number_input(p, value=0),
"Favorite animal": st.text_input,
"Birth date": st.date_input,
"Favorite color": st.color_picker,
}
with st.form("info"):
answers = [fn(p) for p, fn in prompts.items()]
if st.form_submit_button("Submit"):
if all(answers):
for prompt, answer in zip(prompts, answers):
st.write(f"Your {prompt.lower()} is {answer}")
else:
st.error("Please fill all fields")
Streamlit has pretty powerful form handling, so these can be extended much further, but I don’t want to speculate too much on your use case.
I want to get a list of values from user, but I have no idea how to perform.
I tried with code as follows but this is not the correct way.
import streamlit as st
collect_numbers = lambda x : [str(x)]
numbers = st.text_input("PLease enter numbers")
st.write(collect_numbers(numbers))
you can use, c variable is numbers to input, you can change:
list = []
c= 5
while c > 0:
x = input("PLease enter numbers")
list.append(x)
print(x)
c = c - 1
list
As my understanding from your problem you want a list of inputs in output.
So for that I have done some code for you. You can also try this lines of code for get similar type of output as list.
Code :
numbers = []
while True:
num = input("Enter a number (or press Enter to stop): ")
if num == "":
break
numbers.append(num)
print(numbers)
Execution Code :
Enter a number (or press Enter to stop): 1
Enter a number (or press Enter to stop): 2
Enter a number (or press Enter to stop): 3
Enter a number (or press Enter to stop): 6
Enter a number (or press Enter to stop): 4
Enter a number (or press Enter to stop): // here I stop the program and get output in next line
['1', '2', '3', '6', '4']
Need more info about what you are trying to do. If you need some specified values you can try using diferent text inputs as columns. You can display as many columns as you want, the following code is an easy example.
col1, col2 = st.columns(2)
with col1:
val1 = st.text_input("Enter first value")
with col2:
val2 = st.text_input("Enter second value")
list_val = [val1,val2]
Also you can request the user to enter the values as coma separated, then use that to make the list that you want
I’d suggest using a tuple, list or iterable. For example,
import streamlit as st # 1.18.1
numbers = [st.number_input(f"Enter number {i}") for i in range(4)]
st.write(numbers)
Or text:
prompts = "Name", "Age", "Favorite animal", "Birth date", "Favorite color"
answers = [st.text_input(x) for x in prompts]
for prompt, answer in zip(prompts, answers):
st.write(f"Your {prompt.lower()} is {answer}")
Or a mix:
prompts = "Name", "Age", "Favorite animal", "Birth date", "Favorite color"
inputs = (
st.text_input,
lambda p: st.number_input(p, value=0),
st.text_input,
st.date_input,
st.color_picker
)
answers = [fn(p) for p, fn in zip(prompts, inputs)]
for prompt, answer in zip(prompts, answers):
st.write(f"Your {prompt.lower()} is {answer}")
You could put this in a form if you want to handle a final submission after all inputs are filled:
prompts = {
"Name": st.text_input,
"Age": lambda p: st.number_input(p, value=0),
"Favorite animal": st.text_input,
"Birth date": st.date_input,
"Favorite color": st.color_picker,
}
with st.form("info"):
answers = [fn(p) for p, fn in prompts.items()]
if st.form_submit_button("Submit"):
if all(answers):
for prompt, answer in zip(prompts, answers):
st.write(f"Your {prompt.lower()} is {answer}")
else:
st.error("Please fill all fields")
Streamlit has pretty powerful form handling, so these can be extended much further, but I don’t want to speculate too much on your use case.