Mtalha916/UnitConversion132
0
1import streamlit as st2 3# Title with designer name4st.markdown("<h1 style='text-align: center; color: pink;'>Designed by Professional Coder Muhammad Talha 132</h1>", unsafe_allow_html=True)5st.title("Unit Converter")6 7# Conversion functions8def convert_mass(value, from_unit, to_unit):9 units = {"kg": 1, "g": 1000, "lb": 2.20462, "oz": 35.274}10 return value * (units[to_unit] / units[from_unit])11 12def convert_length(value, from_unit, to_unit):13 units = {"m": 1, "cm": 100, "mm": 1000, "inch": 39.3701, "ft": 3.28084}14 return value * (units[to_unit] / units[from_unit])15 16def convert_temperature(value, from_unit, to_unit):17 if from_unit == to_unit:18 return value19 elif from_unit == "C" and to_unit == "F":20 return (value * 9/5) + 3221 elif from_unit == "F" and to_unit == "C":22 return (value - 32) * 5/923 elif from_unit == "C" and to_unit == "K":24 return value + 273.1525 elif from_unit == "K" and to_unit == "C":26 return value - 273.1527 elif from_unit == "F" and to_unit == "K":28 return (value - 32) * 5/9 + 273.1529 elif from_unit == "K" and to_unit == "F":30 return (value - 273.15) * 9/5 + 3231 32def convert_pressure(value, from_unit, to_unit):33 units = {"Pa": 1, "kPa": 0.001, "bar": 1e-5, "psi": 0.000145038}34 return value * (units[to_unit] / units[from_unit])35 36def convert_energy(value, from_unit, to_unit):37 units = {"J": 1, "kJ": 0.001, "cal": 0.239006, "kcal": 0.000239006}38 return value * (units[to_unit] / units[from_unit])39 40# Streamlit UI41category = st.selectbox("Choose a category", ["Mass", "Length", "Temperature", "Pressure", "Energy"])42 43if category == "Mass":44 units = ["kg", "g", "lb", "oz"]45elif category == "Length":46 units = ["m", "cm", "mm", "inch", "ft"]47elif category == "Temperature":48 units = ["C", "F", "K"]49elif category == "Pressure":50 units = ["Pa", "kPa", "bar", "psi"]51elif category == "Energy":52 units = ["J", "kJ", "cal", "kcal"]53 54value = st.number_input("Enter value", min_value=0.0, format="%.5f")55from_unit = st.selectbox("From", units)56to_unit = st.selectbox("To", units)57 58if st.button("Convert"):59 if category == "Mass":60 result = convert_mass(value, from_unit, to_unit)61 elif category == "Length":62 result = convert_length(value, from_unit, to_unit)63 elif category == "Temperature":64 result = convert_temperature(value, from_unit, to_unit)65 elif category == "Pressure":66 result = convert_pressure(value, from_unit, to_unit)67 elif category == "Energy":68 result = convert_energy(value, from_unit, to_unit)69 70 st.success(f"Converted Value: {result:.5f} {to_unit}")71 