tempConverter / app.py
KamalShahid's picture
Update app.py
3bb737e verified
import streamlit as st
import pandas as pd
# Sidebar for Navigation
with st.sidebar:
st.title("Navigation")
st.markdown("### Select an Option:")
selected = st.radio("", ["Temperature Converter", "Conversion History"])
# Title and Description
st.title("🌑️ Temperature Converter")
st.write("Easily convert temperatures between Celsius, Fahrenheit, and Kelvin with an intuitive interface.")
if selected == "Temperature Converter":
st.markdown("### Input Temperature Details")
col1, col2 = st.columns(2)
with col1:
temperature_value = st.number_input("πŸ”’ Enter temperature value:", value=0.0, format="%.2f")
input_unit = st.selectbox("πŸ“₯ Select input unit:", ["Celsius", "Fahrenheit", "Kelvin"])
with col2:
output_unit = st.selectbox("πŸ“€ Select output unit:", ["Celsius", "Fahrenheit", "Kelvin"])
# Conversion Function
def convert_temperature(value, from_unit, to_unit):
if from_unit == to_unit:
return value
elif from_unit == "Celsius":
if to_unit == "Fahrenheit":
return (value * 9/5) + 32
elif to_unit == "Kelvin":
return value + 273.15
elif from_unit == "Fahrenheit":
if to_unit == "Celsius":
return (value - 32) * 5/9
elif to_unit == "Kelvin":
return (value - 32) * 5/9 + 273.15
elif from_unit == "Kelvin":
if to_unit == "Celsius":
return value - 273.15
elif to_unit == "Fahrenheit":
return (value - 273.15) * 9/5 + 32
# Perform Conversion
converted_value = convert_temperature(temperature_value, input_unit, output_unit)
# Display Result
st.metric(label=f"Converted Temperature ({output_unit})", value=f"{converted_value:.2f}", delta=None)
elif selected == "Conversion History":
if "history" not in st.session_state:
st.session_state["history"] = []
# Add New Conversion to History
if "temperature_value" in locals():
st.session_state["history"].append({
"Input Value": temperature_value,
"Input Unit": input_unit,
"Output Unit": output_unit,
"Converted Value": converted_value
})
# Display Conversion History
st.markdown("### Conversion History")
if len(st.session_state["history"]) > 0:
history_df = pd.DataFrame(st.session_state["history"])
st.dataframe(history_df)
else:
st.info("No conversion history available yet.")