Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| def convert_temperature(value, from_scale, to_scale): | |
| try: | |
| value = float(value) | |
| # Same unit conversion | |
| if from_scale == to_scale: | |
| return value | |
| # Celsius conversions | |
| if from_scale == "Celsius": | |
| if to_scale == "Kelvin": | |
| return value + 273.15 | |
| elif to_scale == "Fahrenheit": | |
| return (value * 9 / 5) + 32 | |
| elif to_scale == "Rankine": | |
| return (value + 273.15) * 9 / 5 | |
| # Kelvin conversions | |
| if from_scale == "Kelvin": | |
| if to_scale == "Celsius": | |
| return value - 273.15 | |
| elif to_scale == "Fahrenheit": | |
| return (value - 273.15) * 9 / 5 + 32 | |
| elif to_scale == "Rankine": | |
| return value * 9 / 5 | |
| # Fahrenheit conversions | |
| if from_scale == "Fahrenheit": | |
| if to_scale == "Celsius": | |
| return (value - 32) * 5 / 9 | |
| elif to_scale == "Kelvin": | |
| return (value - 32) * 5 / 9 + 273.15 | |
| elif to_scale == "Rankine": | |
| return value + 459.67 | |
| # Rankine conversions | |
| if from_scale == "Rankine": | |
| if to_scale == "Celsius": | |
| return (value - 491.67) * 5 / 9 | |
| elif to_scale == "Kelvin": | |
| return value * 5 / 9 | |
| elif to_scale == "Fahrenheit": | |
| return value - 459.67 | |
| return "Conversion not supported!" | |
| except ValueError: | |
| return "Invalid input! Please enter a numeric value." | |
| # Streamlit App GUI | |
| st.title("Temperature Conversion App") | |
| st.subheader("Developed by Ahmad Hassan") | |
| st.caption("Supervised by Dr. Hidayatullah Mahar") | |
| # Input fields | |
| temp_value = st.text_input("Enter Temperature Value:") | |
| from_scale = st.selectbox("Convert From", ["Celsius", "Kelvin", "Fahrenheit", "Rankine"]) | |
| to_scale = st.selectbox("Convert To", ["Celsius", "Kelvin", "Fahrenheit", "Rankine"]) | |
| # Conversion logic | |
| if st.button("Convert"): | |
| result = convert_temperature(temp_value, from_scale, to_scale) | |
| st.success(f"Converted Value: {result}") |