Inputting text and numbers
Another extremely useful function in our web application is inputting, which is the process of entering some information. In this case, we have many widgets available out of the box.
In the text_input widget, we only have to specify a label or caption and a placeholder – very easy!
Name = st.text_input(“Your Name”, “Write something…”)
st.text(name)
Everything we write will be saved in the name variable and printed on the screen thanks to st.text():
Figure 3.22: The st.text_input widget
In the same easy way, we can also input numbers. This time, it’s possible to write a number directly or use the + and – icons to increase or decrease it just using st.number_input:
Age = st.number_input(“Input a number”)
Here’s the output:
Figure 3.23: The st.number_input widget
Moving back to text, to input text on more than one line, we can use the text_area widget, like so:
message = st.text_area(“Your Message”, “Write something…”)
As we can see, this time, a wider text area will be displayed:
Figure 3.24: The st.text_area widget
text_area is the perfect tool when we need to input long text, and it can be configured according to our needs.
Slider
Another wonderful input widget is the slider, where we just need to specify a starting and an ending value to have a nice selector on the screen. The syntax is extremely easy:
select_val = st.slider(“Select a Value”, 1, 10)
Here’s the output:
Figure 3.25: The st.slider widget
The slider is quite nice to see and very effective in pursuing its task.
Balloons
A very nice widget is balloons. Think of a situation where you want to show happiness after something good hashappened. In this case, you can use it by clicking on a button, as shown in the following code:
if st.button(“Balloons”):
st.balloons()
See what happens after clicking the button!