Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| def convert_temperature(value, from_unit, to_unit): | |
| """Convert temperature between Celsius, Fahrenheit, and Kelvin.""" | |
| if from_unit == "Celsius": | |
| if to_unit == "Fahrenheit": | |
| return value * 9/5 + 32 | |
| elif to_unit == "Kelvin": | |
| return value + 273.15 | |
| elif from_unit == "Fahrenheit": | |
| if to_unit == "Celsius": | |
| return (value - 32) * 5/9 | |
| elif to_unit == "Kelvin": | |
| return (value - 32) * 5/9 + 273.15 | |
| elif from_unit == "Kelvin": | |
| if to_unit == "Celsius": | |
| return value - 273.15 | |
| elif to_unit == "Fahrenheit": | |
| return (value - 273.15) * 9/5 + 32 | |
| return value | |
| def main(): | |
| st.set_page_config(page_title="Temperature Converter", page_icon="🌡", layout="wide") | |
| st.title("🌡 Temperature Converter") | |
| st.sidebar.header("Conversion Settings") | |
| # Input Section | |
| st.sidebar.subheader("Temperature Input") | |
| value = st.sidebar.number_input("Enter temperature value:", value=0.0, step=0.1, format="%.1f") | |
| from_unit = st.sidebar.selectbox("From Unit:", ["Celsius", "Fahrenheit", "Kelvin"], index=0) | |
| to_unit = st.sidebar.selectbox("To Unit:", ["Celsius", "Fahrenheit", "Kelvin"], index=1) | |
| # Columns for layout | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.subheader("Input Details") | |
| st.write(f"**Temperature Value:** {value:.2f} {from_unit}") | |
| with col2: | |
| st.subheader("Conversion Result") | |
| if st.sidebar.button("Convert"): | |
| if from_unit == to_unit: | |
| result = value | |
| st.write(f"The temperature remains the same: {result:.2f} {to_unit}") | |
| else: | |
| result = convert_temperature(value, from_unit, to_unit) | |
| st.write(f"Converted Temperature: {result:.2f} {to_unit}") | |
| else: | |
| st.write("Click the Convert button to see the result.") | |
| st.sidebar.markdown("---") | |
| st.sidebar.info( | |
| "This application allows you to convert temperatures between Celsius, Fahrenheit, and Kelvin units with ease!" | |
| ) | |
| if __name__ == "__main__": | |
| main() | |