Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
def main():
|
| 4 |
+
st.title("Temperature Converter")
|
| 5 |
+
st.write("Convert temperatures between Celsius, Fahrenheit, and Kelvin.")
|
| 6 |
+
|
| 7 |
+
# Input fields
|
| 8 |
+
temperature = st.number_input("Enter the temperature to convert", value=0.0)
|
| 9 |
+
from_unit = st.selectbox("From Unit", ["Celsius", "Fahrenheit", "Kelvin"])
|
| 10 |
+
to_unit = st.selectbox("To Unit", ["Celsius", "Fahrenheit", "Kelvin"])
|
| 11 |
+
|
| 12 |
+
# Perform conversion
|
| 13 |
+
if st.button("Convert"):
|
| 14 |
+
try:
|
| 15 |
+
converted_temperature = convert_temperature(temperature, from_unit, to_unit)
|
| 16 |
+
st.success(f"{temperature} {from_unit} = {converted_temperature:.2f} {to_unit}")
|
| 17 |
+
except ValueError as e:
|
| 18 |
+
st.error(e)
|
| 19 |
+
|
| 20 |
+
def convert_temperature(value, from_unit, to_unit):
|
| 21 |
+
"""Converts temperature between Celsius, Fahrenheit, and Kelvin."""
|
| 22 |
+
if from_unit == to_unit:
|
| 23 |
+
return value
|
| 24 |
+
|
| 25 |
+
# Convert to Celsius first
|
| 26 |
+
if from_unit == "Fahrenheit":
|
| 27 |
+
value = (value - 32) * 5 / 9
|
| 28 |
+
elif from_unit == "Kelvin":
|
| 29 |
+
value = value - 273.15
|
| 30 |
+
|
| 31 |
+
# Convert from Celsius to target unit
|
| 32 |
+
if to_unit == "Fahrenheit":
|
| 33 |
+
return value * 9 / 5 + 32
|
| 34 |
+
elif to_unit == "Kelvin":
|
| 35 |
+
return value + 273.15
|
| 36 |
+
else: # to_unit == "Celsius"
|
| 37 |
+
return value
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
main()
|