Spaces:
Sleeping
Sleeping
import streamlit as st | |
# Function for temperature conversion | |
def convert_temperature(value, from_unit, to_unit): | |
if from_unit == "Celsius" and to_unit == "Fahrenheit": | |
return (value * 9/5) + 32 | |
elif from_unit == "Fahrenheit" and to_unit == "Celsius": | |
return (value - 32) * 5/9 | |
elif from_unit == "Celsius" and to_unit == "Kelvin": | |
return value + 273.15 | |
elif from_unit == "Kelvin" and to_unit == "Celsius": | |
return value - 273.15 | |
elif from_unit == "Fahrenheit" and to_unit == "Kelvin": | |
return (value - 32) * 5/9 + 273.15 | |
elif from_unit == "Kelvin" and to_unit == "Fahrenheit": | |
return (value - 273.15) * 9/5 + 32 | |
else: | |
return value | |
# Streamlit interface | |
st.title('Temperature Converter') | |
st.write('This app allows you to convert between Celsius, Fahrenheit, and Kelvin.') | |
# Input from the user | |
value = st.number_input("Enter the temperature value:", min_value=-273.15) | |
from_unit = st.selectbox("Select the unit to convert from:", ["Celsius", "Fahrenheit", "Kelvin"]) | |
to_unit = st.selectbox("Select the unit to convert to:", ["Celsius", "Fahrenheit", "Kelvin"]) | |
# Perform conversion | |
if st.button("Convert"): | |
if from_unit != to_unit: | |
result = convert_temperature(value, from_unit, to_unit) | |
st.write(f"The converted temperature is {result:.2f} {to_unit}.") | |
else: | |
st.write("Please select different units for conversion.") | |