Fix calculator bugs and add calculation history feature

#3

Summary

Fixes three bugs in gradio_calculator.py and adds a new Calculation History feature.

Errors Fixed

  • Input order mismatch (Fatal): Corrected calc_button.click argument 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 None guard and TypeError handling. gr.Number returns None for empty fields, which previously crashed the app.
  • Float noise (Cosmetic): Added a format_number() helper to strip float noise (e.g., 0.30000000000000004 now displays as 0.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

  1. Run pip install --upgrade gradio
  2. Run python gradio_calculator.py
  3. 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).

•
This comment has been hidden
ZeenathHamza changed pull request status to open
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment