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