Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
def convert_temperature(value, from_unit, to_unit):
|
| 4 |
+
if from_unit == to_unit:
|
| 5 |
+
return value
|
| 6 |
+
|
| 7 |
+
if from_unit == "Celsius":
|
| 8 |
+
if to_unit == "Fahrenheit":
|
| 9 |
+
return (value * 9/5) + 32
|
| 10 |
+
elif to_unit == "Kelvin":
|
| 11 |
+
return value + 273.15
|
| 12 |
+
|
| 13 |
+
if from_unit == "Fahrenheit":
|
| 14 |
+
if to_unit == "Celsius":
|
| 15 |
+
return (value - 32) * 5/9
|
| 16 |
+
elif to_unit == "Kelvin":
|
| 17 |
+
return (value - 32) * 5/9 + 273.15
|
| 18 |
+
|
| 19 |
+
if from_unit == "Kelvin":
|
| 20 |
+
if to_unit == "Celsius":
|
| 21 |
+
return value - 273.15
|
| 22 |
+
elif to_unit == "Fahrenheit":
|
| 23 |
+
return (value - 273.15) * 9/5 + 32
|
| 24 |
+
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
# Streamlit UI
|
| 28 |
+
st.set_page_config(page_title="Temperature Converter", page_icon="🌡️", layout="centered")
|
| 29 |
+
st.title("🌡️ Temperature Converter")
|
| 30 |
+
st.markdown("### Convert temperatures between Celsius, Fahrenheit, and Kelvin with ease!")
|
| 31 |
+
|
| 32 |
+
# Input fields
|
| 33 |
+
col1, col2 = st.columns(2)
|
| 34 |
+
with col1:
|
| 35 |
+
temp_value = st.number_input("Enter Temperature Value:", min_value=-273.15, format="%.2f")
|
| 36 |
+
with col2:
|
| 37 |
+
from_unit = st.selectbox("From Unit", ["Celsius", "Fahrenheit", "Kelvin"], index=0)
|
| 38 |
+
to_unit = st.selectbox("To Unit", ["Celsius", "Fahrenheit", "Kelvin"], index=1)
|
| 39 |
+
|
| 40 |
+
if st.button("Convert"):
|
| 41 |
+
converted_value = convert_temperature(temp_value, from_unit, to_unit)
|
| 42 |
+
if converted_value is not None:
|
| 43 |
+
st.success(f"Converted Temperature: {converted_value:.2f} {to_unit}")
|
| 44 |
+
else:
|
| 45 |
+
st.error("Invalid conversion. Please check your input.")
|
| 46 |
+
|
| 47 |
+
# Footer
|
| 48 |
+
st.markdown("---")
|
| 49 |
+
st.markdown("**Made with ❤️ using Streamlit**")
|