Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| def celsius_to_fahrenheit(c): | |
| return (c * 9/5) + 32 | |
| def celsius_to_kelvin(c): | |
| return c + 273.15 | |
| def fahrenheit_to_celsius(f): | |
| return (f - 32) * 5/9 | |
| def fahrenheit_to_kelvin(f): | |
| return (f - 32) * 5/9 + 273.15 | |
| def kelvin_to_celsius(k): | |
| return k - 273.15 | |
| def kelvin_to_fahrenheit(k): | |
| return (k - 273.15) * 9/5 + 32 | |
| # App title | |
| st.title("Temperature Converter") | |
| # Input section | |
| st.sidebar.header("Input") | |
| input_value = st.sidebar.number_input("Enter the temperature value:", value=0.0, format="%.2f") | |
| input_unit = st.sidebar.selectbox("Select the input unit:", ["Celsius", "Fahrenheit", "Kelvin"]) | |
| output_unit = st.sidebar.selectbox("Select the output unit:", ["Celsius", "Fahrenheit", "Kelvin"]) | |
| # Conversion logic | |
| converted_value = None | |
| if input_unit == output_unit: | |
| converted_value = input_value | |
| else: | |
| if input_unit == "Celsius": | |
| if output_unit == "Fahrenheit": | |
| converted_value = celsius_to_fahrenheit(input_value) | |
| elif output_unit == "Kelvin": | |
| converted_value = celsius_to_kelvin(input_value) | |
| elif input_unit == "Fahrenheit": | |
| if output_unit == "Celsius": | |
| converted_value = fahrenheit_to_celsius(input_value) | |
| elif output_unit == "Kelvin": | |
| converted_value = fahrenheit_to_kelvin(input_value) | |
| elif input_unit == "Kelvin": | |
| if output_unit == "Celsius": | |
| converted_value = kelvin_to_celsius(input_value) | |
| elif output_unit == "Fahrenheit": | |
| converted_value = kelvin_to_fahrenheit(input_value) | |
| # Output section | |
| if converted_value is not None: | |
| st.write(f"**Converted Value:** {converted_value:.2f} {output_unit}") | |
| else: | |
| st.write("Unable to convert the temperature.") | |
| st.sidebar.write("\n\n**Tip:** Select different units to see the conversion.") | |