bhagwandas's picture
Update app.py
4e2873f verified
import streamlit as st
import math
# App title and configuration
st.set_page_config(
page_title="Temperature Converter",
page_icon="🌡️",
layout="centered",
initial_sidebar_state="expanded"
)
# Custom CSS for a fancy look
st.markdown(
"""
<style>
.title {
font-size: 2.5em;
font-weight: bold;
color: #4CAF50;
text-align: center;
}
.subtitle {
font-size: 1.5em;
color: #2196F3;
text-align: center;
}
.math-box {
background-color: #f9f9f9;
border: 1px solid #ddd;
padding: 10px;
border-radius: 5px;
}
</style>
""",
unsafe_allow_html=True
)
# Title and subtitle
st.markdown('<div class="title">🌡️ Temperature Converter 🌟</div>', unsafe_allow_html=True)
st.markdown('<div class="subtitle">Convert temperatures between Celsius, Fahrenheit, and Kelvin</div>', unsafe_allow_html=True)
# Sidebar for user input
st.sidebar.header("Conversion Settings")
input_unit = st.sidebar.selectbox("Select Input Unit:", ["Celsius (°C)", "Fahrenheit (°F)", "Kelvin (K)"])
output_unit = st.sidebar.selectbox("Select Output Unit:", ["Celsius (°C)", "Fahrenheit (°F)", "Kelvin (K)"])
input_value = st.sidebar.number_input("Enter Temperature Value:", value=0.0)
# Conversion logic
def convert_temperature(value, from_unit, to_unit):
if from_unit == to_unit:
return value
# Convert input to Celsius first
if from_unit == "Celsius (°C)":
celsius = value
elif from_unit == "Fahrenheit (°F)":
celsius = (value - 32) * 5 / 9
elif from_unit == "Kelvin (K)":
celsius = value - 273.15
# Convert from Celsius to target unit
if to_unit == "Celsius (°C)":
return celsius
elif to_unit == "Fahrenheit (°F)":
return celsius * 9 / 5 + 32
elif to_unit == "Kelvin (K)":
return celsius + 273.15
# Perform conversion
result = convert_temperature(input_value, input_unit, output_unit)
# Display result
st.markdown(f"### 🌟 Converted Temperature: **{result:.2f} {output_unit}**")
# Display math formula
st.markdown("#### 📚 Mathematical Formula")
if input_unit == "Celsius (°C)" and output_unit == "Fahrenheit (°F)":
st.markdown('**F = (C × 9/5) + 32**')
elif input_unit == "Fahrenheit (°F)" and output_unit == "Celsius (°C)":
st.markdown('**C = (F - 32) × 5/9**')
elif input_unit == "Celsius (°C)" and output_unit == "Kelvin (K)":
st.markdown('**K = C + 273.15**')
elif input_unit == "Kelvin (K)" and output_unit == "Celsius (°C)":
st.markdown('**C = K - 273.15**')
elif input_unit == "Fahrenheit (°F)" and output_unit == "Kelvin (K)":
st.markdown('**K = (F - 32) × 5/9 + 273.15**')
elif input_unit == "Kelvin (K)" and output_unit == "Fahrenheit (°F)":
st.markdown('**F = (K - 273.15) × 9/5 + 32**')
# Footer
st.markdown("---")
st.markdown('Created with ❤️ using **Streamlit**')