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.
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.
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