asghani's picture
Create app.py
54cf693 verified
import streamlit as st
# Title of the app
st.title('Temperature Converter')
# Instructions
st.write("Convert temperature between Celsius, Fahrenheit, and Kelvin.")
# Input field for temperature value
temp_input = st.number_input("Enter temperature value:", value=0.0)
# Dropdown menu to select the input unit
input_unit = st.selectbox("Select input unit:", ["Celsius", "Fahrenheit", "Kelvin"])
# Dropdown menu to select the output unit
output_unit = st.selectbox("Select output unit:", ["Celsius", "Fahrenheit", "Kelvin"])
# Conversion functions
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def celsius_to_kelvin(celsius):
return celsius + 273.15
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
def fahrenheit_to_kelvin(fahrenheit):
return (fahrenheit - 32) * 5/9 + 273.15
def kelvin_to_celsius(kelvin):
return kelvin - 273.15
def kelvin_to_fahrenheit(kelvin):
return (kelvin - 273.15) * 9/5 + 32
# Conversion logic
if input_unit == "Celsius":
if output_unit == "Fahrenheit":
result = celsius_to_fahrenheit(temp_input)
elif output_unit == "Kelvin":
result = celsius_to_kelvin(temp_input)
else:
result = temp_input # Same unit
elif input_unit == "Fahrenheit":
if output_unit == "Celsius":
result = fahrenheit_to_celsius(temp_input)
elif output_unit == "Kelvin":
result = fahrenheit_to_kelvin(temp_input)
else:
result = temp_input # Same unit
elif input_unit == "Kelvin":
if output_unit == "Celsius":
result = kelvin_to_celsius(temp_input)
elif output_unit == "Fahrenheit":
result = kelvin_to_fahrenheit(temp_input)
else:
result = temp_input # Same unit
# Display result
st.write(f"Converted temperature: {result:.2f} {output_unit}")