Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,40 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
def convert_temperature(value, from_unit, to_unit):
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
else:
|
| 9 |
-
|
| 10 |
-
elif from_unit == "Fahrenheit":
|
| 11 |
-
if to_unit == "Celsius":
|
| 12 |
-
return (value - 32) * 5/9
|
| 13 |
-
else:
|
| 14 |
-
return value # No conversion needed for Fahrenheit to Fahrenheit
|
| 15 |
-
else:
|
| 16 |
-
raise ValueError("Invalid temperature unit")
|
| 17 |
|
| 18 |
st.title("Temperature Converter")
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
from_unit = st.selectbox("From Unit:", options=
|
| 26 |
-
to_unit = st.selectbox("To Unit:", options=
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
try:
|
| 31 |
-
converted_value = convert_temperature(temperature_value, from_unit, to_unit)
|
| 32 |
-
st.success(f"{temperature_value} {from_unit} is equal to {converted_value:.2f} {to_unit}") # Display result with 2 decimal places
|
| 33 |
-
except ValueError as e:
|
| 34 |
-
st.error(f"Error: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
def convert_temperature(value, from_unit, to_unit):
|
| 4 |
+
"""Converts temperature between Celsius, Fahrenheit, and Kelvin."""
|
| 5 |
+
if from_unit == "Celsius":
|
| 6 |
+
if to_unit == "Fahrenheit":
|
| 7 |
+
return (value * 9/5) + 32
|
| 8 |
+
elif to_unit == "Kelvin":
|
| 9 |
+
return value + 273.15
|
| 10 |
+
else:
|
| 11 |
+
return value # No conversion needed for Celsius to Celsius
|
| 12 |
+
elif from_unit == "Fahrenheit":
|
| 13 |
+
if to_unit == "Celsius":
|
| 14 |
+
return (value - 32) * 5/9
|
| 15 |
+
elif to_unit == "Kelvin":
|
| 16 |
+
return (value + 459.67) * 5/9
|
| 17 |
+
else:
|
| 18 |
+
return value # No conversion needed for Fahrenheit to Fahrenheit
|
| 19 |
+
elif from_unit == "Kelvin":
|
| 20 |
+
if to_unit == "Celsius":
|
| 21 |
+
return value - 273.15
|
| 22 |
+
elif to_unit == "Fahrenheit":
|
| 23 |
+
return (value * 9/5) - 459.67
|
| 24 |
+
else:
|
| 25 |
+
return value # No conversion needed for Kelvin to Kelvin
|
| 26 |
else:
|
| 27 |
+
raise ValueError("Invalid temperature unit")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
st.title("Temperature Converter")
|
| 30 |
|
| 31 |
+
with st.container():
|
| 32 |
+
col1, col2 = st.columns(2)
|
| 33 |
+
with col1:
|
| 34 |
+
temperature_value = st.number_input("Enter temperature:", min_value=-273.15, step=0.1)
|
| 35 |
+
with col2:
|
| 36 |
+
from_unit = st.selectbox("From Unit:", options=["Celsius", "Fahrenheit", "Kelvin"])
|
| 37 |
+
to_unit = st.selectbox("To Unit:", options=["Celsius", "Fahrenheit", "Kelvin"])
|
| 38 |
|
| 39 |
+
converted_value = convert_temperature(temperature_value, from_unit, to_unit)
|
| 40 |
+
st.write(f"{temperature_value} {from_unit} is equal to {converted_value:.2f} {to_unit}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|