File size: 1,012 Bytes
0e6e352 fbf18fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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}") |