Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
# Temperature conversion logic
|
| 4 |
+
def convert_temperature(value, from_unit, to_unit):
|
| 5 |
+
if from_unit == to_unit:
|
| 6 |
+
return value
|
| 7 |
+
|
| 8 |
+
# Convert from any to Celsius first
|
| 9 |
+
if from_unit == "Fahrenheit":
|
| 10 |
+
celsius = (value - 32) * 5 / 9
|
| 11 |
+
elif from_unit == "Kelvin":
|
| 12 |
+
celsius = value - 273.15
|
| 13 |
+
elif from_unit == "Rankine":
|
| 14 |
+
celsius = (value - 491.67) * 5 / 9
|
| 15 |
+
else: # from_unit == "Celsius"
|
| 16 |
+
celsius = value
|
| 17 |
+
|
| 18 |
+
# Convert from Celsius to target unit
|
| 19 |
+
if to_unit == "Fahrenheit":
|
| 20 |
+
return (celsius * 9 / 5) + 32
|
| 21 |
+
elif to_unit == "Kelvin":
|
| 22 |
+
return celsius + 273.15
|
| 23 |
+
elif to_unit == "Rankine":
|
| 24 |
+
return (celsius + 273.15) * 9 / 5
|
| 25 |
+
else: # to_unit == "Celsius"
|
| 26 |
+
return celsius
|
| 27 |
+
|
| 28 |
+
# Emoji based on temperature in Celsius
|
| 29 |
+
def get_temperature_emoji(celsius):
|
| 30 |
+
if celsius < 0:
|
| 31 |
+
return "🥶❄️ Ice Cold"
|
| 32 |
+
elif 0 <= celsius < 15:
|
| 33 |
+
return "🧥🍃 Chilly"
|
| 34 |
+
elif 15 <= celsius < 25:
|
| 35 |
+
return "😊🌤 Pleasant"
|
| 36 |
+
elif 25 <= celsius < 35:
|
| 37 |
+
return "🥵☀️ Warm"
|
| 38 |
+
else:
|
| 39 |
+
return "🔥🥵 Scorching Hot"
|
| 40 |
+
|
| 41 |
+
# Streamlit UI
|
| 42 |
+
st.set_page_config(page_title="🌡️ Temperature Converter", page_icon="🌡️")
|
| 43 |
+
|
| 44 |
+
st.title("🌡️ Temperature Converter App")
|
| 45 |
+
st.markdown("Convert temperature between various units and get a fun emoji reaction based on the temperature!")
|
| 46 |
+
|
| 47 |
+
st.divider()
|
| 48 |
+
|
| 49 |
+
value = st.number_input("Enter Temperature Value", format="%.2f")
|
| 50 |
+
|
| 51 |
+
units = ["Celsius", "Fahrenheit", "Kelvin", "Rankine"]
|
| 52 |
+
from_unit = st.selectbox("Convert From", units)
|
| 53 |
+
to_unit = st.selectbox("Convert To", units)
|
| 54 |
+
|
| 55 |
+
if st.button("Convert"):
|
| 56 |
+
result = convert_temperature(value, from_unit, to_unit)
|
| 57 |
+
celsius_equiv = convert_temperature(value, from_unit, "Celsius")
|
| 58 |
+
|
| 59 |
+
st.markdown(f"### ✅ **Converted Temperature: `{result:.2f}° {to_unit}`**")
|
| 60 |
+
st.markdown(f"### {get_temperature_emoji(celsius_equiv)}")
|
| 61 |
+
|
| 62 |
+
st.divider()
|
| 63 |
+
st.caption("Made with ❤️ using Streamlit")
|