EP6. Receiving input from the user
We can also receive the date and time as input too!!!!
Hey Guys!! Welcome back to the EP6 on our basics of Streamlit series. Today in this article, we are going to look at how to receive input from the user. The input that we are going to receive can be either a text, a number, a bunch of text lines, date, time, and even passwords too, and without any further ado, let’s get started!!!
As usual, we will start with import all the required packages. As we are dealing with streamlit we will import streamlit.
import streamlit as st
To receive a text input we will use the function text_input() and this function will take one mandatory parameter that is the question-like text. For example, “Enter your Last name: ”
fn = st.text_input("Enter your first name: ")
You can also set the limit of the character you want the user to give in as the input. To do that we will use the parameter, max_chars. Suppose when we set the parameter max_chars to 20, then the user cannot enter the total characters greater than 20.
fn = st.text_input("Enter your first name: ", max_chars = 10)
By using the text_input() we cannot receive multi-line input from the user. Let’s say you want to receive the user’s address as the input in that case text_input() will not be much useful rather we can use the function text_area().
st.text_area("Give your feedback")
The number_input function helps users to receive the numbers as input from the user. By default, it takes a single parameter that is the question-like text. For example, “Enter your age: ”.
st.number_input(“Enter your age”)
By default, the numbers will increment at the rate of 0.01. But when u pass another value, inside the function then it will determine at what rate the values must change. When we pass an integer then the values increment at the rate of 1, when we pass a float then the values increment at the rate of 0.01.
st.number_input(“Enter your age”, 5)
#values will increase at the rate of +1/-1st.number_input(“Enter your age”, 5.5)
#values will increase at the rate of +0.01/-0.01
You can also add another value which is a positional argument and it will act as the maximum value.
st.number_input(“Enter your age”, 5, 100) # 100 is the max value, any other max value u enter will throw an error.
You can also add another value which is a positional argument and it will act as the default value.
st.number_input(“Enter your age”, 5, 100, 77) # Here 77 will act as the default value.
You can also add another value which is a positional argument and it will act as the step value.
st.number_input(“Enter your age”, 5, 100, 75, 5)
Here it is not that it will increase only in the range 5,10,15…. Suppose when u set ur value to be 17 and then when you increase the value then the values will be incremented by 17,22,27,32….
Date and Time Input
We can also receive the date and time as input from the user. This is one of the most interesting features that streamlit provides us.
# datest.date_input("When is your next flight")# timest.time_input("When is your class time")
We can also get the input as a password from the user. This can be done by using the ‘type’ parameter in our text_input().
and my passwords will look like
password = st.text_input("Enter your Password", type = "password")
With this, we are concluding this article. And today in this article we looked at how to add the media file in our streamlit.
Don’t forget to leave your responses.✌
Everyone stay tuned!! To get my stories in your mailbox kindly subscribe to my newsletter. Thank you for reading! Do not forget to give your claps and to share your responses and share them with a friend.
This article was published earlier on Fittechie.in