syedwaqarumer's picture
Update app.py
3faf367 verified
import streamlit as st
# Set the page configuration for a cleaner, centered layout
st.set_page_config(page_title="Temperature Converter", page_icon="🌡️", layout="centered")
# Custom CSS for improved look and feel
st.markdown("""
<style>
.stApp {
background-color: #f4f4f4;
}
.sidebar .sidebar-content {
background-color: #e0f7fa;
}
.temp-box {
background-color: #e1f5fe;
padding: 10px;
border-radius: 10px;
text-align: center;
margin-top: 10px;
}
.result-box {
background-color: #fff3e0;
padding: 15px;
border-radius: 10px;
text-align: center;
color: #d84315;
}
</style>
""", unsafe_allow_html=True)
# Main title of the app
st.title("🌡️ Temperature Converter")
# Sidebar for inputs
st.sidebar.header("Input Options")
# Temperature unit selection in sidebar
from_unit = st.sidebar.selectbox("Convert from:", ["Celsius", "Fahrenheit", "Kelvin"], index=0)
to_unit = st.sidebar.selectbox("Convert to:", ["Celsius", "Fahrenheit", "Kelvin"], index=1)
# Temperature value input
temp_value = st.sidebar.number_input(f"Enter the temperature in {from_unit}", value=0.0)
# Conversion logic
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
else:
return value
elif from_unit == "Fahrenheit":
if to_unit == "Celsius":
return (value - 32) * 5/9
elif to_unit == "Kelvin":
return (value - 32) * 5/9 + 273.15
else:
return value
elif from_unit == "Kelvin":
if to_unit == "Celsius":
return value - 273.15
elif to_unit == "Fahrenheit":
return (value - 273.15) * 9/5 + 32
else:
return value
# Perform the conversion
converted_temp = convert_temperature(temp_value, from_unit, to_unit)
# Display conversion result in a styled box
st.markdown("<h3 style='text-align: center; color: #00695c;'>Conversion Result</h3>", unsafe_allow_html=True)
# Display the result with custom style and conditions
if from_unit == to_unit:
st.markdown(f"<div class='temp-box'><h4>{temp_value:.2f} {from_unit} is the same as {converted_temp:.2f} {to_unit}</h4></div>", unsafe_allow_html=True)
else:
st.markdown(f"<div class='result-box'><h4>The converted temperature is: {converted_temp:.2f} {to_unit}</h4></div>", unsafe_allow_html=True)
# Add a clean footer with additional information
st.markdown("""
<hr style="border:2px solid #80cbc4;"> </hr>
<div style="text-align: center">
Created by [Your Name]. Streamlit Temperature Converter.
</div>
""", unsafe_allow_html=True)