Input Widgets with Streamlit

Number Input

In addition to sliders, you can have users actually type in numbers directly using the number_input widget. Just as for the slider widget, you can set it up to accept integers or floats depending on how you specify the input parameters.

This widget allows users to increment or decrement the values using buttons. This change will happen in terms of the steps specified. If no step is specified, the change will be by 1 for integers and by 0.01 for floats.

For floating point numbers, you can type in a value with more than two decimal places. The output will display all decimal places, even though the input window will round it to two decimal places.

number_input_selection = st.number_input(“Enter a number”, min_value = -10, max_value = 10, value = 0, key = " Number Input Integer") st.write(number_input_selection, type(number_input_selection)) number_input_selection = st.number_input(“Enter a number”, min_value = -1.0, max_value = 1.0, value = 0.0, key = “Number Input Float”) st.write(number_input_selection, type(number_input_selection))

Text Input

There are two widgets for text input. These are text_input and text_area.

text_input allows just one line of text. It is best used in cases where just a single line of text is required such as names of entities. Termination of text entry is done by pressing the Enter key on the keyboard.

text_area allows for multi-line input of text and would be used in cases where feedback is requested. Termination of text entry is done by pressing Ctrl+Enter on the keyboard.

text_input_selection = st.text_input(“From what you have learnt today, give an example of a pest”) st.write(text_input_selection) text_area_selection = st.text_area(“Give a summary of what you have learnt about insects today”) st.write(text_area_selection)

The following figure demonstrates the slider, number and text input widgets.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy