Talha812's picture
Create app.py
e21bcc2 verified
import streamlit as st
# Temperature conversion logic
def convert_temperature(value, from_unit, to_unit):
if from_unit == to_unit:
return value
# Convert from any to Celsius first
if from_unit == "Fahrenheit":
celsius = (value - 32) * 5 / 9
elif from_unit == "Kelvin":
celsius = value - 273.15
elif from_unit == "Rankine":
celsius = (value - 491.67) * 5 / 9
else: # from_unit == "Celsius"
celsius = value
# Convert from Celsius to target unit
if to_unit == "Fahrenheit":
return (celsius * 9 / 5) + 32
elif to_unit == "Kelvin":
return celsius + 273.15
elif to_unit == "Rankine":
return (celsius + 273.15) * 9 / 5
else: # to_unit == "Celsius"
return celsius
# Emoji based on temperature in Celsius
def get_temperature_emoji(celsius):
if celsius < 0:
return "πŸ₯Άβ„️ Ice Cold"
elif 0 <= celsius < 15:
return "πŸ§₯πŸƒ Chilly"
elif 15 <= celsius < 25:
return "😊🌀 Pleasant"
elif 25 <= celsius < 35:
return "πŸ₯΅β˜€οΈ Warm"
else:
return "πŸ”₯πŸ₯΅ Scorching Hot"
# Streamlit UI
st.set_page_config(page_title="🌑️ Temperature Converter", page_icon="🌑️")
st.title("🌑️ Temperature Converter App")
st.markdown("Convert temperature between various units and get a fun emoji reaction based on the temperature!")
st.divider()
value = st.number_input("Enter Temperature Value", format="%.2f")
units = ["Celsius", "Fahrenheit", "Kelvin", "Rankine"]
from_unit = st.selectbox("Convert From", units)
to_unit = st.selectbox("Convert To", units)
if st.button("Convert"):
result = convert_temperature(value, from_unit, to_unit)
celsius_equiv = convert_temperature(value, from_unit, "Celsius")
st.markdown(f"### βœ… **Converted Temperature: `{result:.2f}Β° {to_unit}`**")
st.markdown(f"### {get_temperature_emoji(celsius_equiv)}")
st.divider()
st.caption("Made with ❀️ using Streamlit")