Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
# Conversion functions
|
| 4 |
+
def length_converter(value, from_unit, to_unit):
|
| 5 |
+
length_units = {
|
| 6 |
+
"meter": 1.0,
|
| 7 |
+
"kilometer": 0.001,
|
| 8 |
+
"centimeter": 100.0,
|
| 9 |
+
"millimeter": 1000.0,
|
| 10 |
+
"mile": 0.000621371,
|
| 11 |
+
"yard": 1.09361,
|
| 12 |
+
"foot": 3.28084,
|
| 13 |
+
"inch": 39.3701
|
| 14 |
+
}
|
| 15 |
+
return value * (length_units[to_unit] / length_units[from_unit])
|
| 16 |
+
|
| 17 |
+
def weight_converter(value, from_unit, to_unit):
|
| 18 |
+
weight_units = {
|
| 19 |
+
"kilogram": 1.0,
|
| 20 |
+
"gram": 1000.0,
|
| 21 |
+
"milligram": 1000000.0,
|
| 22 |
+
"pound": 2.20462,
|
| 23 |
+
"ounce": 35.274
|
| 24 |
+
}
|
| 25 |
+
return value * (weight_units[to_unit] / weight_units[from_unit])
|
| 26 |
+
|
| 27 |
+
def temperature_converter(value, from_unit, to_unit):
|
| 28 |
+
if from_unit == to_unit:
|
| 29 |
+
return value
|
| 30 |
+
if from_unit == "Celsius":
|
| 31 |
+
return value * 9/5 + 32 if to_unit == "Fahrenheit" else value + 273.15
|
| 32 |
+
if from_unit == "Fahrenheit":
|
| 33 |
+
return (value - 32) * 5/9 if to_unit == "Celsius" else (value - 32) * 5/9 + 273.15
|
| 34 |
+
if from_unit == "Kelvin":
|
| 35 |
+
return value - 273.15 if to_unit == "Celsius" else (value - 273.15) * 9/5 + 32
|
| 36 |
+
|
| 37 |
+
def time_converter(value, from_unit, to_unit):
|
| 38 |
+
time_units = {
|
| 39 |
+
"second": 1,
|
| 40 |
+
"minute": 1/60,
|
| 41 |
+
"hour": 1/3600,
|
| 42 |
+
"day": 1/86400
|
| 43 |
+
}
|
| 44 |
+
return value * (time_units[to_unit] / time_units[from_unit])
|
| 45 |
+
|
| 46 |
+
# Streamlit UI
|
| 47 |
+
st.title("Unit Converter")
|
| 48 |
+
|
| 49 |
+
category = st.selectbox("Select Category", ["Length", "Weight", "Temperature", "Time"])
|
| 50 |
+
|
| 51 |
+
if category == "Length":
|
| 52 |
+
units = ["meter", "kilometer", "centimeter", "millimeter", "mile", "yard", "foot", "inch"]
|
| 53 |
+
value = st.number_input("Enter Value", value=1.0)
|
| 54 |
+
from_unit = st.selectbox("From", units)
|
| 55 |
+
to_unit = st.selectbox("To", units)
|
| 56 |
+
if st.button("Convert"):
|
| 57 |
+
result = length_converter(value, from_unit, to_unit)
|
| 58 |
+
st.success(f"{value} {from_unit} = {result:.4f} {to_unit}")
|
| 59 |
+
|
| 60 |
+
elif category == "Weight":
|
| 61 |
+
units = ["kilogram", "gram", "milligram", "pound", "ounce"]
|
| 62 |
+
value = st.number_input("Enter Value", value=1.0)
|
| 63 |
+
from_unit = st.selectbox("From", units)
|
| 64 |
+
to_unit = st.selectbox("To", units)
|
| 65 |
+
if st.button("Convert"):
|
| 66 |
+
result = weight_converter(value, from_unit, to_unit)
|
| 67 |
+
st.success(f"{value} {from_unit} = {result:.4f} {to_unit}")
|
| 68 |
+
|
| 69 |
+
elif category == "Temperature":
|
| 70 |
+
units = ["Celsius", "Fahrenheit", "Kelvin"]
|
| 71 |
+
value = st.number_input("Enter Value", value=0.0)
|
| 72 |
+
from_unit = st.selectbox("From", units)
|
| 73 |
+
to_unit = st.selectbox("To", units)
|
| 74 |
+
if st.button("Convert"):
|
| 75 |
+
result = temperature_converter(value, from_unit, to_unit)
|
| 76 |
+
st.success(f"{value} {from_unit} = {result:.2f} {to_unit}")
|
| 77 |
+
|
| 78 |
+
elif category == "Time":
|
| 79 |
+
units = ["second", "minute", "hour", "day"]
|
| 80 |
+
value = st.number_input("Enter Value", value=1.0)
|
| 81 |
+
from_unit = st.selectbox("From", units)
|
| 82 |
+
to_unit = st.selectbox("To", units)
|
| 83 |
+
if st.button("Convert"):
|
| 84 |
+
result = time_converter(value, from_unit, to_unit)
|
| 85 |
+
st.success(f"{value} {from_unit} = {result:.4f} {to_unit}")
|