How to clear a number after entering a number_input

hi. I have two questions.

I’m making an app that converts inches to millimeters.
If you look at the screenshot, it is inconvenient because the operation of entering a number, hitting the Enter key, deleting the existing number, and entering another number is repeated.
I was wondering how to make the input field blank before I press Enter and enter another number.

Is there a way to see the result rounded to 2 decimal places?

Thank you.

  1. You cannot do that with numeric_input. You can use text_input instead and handle the conversion to number by yourself.

  2. Take a look at the style attribute of DataFrame, in particular its format method. You can pass a Styler to st.dataframe() instead of a DataFrame.

Thank you. Goyo.

Are you talking about the on_change and on_click functions of the text?
I’d like to work around this by using the Enter key instead of using the submit button.
Please advise.

No, I don’t think you need callbacks or forms.

I solved this problem.

Please tell the community how you did that, please?

Cheers,
Arvindra

2

Hi @asehmi

This method was discovered by chance,
My solution is ‘num = st.number_input(“:sunglasses:”,value=1.0,format="%.1f ")’.
%f, %0.1d, %0.2f don’t matter.
The key is that you need a ‘space’ after the ‘f’.
This may be a coincidence, but for me it’s very useful.

  • When ‘space’ is removed, only the specified decimal point is kept and the rest is removed.

Nice you got it working. I can’t poke around as you haven’t posted the text. Please refer to this article next time you post a question as it helps the community and responders help you (e.g. point #3): Streamlit Components: how to post a question in the Streamlit forum

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.