Unit Converter

A basic and universal unit converter with python backend with Flash API and a React frontend.

Overview

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).

Tech Stack

Python Conversion Engine

Backend Server

Frontend Client

Build & Development Tools

Getting Started

1. Clone the repository

git clone https://github.com/your-username/universal-unit-converter.git
cd universal-unit-converter

2. Install and run the Python API

(Optional) create & activate a virtual environment

pip install flask flask-cors
python api.py
# The API will listen on http://localhost:5001

3. Launch the React frontend

cd unit-converter-frontend
npm install
npm start
# Opens http://localhost:3000 in your browser

4. (Optional) Run the Python test suite

python -m unittest discover

Usage

  1. Select a Conversion Type Choose from Length, Area, Volume, Mass, Time, Velocity, Acceleration, Temperature or Data.
  2. Enter a Value Type any number (integers, decimals, positives or negatives).
  3. Choose From/To Units Dropdowns auto-populate based on the selected category; exponents and slashes render correctly.
  4. Click “Convert” The app displays the numeric result and a concise explanation:
    • Temperature: displays the formula (e.g. “°F = (°C × 9/5) + 32”)

    • Others: shows “Multiply by X” or “Divide by Y” or “Units are equivalent.”

Architecture & Components

Python Core (unit_converter.py)

Backend Server (api.py)

Frontend Client (unit-converter-frontend/)

Testing (test/)

Challenges

1. Designing a Unified Conversion Registry

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.



2. Handling Temperature Offsets & Explanations

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.



3. Rendering Complex Unit Labels in React

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.

What I Learned

  1. Crafting a single, generic conversion engine that scales across multiple domains.
  2. Implementing offset-based conversions (temperature) cleanly alongside factor-based ones.
  3. Exposing Python logic via a RESTful Flask API with CORS for seamless frontend integration.
  4. Building a React UI with Tailwind CSS & DaisyUI, handling asynchronous fetches, loading spinners, and error states.
  5. Writing comprehensive unit tests in Python to guard against edge cases (zero, negatives, extremes).
  6. Parsing and rendering superscript exponents and division in JS/JSX with a minimal utility component.

Next Steps

Improvements I’d Like to Make

Screenshots

UI

View on GitHub