KamalShahid commited on
Commit
3bb737e
·
verified ·
1 Parent(s): 8cad829

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -53
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("Convert temperatures between Celsius, Fahrenheit, and Kelvin.")
7
-
8
- # User Inputs
9
- col1, col2 = st.columns(2)
10
-
11
- with col1:
12
- temperature_value = st.number_input("Enter temperature value:", value=0.0, format="%.2f")
13
- input_unit = st.selectbox("Select input unit:", ["Celsius", "Fahrenheit", "Kelvin"])
14
-
15
- with col2:
16
- output_unit = st.selectbox("Select output unit:", ["Celsius", "Fahrenheit", "Kelvin"])
17
-
18
- # Conversion Function
19
- def convert_temperature(value, from_unit, to_unit):
20
- if from_unit == to_unit:
21
- return value
22
- elif from_unit == "Celsius":
23
- if to_unit == "Fahrenheit":
24
- return (value * 9/5) + 32
25
- elif to_unit == "Kelvin":
26
- return value + 273.15
27
- elif from_unit == "Fahrenheit":
28
- if to_unit == "Celsius":
29
- return (value - 32) * 5/9
30
- elif to_unit == "Kelvin":
31
- return (value - 32) * 5/9 + 273.15
32
- elif from_unit == "Kelvin":
33
- if to_unit == "Celsius":
34
- return value - 273.15
35
- elif to_unit == "Fahrenheit":
36
- return (value - 273.15) * 9/5 + 32
37
-
38
- # Perform Conversion
39
- converted_value = convert_temperature(temperature_value, input_unit, output_unit)
40
-
41
- # Display Result
42
- st.metric(label=f"Converted Temperature ({output_unit})", value=f"{converted_value:.2f}")
43
-
44
- # Additional Feature: Conversion History
45
- if "history" not in st.session_state:
46
- st.session_state["history"] = []
47
-
48
- st.session_state["history"].append({
49
- "Input Value": temperature_value,
50
- "Input Unit": input_unit,
51
- "Output Unit": output_unit,
52
- "Converted Value": converted_value
53
- })
54
-
55
- if st.checkbox("Show Conversion History"):
56
- history_df = pd.DataFrame(st.session_state["history"])
57
- st.dataframe(history_df)
 
 
 
 
 
 
 
 
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.")