I saw a question on StackOverflow that I wanted to answer and I stumbled upon following problem.
Suppose you want to restrict the user input from the st.text_input widget. E.g. the user can only type in strings. How would you do that?
My attempt:
import streamlit as st
def main():
st.title("Only allow text example")
#only allow strings
text = st.text_input('Type something', value=str)
#conditional statement to check if the input is a string
if type(text) == str:
st.write(text + ' ' + ' ...works as its a string')
else:
type(text) != str
st.write("Please enter a string")
if __name__ == "__main__":
main()
If I type in 1234 as the input it outputs …1234… works as its a string.
I read the documentation for this widget
docs for st.input_text → st.text_input - Streamlit Docs
…and I know the type argument is good for hiding the text input but I don’t know what the value argument is doing. I feel like by default all that you type into the widget is a string.
Wrapping everything up into a str() is also not advisable.
text = str(st.text_input('Type something', value=str))