-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
317 lines (272 loc) · 9.7 KB
/
Copy pathapp.py
File metadata and controls
317 lines (272 loc) · 9.7 KB
1
2
3
4
5
6
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import streamlit as st
st.set_page_config(page_title="Unit Converter", page_icon="📏", layout="centered")
# Session state to manage dark mode
if "dark_mode" not in st.session_state:
st.session_state.dark_mode = False
# Function to toggle dark mode
def toggle_dark_mode():
st.session_state.dark_mode = not st.session_state.dark_mode
# Dark Mode Toggle Button
if st.sidebar.button("🌙 Toggle Dark Mode"):
toggle_dark_mode()
# Apply CSS dynamically
brown_mode_css = """
<style>
.st-bw, .st-emotion-cache-jh76sn, .stApp {
background-color: #121212;
color: #FFFFFF;
}
.stTextInput, .stSidebar, .st-bx, .st-emotion-cache-124cvek, .st-emotion-cache-124cvek:hover, .st-emotion-cache-topux, .st-d9 {
background-color: #2a2929;
color: #FFFFFF;
}
</style>
"""
dark_mode_css = """
<style>
.st-emotion-cache-jh76sn, .stApp {
background-color: #0E1117;
color: #ffffff;
}
</style>
"""
# Inject CSS based on dark mode state
if st.session_state.dark_mode:
st.markdown(brown_mode_css, unsafe_allow_html=True)
else:
st.markdown(dark_mode_css, unsafe_allow_html=True)
# Title of the app
st.title("Unit Converter - The Ultimate Conversion Tool")
st.markdown("---")
# Sidebar for selecting the type of conversion
st.sidebar.header("🔧 Select Conversion Type")
conversion_type = st.sidebar.selectbox(
"Choose a conversion type:",
["📏 Length", "⚖️ Weight", "🌡️ Temperature", "🧪 Volume", "⏰ Time"]
)
# Length Conversion
if conversion_type == "📏 Length":
st.header("📏 Length Conversion")
length_units = ["Meters", "Foot", "Inches", "Centimeters"]
from_unit = st.selectbox("From", length_units)
to_unit = st.selectbox("To", length_units)
length_value = st.number_input("Enter Value", value=1.0)
# Conversion of Meter
if from_unit == "Meters":
if to_unit == "Foot":
result = length_value * 3.28084
elif to_unit == "Inches":
result = length_value * 39.3701
elif to_unit == "Centimeters":
result = length_value * 100
else:
result = length_value
# Conversion of Foot
elif from_unit == "Foot":
if to_unit == "Meters":
result = length_value / 3.28084
if to_unit == "Inches":
result = length_value * 12
if to_unit == "Centimeters":
result = length_value * 30.48
else:
result = length_value
# Conversion of Inches
elif from_unit == "Inches":
if to_unit == "Meters":
result = length_value / 39.3701
elif to_unit == "Foot":
result = length_value / 12
elif to_unit == "Centimeters":
result = length_value * 2.54
else:
result = length_value
# Conversion of Centimeter
elif from_unit == "Centimeters":
if to_unit == "Meters":
result = length_value / 100
elif to_unit == "Foot":
result = length_value / 30.48
elif to_unit == "Inches":
result = length_value / 2.54
else:
result = length_value
st.success(f"Result: {length_value} {from_unit} = {result:.2f} {to_unit}")
# Weight Conversion
elif conversion_type == "⚖️ Weight":
st.header("⚖️ Weight Conversion")
weight_units = ["Kilograms", "Pounds", "Grams", "Ounces"]
from_unit = st.selectbox("From", weight_units)
to_unit = st.selectbox("To", weight_units)
weight_value = st.number_input("Enter Value", value=1.0)
# Conversion of Kilograms
if from_unit == "Kilograms":
if to_unit == "Pounds":
result = weight_value * 2.20462
elif to_unit == "Grams":
result = weight_value * 1000
elif to_unit == "Onces":
result = weight_value * 35.274
else:
result = weight_value
# Conversion of Pounds
elif from_unit == "Pounds":
if to_unit == "Kilograms":
result = weight_value / 2.20462
elif to_unit == "Grams":
result = weight_value * 453.592
elif to_unit == "Onces":
result = weight_value * 16
else:
result = weight_value
# Conversion of Grams
elif from_unit == "Grams":
if to_unit == "Kilograms":
result = weight_value / 1000
elif to_unit == "Pounds":
result = weight_value / 453.592
elif to_unit == "Ounces":
result = weight_value / 28.3495
else:
result = weight_value
# Conversion of Onces
elif from_unit == "Ounces":
if to_unit == "Kilograms":
result = weight_value / 35.274
elif to_unit == "Pounds":
result = weight_value / 16
elif to_unit == "Grams":
result = weight_value * 28.3495
else:
result = weight_value
st.success(f"Result: {weight_value} {from_unit} = {result:.2f} {to_unit}")
# Temperature Conversion
elif conversion_type == "🌡️ Temperature":
st.header("🌡️ Temperatur Conversion")
temp_units = ["Celsius", "Fahrenheit", "Kelvin"]
from_unit = st.selectbox("From", temp_units)
to_unit = st.selectbox("To", temp_units)
temp_value = st.number_input("Enter Value", value=0.0)
# Conversion of Celsius
if from_unit == "Celsius":
if to_unit == "Fahrenheit":
result = temp_value * 9/5 + 32
elif to_unit == "Kelvin":
result = temp_value + 273.15
else:
result = temp_value
# Conversion of Fahrenheit
if from_unit == "Fahrenheit":
if to_unit == "Celsius":
result = (temp_value - 32) * 5/9
elif to_unit == "Kelvin":
result = (temp_value - 32) * 5/9 + 273.15
else:
result = temp_value
# Conversion of Kelvin
if from_unit == "Kelvin":
if to_unit == "Celsius":
result = temp_value - 273.15
elif to_unit == "Fahrenheit":
result = (temp_value - 273.15) * 9/5 + 32
else:
result = temp_value
st.success(f"Result: {temp_value} {from_unit} = {result:.2f} {to_unit}")
# Volume Conversion
elif conversion_type == "🧪 Volume":
st.header("🧪 Volume Conversion")
volume_units = ["Liters", "Milliliters", "Gallons", "Cubic Meters"]
from_unit = st.selectbox("From", volume_units)
to_unit = st.selectbox("To", volume_units)
volume_value = st.number_input("Enter Value", value=1.0)
# Conversion of Liters
if from_unit == "Liters":
if to_unit == "Milliliters":
result = volume_value * 1000
elif to_unit == "Gallons":
result = volume_value * 0.264172
elif to_unit == "Cubic Meters":
result = volume_value / 1000
else:
result = volume_value
# Conversion of MilliLiters
elif from_unit == "Milliliters":
if to_unit == "Liters":
result = volume_value / 1000
elif to_unit == "Gallons":
result = volume_value * 0.000264172
elif to_unit == "Cubic Meters":
result = volume_value / 1e+6
else:
result = volume_value
# Conversion of Gallons
elif from_unit == "Gallons":
if to_unit == "Liters":
result = volume_value * 3.78541
elif to_unit == "Milliliters":
result = volume_value * 3785.41
elif to_unit == "Cubic Meters":
result = volume_value * 0.00378541
else:
result = volume_value
# Conversion of Cubic Meters
elif from_unit == "Cubic Meters":
if to_unit == "Liters":
result = volume_value * 1000
elif to_unit == "Milliliters":
result = volume_value * 1e+6
elif to_unit == "Gallons":
result = volume_value * 264.172
else:
result = volume_value
st.success(f"Result: {volume_value} {from_unit} = {result:.2f} {to_unit}")
# Time Conversion
elif conversion_type == "⏰ Time":
st.header("⏰ Time Conversion")
time_units = ["Seconds", "Minutes", "Hours", "Days"]
from_unit = st.selectbox("From", time_units)
to_unit = st.selectbox("To", time_units)
time_value = st.number_input("Enter Value", value=1.0)
# Conversion of Seconds
if from_unit == "Seconds":
if to_unit == "Minutes":
result = time_value / 60
elif to_unit == "Hours":
result = time_value / 3600
elif to_unit == "Days":
result = time_value / 86400
else:
result = time_value
# Conversion of Minutes
elif from_unit == "Minutes":
if to_unit == "Seconds":
result = time_value * 60
elif to_unit == "Hours":
result = time_value / 60
elif to_unit == "Days":
result = time_value / 1440
else:
result = time_value
# Conversion of Hours
elif from_unit == "Hours":
if to_unit == "Seconds":
result = time_value * 3600
elif to_unit == "Minutes":
result = time_value * 60
elif to_unit == "Days":
result = time_value / 24
else:
result = time_value
elif from_unit == "Days":
if to_unit == "Seconds":
result = time_value * 86400
elif to_unit == "Minutes":
result = time_value * 1440
elif to_unit == "Hours":
result = time_value * 24
else:
result = time_value
st.success(f"Result: {time_value} {from_unit} = {result:.2f} {to_unit}")
st.markdown('</div', unsafe_allow_html=True)
st.markdown("---")
st.markdown("<p style='text-align: center; font-size: 14px;'>© 2025 Developed by <span style='font-weight: bold;'>Abdullah Ikhlaq </span></br> All Rights Reserved </p>", unsafe_allow_html=True)