A basic and universal unit converter with python backend with Flash API and a React frontend.
This project is a Universal Unit Converter that marries a lightweight yet powerful Python conversion engine with a modern, responsive React frontend. It supports conversions across nine major categories—Length, Area, Volume, Mass, Time, Velocity, Acceleration, Temperature and Data Storage—providing both the numeric result and a human-readable explanation (multiplication factor or formula). The backend is a Flask API that exposes the conversion logic as REST endpoints, and the frontend is built with Create React App, styled by Tailwind CSS & DaisyUI, offering an intuitive UI with real-time validation, loading states and nicely formatted unit labels (including superscripts for exponents and division symbols).
unit_converter.pyUnitConverter class with a single _factors registry mapping every unit (and its aliases) to a
base-unit factorconvert_length, convert_area, … convert_acceleration) that
normalize units, apply factor arithmetic, and handle special cases (e.g. area aliases “sq m” → m²)convert_temperature which walks through an intermediate Celsius step to handle Fahrenheit and Kelvin
offsetsget_conversion_explanation() that returns either a formula (for temperature) or “Multiply by X” /
“Divide by Y” for other quantitiesflask-corsGET /units – Returns JSON of available units per category (sorted alphabetically, except Data Storage
sorted by size)POST /convert – Accepts { type, value, fromUnit, toUnit }, invokes the converter, and returns {
result, explanation } or an errorApp.js – Manages state (types, units, input, result, loading/error flags), fetches /units, posts
/convert, and renders the form & result cardUnitDisplay.js – Parses unit strings like m2, cm3, km/h and injects <sup> tags for
exponents, handles division slashespip install flask flask-corsunittest suite in test/test_unit_converter.pygit clone https://github.com/your-username/universal-unit-converter.git
cd universal-unit-converter
pip install flask flask-cors
python api.py
# The API will listen on http://localhost:5001
cd unit-converter-frontend
npm install
npm start
# Opens http://localhost:3000 in your browser
python -m unittest discover
Temperature: displays the formula (e.g. “°F = (°C × 9/5) + 32”)
Others: shows “Multiply by X” or “Divide by Y” or “Units are equivalent.”
_factors: maps every unit and alias to a base-unit factor (e.g. 'km': 1000.0, 'sq m': 1.0)_convert(): lowercases units, validates them, applies factor arithmetic_convert() or implement special logic (convert_area,
convert_temperature)get_conversion_explanation():
For temperature, returns standard formulas
For other types, computes result÷input to determine “Multiply by” or “Divide by”
UnitConverter instance/units: iterates over converter._factors, sorts keys, capitalizes type names, injects a hard-coded
Temperature list/convert: parses JSON, maps type to conversion method, catches errors, returns structured JSONApp.js
useEffect to fetch unit lists on mount
State hooks for inputs, result, loading, errors
handleConvert() posts JSON to /convert and displays the response
UnitDisplay.js
<sup>, reassembles division partsDaisyUI “card” layout, form controls, alerts, spinners
Responsive grid for input/result panels
test_unit_converter.py
Validates every category’s conversions, edge cases (zero, negative, large/small magnitudes)
Verifies get_conversion_explanation() for both formula and factor-based outputs
Problem: Supporting dozens of units (and their many aliases) across multiple physical quantities without repetitive code. Solution: A single _factors dictionary keyed by category, enriched with programmatic aliases (e.g. area “sq m” → “m2”). _convert() handles all arithmetic uniformly. Why: Minimizes duplication, centralizes validation and error messages. Future: Load factor data from a schema or external JSON to allow user-defined units.
Problem: Celsius, Fahrenheit and Kelvin don’t share a purely multiplicative relationship; they require additive offsets and different formulas. Solution: Convert any input to Celsius first, then to target; get_conversion_explanation() returns standard formulas (e.g. “°C = (°F − 32) × 5/9”). Why: Keeps temperature logic isolated and expressive. Future: Support additional temperature scales (Rankine, Réaumur) and display worked-out examples.
Problem: Units like “m2”, “cm3” or “km/h” need proper typographic treatment (superscripts, division signs) in a JSX environment. Solution: UnitDisplay.js parses the string with regex, injects for digits, and correctly reassembles parts around “/”. Why: Enhances readability and professionalism of the UI. Future: Extend to handle compound units (e.g. “N·m”, “J/s²”) and locale-specific formatting.
