bhagwandas commited on
Commit
4e2873f
·
verified ·
1 Parent(s): 4989758

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py CHANGED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import math
3
+
4
+ # App title and configuration
5
+ st.set_page_config(
6
+ page_title="Temperature Converter",
7
+ page_icon="🌡️",
8
+ layout="centered",
9
+ initial_sidebar_state="expanded"
10
+ )
11
+
12
+ # Custom CSS for a fancy look
13
+ st.markdown(
14
+ """
15
+ <style>
16
+ .title {
17
+ font-size: 2.5em;
18
+ font-weight: bold;
19
+ color: #4CAF50;
20
+ text-align: center;
21
+ }
22
+ .subtitle {
23
+ font-size: 1.5em;
24
+ color: #2196F3;
25
+ text-align: center;
26
+ }
27
+ .math-box {
28
+ background-color: #f9f9f9;
29
+ border: 1px solid #ddd;
30
+ padding: 10px;
31
+ border-radius: 5px;
32
+ }
33
+ </style>
34
+ """,
35
+ unsafe_allow_html=True
36
+ )
37
+
38
+ # Title and subtitle
39
+ st.markdown('<div class="title">🌡️ Temperature Converter 🌟</div>', unsafe_allow_html=True)
40
+ st.markdown('<div class="subtitle">Convert temperatures between Celsius, Fahrenheit, and Kelvin</div>', unsafe_allow_html=True)
41
+
42
+ # Sidebar for user input
43
+ st.sidebar.header("Conversion Settings")
44
+ input_unit = st.sidebar.selectbox("Select Input Unit:", ["Celsius (°C)", "Fahrenheit (°F)", "Kelvin (K)"])
45
+ output_unit = st.sidebar.selectbox("Select Output Unit:", ["Celsius (°C)", "Fahrenheit (°F)", "Kelvin (K)"])
46
+ input_value = st.sidebar.number_input("Enter Temperature Value:", value=0.0)
47
+
48
+ # Conversion logic
49
+ def convert_temperature(value, from_unit, to_unit):
50
+ if from_unit == to_unit:
51
+ return value
52
+
53
+ # Convert input to Celsius first
54
+ if from_unit == "Celsius (°C)":
55
+ celsius = value
56
+ elif from_unit == "Fahrenheit (°F)":
57
+ celsius = (value - 32) * 5 / 9
58
+ elif from_unit == "Kelvin (K)":
59
+ celsius = value - 273.15
60
+
61
+ # Convert from Celsius to target unit
62
+ if to_unit == "Celsius (°C)":
63
+ return celsius
64
+ elif to_unit == "Fahrenheit (°F)":
65
+ return celsius * 9 / 5 + 32
66
+ elif to_unit == "Kelvin (K)":
67
+ return celsius + 273.15
68
+
69
+ # Perform conversion
70
+ result = convert_temperature(input_value, input_unit, output_unit)
71
+
72
+ # Display result
73
+ st.markdown(f"### 🌟 Converted Temperature: **{result:.2f} {output_unit}**")
74
+
75
+ # Display math formula
76
+ st.markdown("#### 📚 Mathematical Formula")
77
+ if input_unit == "Celsius (°C)" and output_unit == "Fahrenheit (°F)":
78
+ st.markdown('**F = (C × 9/5) + 32**')
79
+ elif input_unit == "Fahrenheit (°F)" and output_unit == "Celsius (°C)":
80
+ st.markdown('**C = (F - 32) × 5/9**')
81
+ elif input_unit == "Celsius (°C)" and output_unit == "Kelvin (K)":
82
+ st.markdown('**K = C + 273.15**')
83
+ elif input_unit == "Kelvin (K)" and output_unit == "Celsius (°C)":
84
+ st.markdown('**C = K - 273.15**')
85
+ elif input_unit == "Fahrenheit (°F)" and output_unit == "Kelvin (K)":
86
+ st.markdown('**K = (F - 32) × 5/9 + 273.15**')
87
+ elif input_unit == "Kelvin (K)" and output_unit == "Fahrenheit (°F)":
88
+ st.markdown('**F = (K - 273.15) × 9/5 + 32**')
89
+
90
+ # Footer
91
+ st.markdown("---")
92
+ st.markdown('Created with ❤️ using **Streamlit**')