Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
def celsius_to_fahrenheit(c):
|
| 4 |
+
return (c * 9/5) + 32
|
| 5 |
+
|
| 6 |
+
def celsius_to_kelvin(c):
|
| 7 |
+
return c + 273.15
|
| 8 |
+
|
| 9 |
+
def fahrenheit_to_celsius(f):
|
| 10 |
+
return (f - 32) * 5/9
|
| 11 |
+
|
| 12 |
+
def fahrenheit_to_kelvin(f):
|
| 13 |
+
return (f - 32) * 5/9 + 273.15
|
| 14 |
+
|
| 15 |
+
def kelvin_to_celsius(k):
|
| 16 |
+
return k - 273.15
|
| 17 |
+
|
| 18 |
+
def kelvin_to_fahrenheit(k):
|
| 19 |
+
return (k - 273.15) * 9/5 + 32
|
| 20 |
+
|
| 21 |
+
# App title
|
| 22 |
+
st.title("Temperature Converter")
|
| 23 |
+
|
| 24 |
+
# Input section
|
| 25 |
+
st.sidebar.header("Input")
|
| 26 |
+
input_value = st.sidebar.number_input("Enter the temperature value:", value=0.0, format="%.2f")
|
| 27 |
+
input_unit = st.sidebar.selectbox("Select the input unit:", ["Celsius", "Fahrenheit", "Kelvin"])
|
| 28 |
+
output_unit = st.sidebar.selectbox("Select the output unit:", ["Celsius", "Fahrenheit", "Kelvin"])
|
| 29 |
+
|
| 30 |
+
# Conversion logic
|
| 31 |
+
converted_value = None
|
| 32 |
+
if input_unit == output_unit:
|
| 33 |
+
converted_value = input_value
|
| 34 |
+
else:
|
| 35 |
+
if input_unit == "Celsius":
|
| 36 |
+
if output_unit == "Fahrenheit":
|
| 37 |
+
converted_value = celsius_to_fahrenheit(input_value)
|
| 38 |
+
elif output_unit == "Kelvin":
|
| 39 |
+
converted_value = celsius_to_kelvin(input_value)
|
| 40 |
+
elif input_unit == "Fahrenheit":
|
| 41 |
+
if output_unit == "Celsius":
|
| 42 |
+
converted_value = fahrenheit_to_celsius(input_value)
|
| 43 |
+
elif output_unit == "Kelvin":
|
| 44 |
+
converted_value = fahrenheit_to_kelvin(input_value)
|
| 45 |
+
elif input_unit == "Kelvin":
|
| 46 |
+
if output_unit == "Celsius":
|
| 47 |
+
converted_value = kelvin_to_celsius(input_value)
|
| 48 |
+
elif output_unit == "Fahrenheit":
|
| 49 |
+
converted_value = kelvin_to_fahrenheit(input_value)
|
| 50 |
+
|
| 51 |
+
# Output section
|
| 52 |
+
if converted_value is not None:
|
| 53 |
+
st.write(f"**Converted Value:** {converted_value:.2f} {output_unit}")
|
| 54 |
+
else:
|
| 55 |
+
st.write("Unable to convert the temperature.")
|
| 56 |
+
|
| 57 |
+
st.sidebar.write("\n\n**Tip:** Select different units to see the conversion.")
|