| import streamlit as st | |
| st.set_page_config(page_title="Temperature Converter", page_icon="🌡️") | |
| st.title("🌡️ Temperature Converter") | |
| # Input | |
| temp = st.number_input("Enter temperature value:", value=0.0) | |
| # Unit selection | |
| unit_from = st.selectbox("From:", ["Celsius", "Fahrenheit", "Kelvin"]) | |
| unit_to = st.selectbox("To:", ["Celsius", "Fahrenheit", "Kelvin"]) | |
| # Conversion function | |
| def convert_temperature(value, from_unit, to_unit): | |
| # Convert input to Celsius first | |
| if from_unit == "Celsius": | |
| c = value | |
| elif from_unit == "Fahrenheit": | |
| c = (value - 32) * 5/9 | |
| elif from_unit == "Kelvin": | |
| c = value - 273.15 | |
| # Convert Celsius to target | |
| if to_unit == "Celsius": | |
| return c | |
| elif to_unit == "Fahrenheit": | |
| return (c * 9/5) + 32 | |
| elif to_unit == "Kelvin": | |
| return c + 273.15 | |
| # Button | |
| if st.button("Convert"): | |
| result = convert_temperature(temp, unit_from, unit_to) | |
| st.success(f"{temp} {unit_from} = {result:.2f} {unit_to}") |