Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import math | |
| def convert_temperature(value, from_unit, to_unit): | |
| if from_unit == "Celsius" and to_unit == "Fahrenheit": | |
| return value * 9 / 5 + 32 | |
| elif from_unit == "Fahrenheit" and to_unit == "Celsius": | |
| return (value - 32) * 5 / 9 | |
| elif from_unit == "Celsius" and to_unit == "Kelvin": | |
| return value + 273.15 | |
| elif from_unit == "Kelvin" and to_unit == "Celsius": | |
| return value - 273.15 | |
| elif from_unit == "Fahrenheit" and to_unit == "Kelvin": | |
| return (value - 32) * 5 / 9 + 273.15 | |
| elif from_unit == "Kelvin" and to_unit == "Fahrenheit": | |
| return (value - 273.15) * 9 / 5 + 32 | |
| else: | |
| return value | |
| st.title("Temperature Converter") | |
| st.sidebar.header("Input") | |
| temp_value = st.sidebar.number_input("Enter the temperature:", value=0.0, format="%.2f") | |
| from_unit = st.sidebar.selectbox("From Unit", ["Celsius", "Fahrenheit", "Kelvin"]) | |
| to_unit = st.sidebar.selectbox("To Unit", ["Celsius", "Fahrenheit", "Kelvin"]) | |
| if from_unit == to_unit: | |
| st.warning("The units are the same. Please select different units for conversion.") | |
| else: | |
| converted_temp = convert_temperature(temp_value, from_unit, to_unit) | |
| st.success(f"{temp_value} {from_unit} is equal to {converted_temp:.2f} {to_unit}.") | |