Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| # Custom CSS for the UI | |
| st.markdown(""" | |
| <style> | |
| /* Change the primary color of the app */ | |
| .stButton>button { | |
| background-color: #1E90FF; /* Primary color */ | |
| color: white; | |
| font-size: 16px; | |
| font-weight: bold; | |
| } | |
| .stButton>button:hover { | |
| background-color: #4682B4; /* Darker shade on hover */ | |
| } | |
| /* Change input field style */ | |
| .stNumberInput>div>input { | |
| background-color: #f0f8ff; | |
| border-radius: 8px; | |
| padding: 10px; | |
| font-size: 16px; | |
| } | |
| /* Custom font for the title */ | |
| h1 { | |
| font-family: 'Arial', sans-serif; | |
| color: #2E8B57; | |
| } | |
| /* Styling the select box */ | |
| .stSelectbox>div>input { | |
| background-color: #f0f8ff; | |
| border-radius: 8px; | |
| padding: 10px; | |
| font-size: 16px; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Function to convert temperatures | |
| def convert_temperature(value, from_unit, to_unit): | |
| if from_unit == to_unit: | |
| return value | |
| # Convert to Celsius | |
| if from_unit == "Fahrenheit": | |
| value = (value - 32) * 5 / 9 | |
| elif from_unit == "Kelvin": | |
| value = value - 273.15 | |
| # Convert from Celsius to target unit | |
| if to_unit == "Fahrenheit": | |
| return value * 9 / 5 + 32 | |
| elif to_unit == "Kelvin": | |
| return value + 273.15 | |
| else: | |
| return value | |
| # Streamlit UI | |
| st.title("🌡️ Temperature Converter") | |
| st.write("Convert temperatures between Celsius, Fahrenheit, and Kelvin with ease.") | |
| # Input temperature | |
| temp_value = st.number_input("Enter the temperature:", value=0.0, step=0.1, format="%.2f") | |
| # Units selection | |
| units = ["Celsius", "Fahrenheit", "Kelvin"] | |
| from_unit = st.selectbox("From Unit:", units) | |
| to_unit = st.selectbox("To Unit:", units) | |
| # Convert button | |
| if st.button("Convert"): | |
| converted_temp = convert_temperature(temp_value, from_unit, to_unit) | |
| st.success(f"The converted temperature is: **{converted_temp:.2f} {to_unit}**") | |