Home > Back-end >  check the user input type using python and streamlit
check the user input type using python and streamlit

Time:10-23

I want to check the user input if its numeric or string.

The problem is that the if statement always take the user input as string even if the input is numeric its converted by default into a string.

How to fix this problem ?

all_columns = df.columns.tolist()
st_input_update = st.number_input if is_numeric_dtype(all_columns) else st.text_input
with col1_search:
    regular_search_term=st_input_update("Enter Search Term")

CodePudding user response:

The argument to is_numeric_dtype() should either be a type or a pandas array. You gave it a list of column names, which is neither.

Use df.dtypes to get an array of datatypes.

st_input_update = st.number_input if is_numeric_dtype(df.dtypes) else st.text_input

CodePudding user response:

user_input = input("Enter something:")
try:
    x = int(user_input)
    print("Is a number")
except ValueError:
    print("That's not an int!")
  • Related