Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,70 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# Title and Description
|
| 5 |
-
st.title("Temperature Converter")
|
| 6 |
-
st.write("
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
"
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
# Sidebar for Navigation
|
| 5 |
+
with st.sidebar:
|
| 6 |
+
st.title("Navigation")
|
| 7 |
+
st.markdown("### Select an Option:")
|
| 8 |
+
selected = st.radio("", ["Temperature Converter", "Conversion History"])
|
| 9 |
+
|
| 10 |
# Title and Description
|
| 11 |
+
st.title("🌡️ Temperature Converter")
|
| 12 |
+
st.write("Easily convert temperatures between Celsius, Fahrenheit, and Kelvin with an intuitive interface.")
|
| 13 |
+
|
| 14 |
+
if selected == "Temperature Converter":
|
| 15 |
+
st.markdown("### Input Temperature Details")
|
| 16 |
+
col1, col2 = st.columns(2)
|
| 17 |
+
|
| 18 |
+
with col1:
|
| 19 |
+
temperature_value = st.number_input("🔢 Enter temperature value:", value=0.0, format="%.2f")
|
| 20 |
+
input_unit = st.selectbox("📥 Select input unit:", ["Celsius", "Fahrenheit", "Kelvin"])
|
| 21 |
+
|
| 22 |
+
with col2:
|
| 23 |
+
output_unit = st.selectbox("📤 Select output unit:", ["Celsius", "Fahrenheit", "Kelvin"])
|
| 24 |
+
|
| 25 |
+
# Conversion Function
|
| 26 |
+
def convert_temperature(value, from_unit, to_unit):
|
| 27 |
+
if from_unit == to_unit:
|
| 28 |
+
return value
|
| 29 |
+
elif from_unit == "Celsius":
|
| 30 |
+
if to_unit == "Fahrenheit":
|
| 31 |
+
return (value * 9/5) + 32
|
| 32 |
+
elif to_unit == "Kelvin":
|
| 33 |
+
return value + 273.15
|
| 34 |
+
elif from_unit == "Fahrenheit":
|
| 35 |
+
if to_unit == "Celsius":
|
| 36 |
+
return (value - 32) * 5/9
|
| 37 |
+
elif to_unit == "Kelvin":
|
| 38 |
+
return (value - 32) * 5/9 + 273.15
|
| 39 |
+
elif from_unit == "Kelvin":
|
| 40 |
+
if to_unit == "Celsius":
|
| 41 |
+
return value - 273.15
|
| 42 |
+
elif to_unit == "Fahrenheit":
|
| 43 |
+
return (value - 273.15) * 9/5 + 32
|
| 44 |
+
|
| 45 |
+
# Perform Conversion
|
| 46 |
+
converted_value = convert_temperature(temperature_value, input_unit, output_unit)
|
| 47 |
+
|
| 48 |
+
# Display Result
|
| 49 |
+
st.metric(label=f"Converted Temperature ({output_unit})", value=f"{converted_value:.2f}", delta=None)
|
| 50 |
+
|
| 51 |
+
elif selected == "Conversion History":
|
| 52 |
+
if "history" not in st.session_state:
|
| 53 |
+
st.session_state["history"] = []
|
| 54 |
+
|
| 55 |
+
# Add New Conversion to History
|
| 56 |
+
if "temperature_value" in locals():
|
| 57 |
+
st.session_state["history"].append({
|
| 58 |
+
"Input Value": temperature_value,
|
| 59 |
+
"Input Unit": input_unit,
|
| 60 |
+
"Output Unit": output_unit,
|
| 61 |
+
"Converted Value": converted_value
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
# Display Conversion History
|
| 65 |
+
st.markdown("### Conversion History")
|
| 66 |
+
if len(st.session_state["history"]) > 0:
|
| 67 |
+
history_df = pd.DataFrame(st.session_state["history"])
|
| 68 |
+
st.dataframe(history_df)
|
| 69 |
+
else:
|
| 70 |
+
st.info("No conversion history available yet.")
|