temp-convertor / app.py
amasood's picture
Update app.py
c549026 verified
import streamlit as st
# Conversion functions
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
def celsius_to_kelvin(celsius):
return celsius + 273.15
def kelvin_to_celsius(kelvin):
return kelvin - 273.15
def fahrenheit_to_kelvin(fahrenheit):
celsius = fahrenheit_to_celsius(fahrenheit)
return celsius_to_kelvin(celsius)
def kelvin_to_fahrenheit(kelvin):
celsius = kelvin_to_celsius(kelvin)
return celsius_to_fahrenheit(celsius)
# Streamlit app
st.set_page_config(
page_title="Temperature Converter",
page_icon="🌡️",
layout="centered"
)
# App title with icon
st.markdown(
"<h1 style='text-align: center; color: #FF5733;'>🌡️ Temperature Conversion App</h1>",
unsafe_allow_html=True
)
st.write("Convert temperatures between Celsius, Fahrenheit, and Kelvin with ease!")
# Sidebar for user input
st.sidebar.header("Conversion Options")
conversion_type = st.sidebar.radio(
"Choose a conversion type:",
(
"Celsius to Fahrenheit",
"Fahrenheit to Celsius",
"Celsius to Kelvin",
"Kelvin to Celsius",
"Fahrenheit to Kelvin",
"Kelvin to Fahrenheit"
)
)
st.sidebar.markdown(
"<hr style='border: 1px solid #FF5733;'>",
unsafe_allow_html=True
)
temperature_input = st.sidebar.number_input(
"Enter the temperature value:",
format="%.2f"
)
# Add a button for conversion
if st.sidebar.button("Convert"):
if conversion_type == "Celsius to Fahrenheit":
converted_temp = celsius_to_fahrenheit(temperature_input)
result = f"{temperature_input}°C is equal to {converted_temp:.2f}°F."
elif conversion_type == "Fahrenheit to Celsius":
converted_temp = fahrenheit_to_celsius(temperature_input)
result = f"{temperature_input}°F is equal to {converted_temp:.2f}°C."
elif conversion_type == "Celsius to Kelvin":
converted_temp = celsius_to_kelvin(temperature_input)
result = f"{temperature_input}°C is equal to {converted_temp:.2f}K."
elif conversion_type == "Kelvin to Celsius":
converted_temp = kelvin_to_celsius(temperature_input)
result = f"{temperature_input}K is equal to {converted_temp:.2f}°C."
elif conversion_type == "Fahrenheit to Kelvin":
converted_temp = fahrenheit_to_kelvin(temperature_input)
result = f"{temperature_input}°F is equal to {converted_temp:.2f}K."
elif conversion_type == "Kelvin to Fahrenheit":
converted_temp = kelvin_to_fahrenheit(temperature_input)
result = f"{temperature_input}K is equal to {converted_temp:.2f}°F."
# Display result with styled text
st.markdown(
f"<h3 style='text-align: center; color: #33A1FF;'>{result}</h3>",
unsafe_allow_html=True
)
# Footer
st.markdown(
"<hr style='border: 1px solid #FF5733;'>",
unsafe_allow_html=True
)
st.markdown(
"<p style='text-align: center;'>Made with ❤️ using Streamlit</p>",
unsafe_allow_html=True
)