Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
def convert_temperature(value, from_unit, to_unit):
|
| 4 |
+
"""Converts temperature between Celsius and Fahrenheit."""
|
| 5 |
+
if from_unit == "Celsius":
|
| 6 |
+
if to_unit == "Fahrenheit":
|
| 7 |
+
return (value * 9/5) + 32
|
| 8 |
+
else:
|
| 9 |
+
return value # No conversion needed for Celsius to Celsius
|
| 10 |
+
elif from_unit == "Fahrenheit":
|
| 11 |
+
if to_unit == "Celsius":
|
| 12 |
+
return (value - 32) * 5/9
|
| 13 |
+
else:
|
| 14 |
+
return value # No conversion needed for Fahrenheit to Fahrenheit
|
| 15 |
+
else:
|
| 16 |
+
raise ValueError("Invalid temperature unit")
|
| 17 |
+
|
| 18 |
+
st.title("Temperature Converter")
|
| 19 |
+
|
| 20 |
+
# Input temperature value
|
| 21 |
+
temperature_value = st.number_input("Enter temperature:", min_value=-273.15, step=0.1) # Allow negative values for Kelvin
|
| 22 |
+
|
| 23 |
+
# Dropdown menus for unit selection
|
| 24 |
+
unit_options = ["Celsius", "Fahrenheit"]
|
| 25 |
+
from_unit = st.selectbox("From Unit:", options=unit_options)
|
| 26 |
+
to_unit = st.selectbox("To Unit:", options=unit_options)
|
| 27 |
+
|
| 28 |
+
# Convert button
|
| 29 |
+
if st.button("Convert"):
|
| 30 |
+
try:
|
| 31 |
+
converted_value = convert_temperature(temperature_value, from_unit, to_unit)
|
| 32 |
+
st.success(f"{temperature_value} {from_unit} is equal to {converted_value:.2f} {to_unit}") # Display result with 2 decimal places
|
| 33 |
+
except ValueError as e:
|
| 34 |
+
st.error(f"Error: {e}")
|