A single-page web calculator with a Flask backend and vanilla HTML/CSS/JS frontend. Mathematical expressions are evaluated server-side using Python's ast module — never eval().
- Python 3 + Flask — two-route backend (
/and/calculate) - AST-based safe evaluator — whitelist-only operator dispatch, no arbitrary code execution
- Vanilla JS — event delegation, keyboard support, fetch API
Browser ──POST /calculate──▶ Flask (app.py)
◀──JSON { result }──
The frontend sends the expression as JSON; Flask parses it through safe_eval, which walks the AST and only allows nodes present in the SAFE_OPERATORS dispatch table. Any other construct (function calls, imports, attribute access) raises ValueError.
cd pycalc
pip install -r requirements.txt
python app.pyOpen http://localhost:5001 in your browser.
- Flask over Django — two routes, no database, no auth; Django would be overkill
astovereval()— prevents arbitrary code execution even in a local project- Server-side evaluation — the frontend only builds the string and displays the result