Spaces:
Sleeping
Sleeping
| # Define the function for joule per second to watt conversion | |
| def joule_per_second_to_watt(joules_per_second): | |
| return joules_per_second # 1 joule per second = 1 watt | |
| import streamlit as st | |
| import math | |
| # Length conversions | |
| def micrometers_to_nanometers(micrometers): | |
| return micrometers * 1000 | |
| def angstroms_to_nanometers(angstroms): | |
| return angstroms * 0.1 | |
| def light_years_to_kilometers(light_years): | |
| return light_years * 9.461e12 | |
| def au_to_kilometers(au): | |
| return au * 1.496e8 | |
| def parsecs_to_light_years(parsecs): | |
| return parsecs * 3.262 | |
| def chains_to_meters(chains): | |
| return chains * 20.1168 | |
| def furlongs_to_meters(furlongs): | |
| return furlongs * 201.168 | |
| def meter_to_millimeter(meters): | |
| return meters * 1000 | |
| def millimeter_to_meter(millimeters): | |
| return millimeters / 1000 | |
| def kilometer_to_meter(kilometers): | |
| return kilometers * 1000 | |
| def meter_to_kilometer(meters): | |
| return meters / 1000 | |
| def centimeter_to_meter(centimeters): | |
| return centimeters / 100 | |
| def meter_to_centimeter(meters): | |
| return meters * 100 | |
| # Weight conversions | |
| def gram_to_kilogram(grams): | |
| return grams / 1000 | |
| def kilogram_to_gram(kilograms): | |
| return kilograms * 1000 | |
| def milligram_to_gram(milligrams): | |
| return milligrams / 1000 | |
| def gram_to_milligram(grams): | |
| return grams * 1000 | |
| def pound_to_kilogram(pounds): | |
| return pounds * 0.453592 | |
| def kilogram_to_pound(kilograms): | |
| return kilograms * 2.20462 | |
| def ounce_to_gram(ounces): | |
| return ounces * 28.3495 | |
| def gram_to_ounce(grams): | |
| return grams * 0.035274 | |
| # Time conversions | |
| def seconds_to_minutes(seconds): | |
| return seconds / 60 | |
| def minutes_to_seconds(minutes): | |
| return minutes * 60 | |
| def hours_to_minutes(hours): | |
| return hours * 60 | |
| def minutes_to_hours(minutes): | |
| return minutes / 60 | |
| def days_to_hours(days): | |
| return days * 24 | |
| def hours_to_days(hours): | |
| return hours / 24 | |
| # Temperature conversions | |
| 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 (fahrenheit - 32) * 5/9 + 273.15 | |
| def kelvin_to_fahrenheit(kelvin): | |
| return (kelvin - 273.15) * 9/5 + 32 | |
| # Pressure conversions | |
| def pascal_to_atm(pascals): | |
| return pascals / 101325 | |
| def atm_to_pascal(atms): | |
| return atms * 101325 | |
| def pascal_to_bar(pascals): | |
| return pascals / 100000 | |
| def bar_to_pascal(bars): | |
| return bars * 100000 | |
| def psi_to_pascal(psi): | |
| return psi * 6894.76 | |
| def pascal_to_psi(pascals): | |
| return pascals / 6894.76 | |
| # Energy conversions | |
| def joule_to_calorie(joules): | |
| return joules / 4.184 | |
| def calorie_to_joule(calories): | |
| return calories * 4.184 | |
| def joule_to_kilojoule(joules): | |
| return joules / 1000 | |
| def kilojoule_to_joule(kilojoules): | |
| return kilojoules * 1000 | |
| def watt_hour_to_joule(watt_hours): | |
| return watt_hours * 3600 | |
| def joule_to_watt_hour(joules): | |
| return joules / 3600 | |
| # Power conversions | |
| def watt_to_horsepower(watts): | |
| return watts / 745.7 | |
| def horsepower_to_watt(horsepower): | |
| return horsepower * 745.7 | |
| def watt_to_kilowatt(watts): | |
| return watts / 1000 | |
| def kilowatt_to_watt(kilowatts): | |
| return kilowatts * 1000 | |
| # Volume conversions | |
| def liter_to_milliliter(liters): | |
| return liters * 1000 | |
| def milliliter_to_liter(milliliters): | |
| return milliliters / 1000 | |
| def gallon_to_liter(gallons): | |
| return gallons * 3.78541 | |
| def liter_to_gallon(liters): | |
| return liters / 3.78541 | |
| def cubic_meter_to_liter(cubic_meters): | |
| return cubic_meters * 1000 | |
| def liter_to_cubic_meter(liters): | |
| return liters / 1000 | |
| # Speed conversions | |
| def kilometer_per_hour_to_meter_per_second(kmph): | |
| return kmph / 3.6 | |
| def meter_per_second_to_kilometer_per_hour(mps): | |
| return mps * 3.6 | |
| def mile_per_hour_to_kilometer_per_hour(mph): | |
| return mph * 1.60934 | |
| def kilometer_per_hour_to_mile_per_hour(kmph): | |
| return kmph / 1.60934 | |
| # Angle conversions | |
| def radians_to_degrees(radians): | |
| return radians * (180 / math.pi) | |
| def degrees_to_radians(degrees): | |
| return degrees * (math.pi / 180) | |
| # Frequency conversions | |
| def hertz_to_kilohertz(hertz): | |
| return hertz / 1000 | |
| def kilohertz_to_hertz(kilohertz): | |
| return kilohertz * 1000 | |
| def hertz_to_megahertz(hertz): | |
| return hertz / 1e6 | |
| def megahertz_to_hertz(megahertz): | |
| return megahertz * 1e6 | |
| # Streamlit UI | |
| st.title("Unit Conversion App 🧮") | |
| # Add selectbox for the type of conversion category | |
| category = st.selectbox("Choose a conversion category", [ | |
| "Length 📏", | |
| "Weight ⚖️", | |
| "Time ⏰", | |
| "Temperature 🌡️", | |
| "Pressure 💨", | |
| "Energy ⚡", | |
| "Power 💪", | |
| "Volume 🧃", | |
| "Speed 🚗", | |
| "Angle 🔺", | |
| "Frequency 📶" | |
| ]) | |
| # Conversion selection based on category | |
| # Full updated set of handlers | |
| conversions = { | |
| "Length 📏": [ | |
| ("Micrometers to Nanometers", micrometers_to_nanometers), | |
| ("Angstroms to Nanometers", angstroms_to_nanometers), | |
| ("Light-Years to Kilometers", light_years_to_kilometers), | |
| ("Astronomical Units (AU) to Kilometers", au_to_kilometers), | |
| ("Parsecs to Light-Years", parsecs_to_light_years), | |
| ("Chains to Meters", chains_to_meters), | |
| ("Furlongs to Meters", furlongs_to_meters), | |
| ("Meter to Millimeter", meter_to_millimeter), | |
| ("Millimeter to Meter", millimeter_to_meter), | |
| ("Kilometer to Meter", kilometer_to_meter), | |
| ("Meter to Kilometer", meter_to_kilometer), | |
| ("Centimeter to Meter", centimeter_to_meter), | |
| ("Meter to Centimeter", meter_to_centimeter), | |
| ], | |
| "Weight ⚖️": [ | |
| ("Gram to Kilogram", gram_to_kilogram), | |
| ("Kilogram to Gram", kilogram_to_gram), | |
| ("Milligram to Gram", milligram_to_gram), | |
| ("Gram to Milligram", gram_to_milligram), | |
| ("Pound to Kilogram", pound_to_kilogram), | |
| ("Kilogram to Pound", kilogram_to_pound), | |
| ("Ounce to Gram", ounce_to_gram), | |
| ("Gram to Ounce", gram_to_ounce), | |
| ], | |
| "Time ⏰": [ | |
| ("Seconds to Minutes", seconds_to_minutes), | |
| ("Minutes to Seconds", minutes_to_seconds), | |
| ("Hours to Minutes", hours_to_minutes), | |
| ("Minutes to Hours", minutes_to_hours), | |
| ("Days to Hours", days_to_hours), | |
| ("Hours to Days", hours_to_days), | |
| ], | |
| "Temperature 🌡️": [ | |
| ("Celsius to Fahrenheit", celsius_to_fahrenheit), | |
| ("Fahrenheit to Celsius", fahrenheit_to_celsius), | |
| ("Celsius to Kelvin", celsius_to_kelvin), | |
| ("Kelvin to Celsius", kelvin_to_celsius), | |
| ("Fahrenheit to Kelvin", fahrenheit_to_kelvin), | |
| ("Kelvin to Fahrenheit", kelvin_to_fahrenheit), | |
| ], | |
| "Pressure 💨": [ | |
| ("Pascal to ATM", pascal_to_atm), | |
| ("ATM to Pascal", atm_to_pascal), | |
| ("Pascal to Bar", pascal_to_bar), | |
| ("Bar to Pascal", bar_to_pascal), | |
| ("PSI to Pascal", psi_to_pascal), | |
| ("Pascal to PSI", pascal_to_psi), | |
| ], | |
| "Energy ⚡": [ | |
| ("Joule to Calorie", joule_to_calorie), | |
| ("Calorie to Joule", calorie_to_joule), | |
| ("Joule to Kilojoule", joule_to_kilojoule), | |
| ("Kilojoule to Joule", kilojoule_to_joule), | |
| ("Watt Hour to Joule", watt_hour_to_joule), | |
| ("Joule to Watt Hour", joule_to_watt_hour), | |
| ], | |
| "Power 💪": [ | |
| ("Watt to Horsepower", watt_to_horsepower), | |
| ("Horsepower to Watt", horsepower_to_watt), | |
| ("Watt to Kilowatt", watt_to_kilowatt), | |
| ("Kilowatt to Watt", kilowatt_to_watt), | |
| ("Joule per second to Watt", joule_per_second_to_watt), | |
| ("Watt to Joule per second", watt_to_joule_per_second), | |
| ], | |
| "Volume 🧃": [ | |
| ("Liter to Milliliter", liter_to_milliliter), | |
| ("Milliliter to Liter", milliliter_to_liter), | |
| ("Gallon to Liter", gallon_to_liter), | |
| ("Liter to Gallon", liter_to_gallon), | |
| ("Cubic Meter to Liter", cubic_meter_to_liter), | |
| ("Liter to Cubic Meter", liter_to_cubic_meter), | |
| ], | |
| "Speed 🚗": [ | |
| ("Kilometer per Hour to Meter per Second", kilometer_per_hour_to_meter_per_second), | |
| ("Meter per Second to Kilometer per Hour", meter_per_second_to_kilometer_per_hour), | |
| ("Mile per Hour to Kilometer per Hour", mile_per_hour_to_kilometer_per_hour), | |
| ("Kilometer per Hour to Mile per Hour", kilometer_per_hour_to_mile_per_hour), | |
| ], | |
| "Angle 🔺": [ | |
| ("Radians to Degrees", radians_to_degrees), | |
| ("Degrees to Radians", degrees_to_radians), | |
| ], | |
| "Frequency 📶": [ | |
| ("Hertz to Kilohertz", hertz_to_kilohertz), | |
| ("Kilohertz to Hertz", kilohertz_to_hertz), | |
| ("Hertz to Megahertz", hertz_to_megahertz), | |
| ("Megahertz to Hertz", megahertz_to_hertz), | |
| ], | |
| } | |
| # User selection for conversion | |
| conversion_type = st.selectbox( | |
| f"Choose a {category.lower()} conversion", | |
| conversions[category], | |
| format_func=lambda x: x[0] | |
| ) | |
| # Input for conversion | |
| value = st.number_input("Enter the value to convert", min_value=0.0) | |
| # Perform conversion | |
| if st.button("Convert"): | |
| func = conversion_type[1] | |
| result = func(value) | |
| st.success(f"The result is: {result}") | |
| # Full updated set of handlers | |