Kamran Zulfiqar commited on
Commit
01f307a
·
verified ·
1 Parent(s): 9dffab5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -20
app.py CHANGED
@@ -1,24 +1,42 @@
1
  import streamlit as st
2
 
3
- # App title
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  st.title("🌡️ Temperature Converter")
5
 
6
- # Temperature unit selection
7
- conversion_type = st.radio(
8
- "Select Conversion Type:",
9
- ("Celsius to Fahrenheit", "Fahrenheit to Celsius")
10
- )
11
-
12
- # Temperature input using a slider
13
- if conversion_type == "Celsius to Fahrenheit":
14
- temp_c = st.slider("Select Temperature in °C:", -100.0, 100.0, 0.0)
15
- temp_f = (temp_c * 9/5) + 32
16
- st.metric("Converted Temperature (°F)", f"{temp_f:.2f} °F")
17
- else:
18
- temp_f = st.slider("Select Temperature in °F:", -148.0, 212.0, 32.0)
19
- temp_c = (temp_f - 32) * 5/9
20
- st.metric("Converted Temperature (°C)", f"{temp_c:.2f} °C")
21
-
22
- # Optional clear button (refreshes app)
23
- if st.button("🔄 Reset"):
24
- st.experimental_rerun()
 
1
  import streamlit as st
2
 
3
+ # Temperature conversion functions
4
+ def convert_temperature(value, from_unit, to_unit):
5
+ if from_unit == to_unit:
6
+ return value
7
+
8
+ # Convert input to Celsius
9
+ if from_unit == "Celsius":
10
+ celsius = value
11
+ elif from_unit == "Fahrenheit":
12
+ celsius = (value - 32) * 5/9
13
+ elif from_unit == "Kelvin":
14
+ celsius = value - 273.15
15
+
16
+ # Convert from Celsius to target
17
+ if to_unit == "Celsius":
18
+ return celsius
19
+ elif to_unit == "Fahrenheit":
20
+ return (celsius * 9/5) + 32
21
+ elif to_unit == "Kelvin":
22
+ return celsius + 273.15
23
+
24
+ # Streamlit UI
25
+ st.set_page_config(page_title="Temperature Converter", layout="centered")
26
+
27
  st.title("🌡️ Temperature Converter")
28
 
29
+ col1, col2 = st.columns(2)
30
+
31
+ with col1:
32
+ from_unit = st.selectbox("From Unit", ["Celsius", "Fahrenheit", "Kelvin"])
33
+ with col2:
34
+ to_unit = st.selectbox("To Unit", ["Celsius", "Fahrenheit", "Kelvin"])
35
+
36
+ temp_value = st.slider("Select Temperature", min_value=0.0, max_value=500.0, step=0.1)
37
+
38
+ converted = convert_temperature(temp_value, from_unit, to_unit)
39
+
40
+ st.markdown(f"### 🔁 Converted Temperature: **{converted:.2f} {to_unit}**")
41
+
42
+