Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
def convert_temperature(value, from_unit, to_unit):
|
| 4 |
+
if from_unit == to_unit:
|
| 5 |
+
return value
|
| 6 |
+
|
| 7 |
+
# Convert to Celsius
|
| 8 |
+
if from_unit == "Fahrenheit":
|
| 9 |
+
value = (value - 32) * 5 / 9
|
| 10 |
+
elif from_unit == "Kelvin":
|
| 11 |
+
value = value - 273.15
|
| 12 |
+
|
| 13 |
+
# Convert from Celsius to target unit
|
| 14 |
+
if to_unit == "Fahrenheit":
|
| 15 |
+
return value * 9 / 5 + 32
|
| 16 |
+
elif to_unit == "Kelvin":
|
| 17 |
+
return value + 273.15
|
| 18 |
+
else:
|
| 19 |
+
return value
|
| 20 |
+
|
| 21 |
+
# Streamlit UI
|
| 22 |
+
st.title("🌡️ Temperature Converter")
|
| 23 |
+
|
| 24 |
+
st.write("Convert temperatures between Celsius, Fahrenheit, and Kelvin with ease.")
|
| 25 |
+
|
| 26 |
+
# Input temperature
|
| 27 |
+
temp_value = st.number_input("Enter the temperature:", value=0.0, step=0.1, format="%.2f")
|
| 28 |
+
|
| 29 |
+
# Units selection
|
| 30 |
+
units = ["Celsius", "Fahrenheit", "Kelvin"]
|
| 31 |
+
from_unit = st.selectbox("From Unit:", units)
|
| 32 |
+
to_unit = st.selectbox("To Unit:", units)
|
| 33 |
+
|
| 34 |
+
# Convert button
|
| 35 |
+
if st.button("Convert"):
|
| 36 |
+
converted_temp = convert_temperature(temp_value, from_unit, to_unit)
|
| 37 |
+
st.success(f"The converted temperature is: **{converted_temp:.2f} {to_unit}**")
|