Introduction
Converting units like meters to feet, kilograms to pounds, or Celsius to Fahrenheit is a common daily task. Instead of searching online, why not build your own custom Unit Converter? Date math is another common conversion need — the age calculator app covers that side.
In this guide, you will build an interactive Unit Converter using Python and Streamlit. The application supports multiple unit categories, performs instant math calculations in the background, and outputs results in a clean card layout.
> 🎬 Watch the Full Video Tutorial:
> Watch the masterclass on YouTube: PYTHON MASTER Shares Top Secrets for Building Unit Converter Apps!
Prerequisites
To get started, make sure you have Streamlit installed:
pip install streamlitStep 1: Create the Unit Converter App
Create a file named unit_converter.py and paste the following Python code:
import streamlit as st
st.set_page_config(page_title="Unit Converter", page_icon="⚖️", layout="centered")
st.title("⚖️ Smart Unit Converter")
# Choose Category
category = st.selectbox(
"Select Category:",
["Length (Distance)", "Weight (Mass)", "Temperature"]
)
# Conversion formulas and calculations
if category == "Length (Distance)":
st.subheader("📏 Length Conversion")
units = ["Meters", "Kilometers", "Feet", "Miles"]
from_unit = st.selectbox("From:", units, key="len_from")
to_unit = st.selectbox("To:", units, key="len_to")
value = st.number_input("Enter Value:", min_value=0.0, value=1.0, step=1.0)
# Length conversions relative to Meters
meters_map = {
"Meters": 1.0,
"Kilometers": 1000.0,
"Feet": 0.3048,
"Miles": 1609.34
}
# Convert to Meters first, then to target unit
result = (value * meters_map[from_unit]) / meters_map[to_unit]
st.success(f"✨ **{value} {from_unit}** = **{result:.4f} {to_unit}**")
elif category == "Weight (Mass)":
st.subheader("⚖️ Weight Conversion")
units = ["Grams", "Kilograms", "Pounds", "Ounces"]
from_unit = st.selectbox("From:", units, key="wt_from")
to_unit = st.selectbox("To:", units, key="wt_to")
value = st.number_input("Enter Value:", min_value=0.0, value=1.0, step=1.0)
# Weight conversions relative to Grams
grams_map = {
"Grams": 1.0,
"Kilograms": 1000.0,
"Pounds": 453.592,
"Ounces": 28.3495
}
result = (value * grams_map[from_unit]) / grams_map[to_unit]
st.success(f"✨ **{value} {from_unit}** = **{result:.4f} {to_unit}**")
elif category == "Temperature":
st.subheader("🌡️ Temperature Conversion")
units = ["Celsius", "Fahrenheit", "Kelvin"]
from_unit = st.selectbox("From:", units, key="temp_from")
to_unit = st.selectbox("To:", units, key="temp_to")
value = st.number_input("Enter Value:", value=0.0, step=1.0)
# Temperature calculation logic
def convert_temp(val, from_u, to_u):
if from_u == to_u:
return val
# Convert from origin to Celsius
c = val
if from_u == "Fahrenheit":
c = (val - 32) * 5/9
elif from_u == "Kelvin":
c = val - 273.15
# Convert Celsius to target
if to_u == "Celsius":
return c
elif to_u == "Fahrenheit":
return (c * 9/5) + 32
elif to_u == "Kelvin":
return c + 273.15
result = convert_temp(value, from_unit, to_unit)
st.success(f"✨ **{value}° {from_unit}** = **{result:.2f}° {to_unit}**")Step 2: Run the Application
To run the server, execute this command in your terminal:
streamlit run unit_converter.pyHow It Works
A unit converter is really a dictionary problem. Length and weight conversions are *linear*: every unit is a factor relative to a base (meters, grams), so converting is value * from_factor / to_factor. One nested dictionary of factors covers every unit pair — adding a unit is a one-line change, no new code paths.
Temperature is the exception: Celsius, Fahrenheit, and Kelvin have *offsets*, not just factors. The clean solution is converting through a pivot (anything → Celsius → target) so you write the offset logic once instead of handling all 9 pairs.
The UI pattern is a sidebar st.selectbox for category, then two dependent selectboxes for from/to units populated from that category's dict, an st.number_input for value, and the result in st.metric or st.success. Because Streamlit reruns the script on every widget change, the conversion updates live with no button needed — instant feedback that makes the app feel native.
Key Concepts Covered
* `st.number_input()` — A standard input widget for entering floats or integers with precision increments.
* Category Mapping — We mapped length and weight to a common baseline unit (Meters & Grams) to keep conversion logic linear and clean.
What to Try Next
* Add more categories: Extend the application to support Speed (km/h, mph), Volume (Liters, Gallons), or Time (seconds, minutes, hours).
Common Errors & Fixes
value * factor.keys and reset, or derive units after the category selection.round(result, 6) or format strings, keeping full precision internally.set somewhere.FAQ
How do I add a new category like area or speed?
Add one entry to the categories dictionary with its unit factors — the rest of the app picks it up automatically.
Can it handle currency?
Not statically — rates change daily. Fetch live rates from an API (the weather app shows the request pattern) and cache them.
Why convert through a pivot for temperature?
One canonical path (unit → base → target) replaces nine special cases and makes the code impossible to get subtly wrong.