Fix calculator bugs and add calculation history feature
#3
by ZeenathHamza - opened
Summary
Fixes three bugs in gradio_calculator.py and adds a new Calculation History feature.
Errors Fixed
- Input order mismatch (Fatal): Corrected
calc_button.clickargument order. The original code passed inputs in the wrong order, causing every calculation to return "Please enter valid numbers". - Empty-input crash (Fatal): Added a
Noneguard andTypeErrorhandling.gr.NumberreturnsNonefor empty fields, which previously crashed the app. - Float noise (Cosmetic): Added a
format_number()helper to strip float noise (e.g.,0.30000000000000004now displays as0.3).
New Feature
- Calculation History: Every successful calculation is appended to a numbered list displayed in a collapsible panel.
- History is stored in
gr.State, meaning it is isolated per user session. - Added a Clear History button to reset the list.
How to test
- Run
pip install --upgrade gradio - Run
python gradio_calculator.py - Perform a few operations and click Clear History to verify.
Notes
Requires Python 3.12 or 3.13. (Gradio currently cannot be installed on Python 3.14 due to a pydantic-core dependency conflict).
ZeenathHamza changed pull request status to open