Check the user input type using python and streamlit

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")

You probably need to post a better toy example - this one doesn’t work.

What are you even testing for type? The columns of the dataframe? How does that relate to the user input?

Yes you are right the logic is wrong
Whay i need is to check the type of user input regardless the columns types.

How can this be done ??

So. The answer is almost certainly yes.

However, there is a big risk of an X Y problem when answering a question like this.

What are you actually trying to achieve here? Try to explain to us the reason that you are asking this question in the first place - there may well be a different approach that would work better for you.

Python is dynamically typed for good reasons…

I have a text_input that take the user input than i run a query on the dataframe in order to filter the data and return matching records based on the user input
However the dataframe is consist of 4 columns
Id int
First object
Last object
Raking int

If the user enter a string (means that the query must filter the data based on object columns ) the return is correct
But if the user enter a number (means that the query must filter the data based on int column)the return is wrong because it consider the input as string.

Ok. So you can’t know in advance what the user will enter, and you want them to be able to enter either a number or a string?

As you have discovered, the input widget will just return a string.

In python, strings have methods, and you can call isnumeric() on the string to determine if it only contains numeric characters. There are other methods isdigit() or isdecimal() that you might with to consider.

If this test is true, you can cast it to an integer or a float depending on what type of number you’re after - just use int() or float() for this.

Hopefully that’s enough to get you on your way!

So bases on your reply i can check if variable regular search term isnumiric()
So the statement becomes:

st_input_update = st.number_input if isnumeric(regular_search_term) else st.text_input

with col1_search:                          
          regular_search_term=st_input_update("Enter Search Term")

No, not really. You can’t test what the user input is before you’ve set up the widget to get the user input in the first place.

I would try something more like

user_input = st.text_input()

if user_input.isnumeric():
    user_input = int(user_input)
    Do stuff with a numeric input

else:
    Do stuff with text-based input
1 Like

This was what iam looking for thank you

No problem, best of luck with the project :+1: