Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| # Function to convert temperatures | |
| def convert_temperature(value, from_unit, to_unit): | |
| 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 # In case of same unit conversion | |
| # Streamlit app | |
| st.title("Temperature Converter") | |
| # Input temperature | |
| temperature = st.number_input("Enter the temperature:", format="%.2f") | |
| # Select the unit of the input temperature | |
| from_unit = st.selectbox("Select the unit of the input temperature:", ["Celsius", "Fahrenheit", "Kelvin"]) | |
| # Select the unit to convert to | |
| to_unit = st.selectbox("Select the unit to convert to:", ["Celsius", "Fahrenheit", "Kelvin"]) | |
| # Convert and display the result | |
| if st.button("Convert"): | |
| converted_temperature = convert_temperature(temperature, from_unit, to_unit) | |
| st.success(f"{temperature} {from_unit} is equal to {converted_temperature:.2f} {to_unit}.") | |