Spaces:
Sleeping
Sleeping
| 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") | |