Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| # Conversion functions | |
| def celsius_to_fahrenheit(celsius): | |
| return (celsius * 9/5) + 32 | |
| def fahrenheit_to_celsius(fahrenheit): | |
| return (fahrenheit - 32) * 5/9 | |
| def celsius_to_kelvin(celsius): | |
| return celsius + 273.15 | |
| def kelvin_to_celsius(kelvin): | |
| return kelvin - 273.15 | |
| def fahrenheit_to_kelvin(fahrenheit): | |
| return celsius_to_kelvin(fahrenheit_to_celsius(fahrenheit)) | |
| def kelvin_to_fahrenheit(kelvin): | |
| return celsius_to_fahrenheit(kelvin_to_celsius(kelvin)) | |
| # Sidebar for navigation | |
| st.sidebar.title("Navigation") | |
| st.sidebar.info("Choose the conversion type and enter the temperature value in the input box below.") | |
| # Main title | |
| st.title("🌡️ Temperature Conversion App") | |
| # Conversion options in sidebar | |
| conversion_type = st.sidebar.radio( | |
| "Choose Conversion Type:", | |
| ( | |
| "Celsius to Fahrenheit", | |
| "Fahrenheit to Celsius", | |
| "Celsius to Kelvin", | |
| "Kelvin to Celsius", | |
| "Fahrenheit to Kelvin", | |
| "Kelvin to Fahrenheit" | |
| ) | |
| ) | |
| # Layout: Use two columns for input and output | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.header("Input") | |
| if conversion_type == "Celsius to Fahrenheit": | |
| celsius = st.number_input("Enter temperature in Celsius:", min_value=-273.15, step=0.1) | |
| if st.button("Convert"): | |
| fahrenheit = celsius_to_fahrenheit(celsius) | |
| with col2: | |
| st.header("Output") | |
| st.success(f"{celsius}°C = {fahrenheit:.2f}°F") | |
| elif conversion_type == "Fahrenheit to Celsius": | |
| fahrenheit = st.number_input("Enter temperature in Fahrenheit:", min_value=-459.67, step=0.1) | |
| if st.button("Convert"): | |
| celsius = fahrenheit_to_celsius(fahrenheit) | |
| with col2: | |
| st.header("Output") | |
| st.success(f"{fahrenheit}°F = {celsius:.2f}°C") | |
| elif conversion_type == "Celsius to Kelvin": | |
| celsius = st.number_input("Enter temperature in Celsius:", min_value=-273.15, step=0.1) | |
| if st.button("Convert"): | |
| kelvin = celsius_to_kelvin(celsius) | |
| with col2: | |
| st.header("Output") | |
| st.success(f"{celsius}°C = {kelvin:.2f} K") | |
| elif conversion_type == "Kelvin to Celsius": | |
| kelvin = st.number_input("Enter temperature in Kelvin:", min_value=0.0, step=0.1) | |
| if st.button("Convert"): | |
| celsius = kelvin_to_celsius(kelvin) | |
| with col2: | |
| st.header("Output") | |
| st.success(f"{kelvin} K = {celsius:.2f}°C") | |
| elif conversion_type == "Fahrenheit to Kelvin": | |
| fahrenheit = st.number_input("Enter temperature in Fahrenheit:", min_value=-459.67, step=0.1) | |
| if st.button("Convert"): | |
| kelvin = fahrenheit_to_kelvin(fahrenheit) | |
| with col2: | |
| st.header("Output") | |
| st.success(f"{fahrenheit}°F = {kelvin:.2f} K") | |
| elif conversion_type == "Kelvin to Fahrenheit": | |
| kelvin = st.number_input("Enter temperature in Kelvin:", min_value=0.0, step=0.1) | |
| if st.button("Convert"): | |
| fahrenheit = kelvin_to_fahrenheit(kelvin) | |
| with col2: | |
| st.header("Output") | |
| st.success(f"{kelvin} K = {fahrenheit:.2f}°F") | |
| # Footer | |
| st.sidebar.markdown("----") | |
| st.sidebar.write("Developed by **Malik Shehram Khan**") | |
| st.sidebar.markdown("🌐 Powered by [Streamlit](https://streamlit.io)") | |