Spaces:
Sleeping
Sleeping
import streamlit as st | |
# Title and description | |
st.set_page_config(page_title="Temperature Converter", layout="centered") | |
st.title("🌡️ Temperature Converter") | |
# Conversion function | |
def convert_temperature(value, from_unit, to_unit): | |
if from_unit == to_unit: | |
return value | |
# Convert to Celsius first | |
if from_unit == "Celsius": | |
celsius = value | |
elif from_unit == "Fahrenheit": | |
celsius = (value - 32) * 5 / 9 | |
elif from_unit == "Kelvin": | |
celsius = value - 273.15 | |
# Convert from Celsius to target | |
if to_unit == "Celsius": | |
return celsius | |
elif to_unit == "Fahrenheit": | |
return (celsius * 9 / 5) + 32 | |
elif to_unit == "Kelvin": | |
return celsius + 273.15 | |
# Input section | |
col1, col2 = st.columns(2) | |
with col1: | |
from_unit = st.selectbox("From Unit", ["Celsius", "Fahrenheit", "Kelvin"]) | |
with col2: | |
to_unit = st.selectbox("To Unit", ["Celsius", "Fahrenheit", "Kelvin"]) | |
temp_input = st.text_input("Enter Temperature (0 - 500)", "0") | |
# Validate input | |
try: | |
temp_value = float(temp_input) | |
if temp_value < 0 or temp_value > 500: | |
st.error("Please enter a temperature between 0 and 500.") | |
else: | |
converted = convert_temperature(temp_value, from_unit, to_unit) | |
st.success(f"Converted Temperature: {converted:.2f} {to_unit}") | |
except ValueError: | |
st.warning("Please enter a valid numeric temperature.") | |