AmnaHassan commited on
Commit
6ce5d51
·
verified ·
1 Parent(s): 10cf7dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py CHANGED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ # Custom CSS for styling
4
+ st.markdown(
5
+ """
6
+ <style>
7
+ body {
8
+ background: linear-gradient(to right, #1e3c72, #2a5298);
9
+ font-family: 'Segoe UI', sans-serif;
10
+ color: white;
11
+ }
12
+ .stButton > button {
13
+ background-color: #1f78b4;
14
+ color: white;
15
+ border-radius: 8px;
16
+ font-size: 16px;
17
+ padding: 10px 20px;
18
+ border: none;
19
+ transition: 0.3s;
20
+ }
21
+ .stButton > button:hover {
22
+ background-color: #14527a;
23
+ }
24
+ </style>
25
+ """,
26
+ unsafe_allow_html=True,
27
+ )
28
+
29
+ # App title and introduction
30
+ st.title("🌡️ Temperature Converter")
31
+ st.markdown(
32
+ """
33
+ Welcome to the **Temperature Converter** app!
34
+ Convert temperatures seamlessly between Celsius, Fahrenheit, and Kelvin.
35
+ """
36
+ )
37
+
38
+ # Conversion function
39
+ def convert_temperature(value, from_unit, to_unit):
40
+ if from_unit == "Celsius":
41
+ if to_unit == "Fahrenheit":
42
+ return value * 9/5 + 32
43
+ elif to_unit == "Kelvin":
44
+ return value + 273.15
45
+ elif from_unit == "Fahrenheit":
46
+ if to_unit == "Celsius":
47
+ return (value - 32) * 5/9
48
+ elif to_unit == "Kelvin":
49
+ return (value - 32) * 5/9 + 273.15
50
+ elif from_unit == "Kelvin":
51
+ if to_unit == "Celsius":
52
+ return value - 273.15
53
+ elif to_unit == "Fahrenheit":
54
+ return (value - 273.15) * 9/5 + 32
55
+ return value
56
+
57
+ # Input fields
58
+ st.markdown("### Enter the temperature to convert:")
59
+ temperature = st.number_input("Temperature:", min_value=-273.15, value=0.0, step=0.1)
60
+
61
+ from_unit = st.selectbox("Convert from:", ["Celsius", "Fahrenheit", "Kelvin"])
62
+ to_unit = st.selectbox("Convert to:", ["Celsius", "Fahrenheit", "Kelvin"])
63
+
64
+ # Convert and display the result
65
+ if st.button("Convert"):
66
+ if from_unit == to_unit:
67
+ st.warning("Please select different units for conversion.")
68
+ else:
69
+ result = convert_temperature(temperature, from_unit, to_unit)
70
+ st.success(f"{temperature}° {from_unit} is equal to {result:.2f}° {to_unit}.")