Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| # Conversion functions | |
| def length_converter(value, from_unit, to_unit): | |
| length_units = { | |
| "meter": 1.0, | |
| "kilometer": 0.001, | |
| "centimeter": 100.0, | |
| "millimeter": 1000.0, | |
| "mile": 0.000621371, | |
| "yard": 1.09361, | |
| "foot": 3.28084, | |
| "inch": 39.3701 | |
| } | |
| return value * (length_units[to_unit] / length_units[from_unit]) | |
| def weight_converter(value, from_unit, to_unit): | |
| weight_units = { | |
| "kilogram": 1.0, | |
| "gram": 1000.0, | |
| "milligram": 1000000.0, | |
| "pound": 2.20462, | |
| "ounce": 35.274 | |
| } | |
| return value * (weight_units[to_unit] / weight_units[from_unit]) | |
| def temperature_converter(value, from_unit, to_unit): | |
| if from_unit == to_unit: | |
| return value | |
| if from_unit == "Celsius": | |
| return value * 9/5 + 32 if to_unit == "Fahrenheit" else value + 273.15 | |
| if from_unit == "Fahrenheit": | |
| return (value - 32) * 5/9 if to_unit == "Celsius" else (value - 32) * 5/9 + 273.15 | |
| if from_unit == "Kelvin": | |
| return value - 273.15 if to_unit == "Celsius" else (value - 273.15) * 9/5 + 32 | |
| def time_converter(value, from_unit, to_unit): | |
| time_units = { | |
| "second": 1, | |
| "minute": 1/60, | |
| "hour": 1/3600, | |
| "day": 1/86400 | |
| } | |
| return value * (time_units[to_unit] / time_units[from_unit]) | |
| # Streamlit UI | |
| st.title("Unit Converter") | |
| category = st.selectbox("Select Category", ["Length", "Weight", "Temperature", "Time"]) | |
| if category == "Length": | |
| units = ["meter", "kilometer", "centimeter", "millimeter", "mile", "yard", "foot", "inch"] | |
| value = st.number_input("Enter Value", value=1.0) | |
| from_unit = st.selectbox("From", units) | |
| to_unit = st.selectbox("To", units) | |
| if st.button("Convert"): | |
| result = length_converter(value, from_unit, to_unit) | |
| st.success(f"{value} {from_unit} = {result:.4f} {to_unit}") | |
| elif category == "Weight": | |
| units = ["kilogram", "gram", "milligram", "pound", "ounce"] | |
| value = st.number_input("Enter Value", value=1.0) | |
| from_unit = st.selectbox("From", units) | |
| to_unit = st.selectbox("To", units) | |
| if st.button("Convert"): | |
| result = weight_converter(value, from_unit, to_unit) | |
| st.success(f"{value} {from_unit} = {result:.4f} {to_unit}") | |
| elif category == "Temperature": | |
| units = ["Celsius", "Fahrenheit", "Kelvin"] | |
| value = st.number_input("Enter Value", value=0.0) | |
| from_unit = st.selectbox("From", units) | |
| to_unit = st.selectbox("To", units) | |
| if st.button("Convert"): | |
| result = temperature_converter(value, from_unit, to_unit) | |
| st.success(f"{value} {from_unit} = {result:.2f} {to_unit}") | |
| elif category == "Time": | |
| units = ["second", "minute", "hour", "day"] | |
| value = st.number_input("Enter Value", value=1.0) | |
| from_unit = st.selectbox("From", units) | |
| to_unit = st.selectbox("To", units) | |
| if st.button("Convert"): | |
| result = time_converter(value, from_unit, to_unit) | |
| st.success(f"{value} {from_unit} = {result:.4f} {to_unit}") | |