Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,218 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import math
|
| 3 |
|
| 4 |
-
# Conversion functions
|
| 5 |
-
#
|
| 6 |
-
def micrometers_to_nanometers(micrometers):
|
| 7 |
-
return micrometers * 1000
|
| 8 |
-
|
| 9 |
-
def angstroms_to_nanometers(angstroms):
|
| 10 |
-
return angstroms * 0.1
|
| 11 |
-
|
| 12 |
-
def light_years_to_kilometers(light_years):
|
| 13 |
-
return light_years * 9.461e12
|
| 14 |
-
|
| 15 |
-
def au_to_kilometers(au):
|
| 16 |
-
return au * 1.496e8
|
| 17 |
-
|
| 18 |
-
def parsecs_to_light_years(parsecs):
|
| 19 |
-
return parsecs * 3.262
|
| 20 |
-
|
| 21 |
-
def chains_to_meters(chains):
|
| 22 |
-
return chains * 20.1168
|
| 23 |
-
|
| 24 |
-
def furlongs_to_meters(furlongs):
|
| 25 |
-
return furlongs * 201.168
|
| 26 |
-
|
| 27 |
-
def meter_to_millimeter(meters):
|
| 28 |
-
return meters * 1000
|
| 29 |
-
|
| 30 |
-
def millimeter_to_meter(millimeters):
|
| 31 |
-
return millimeters / 1000
|
| 32 |
-
|
| 33 |
-
def kilometer_to_meter(kilometers):
|
| 34 |
-
return kilometers * 1000
|
| 35 |
-
|
| 36 |
-
def meter_to_kilometer(meters):
|
| 37 |
-
return meters / 1000
|
| 38 |
-
|
| 39 |
-
def centimeter_to_meter(centimeters):
|
| 40 |
-
return centimeters / 100
|
| 41 |
-
|
| 42 |
-
def meter_to_centimeter(meters):
|
| 43 |
-
return meters * 100
|
| 44 |
-
|
| 45 |
-
# Weight conversions
|
| 46 |
-
def gram_to_kilogram(grams):
|
| 47 |
-
return grams / 1000
|
| 48 |
-
|
| 49 |
-
def kilogram_to_gram(kilograms):
|
| 50 |
-
return kilograms * 1000
|
| 51 |
-
|
| 52 |
-
def milligram_to_gram(milligrams):
|
| 53 |
-
return milligrams / 1000
|
| 54 |
-
|
| 55 |
-
def gram_to_milligram(grams):
|
| 56 |
-
return grams * 1000
|
| 57 |
-
|
| 58 |
-
def pound_to_kilogram(pounds):
|
| 59 |
-
return pounds * 0.453592
|
| 60 |
-
|
| 61 |
-
def kilogram_to_pound(kilograms):
|
| 62 |
-
return kilograms * 2.20462
|
| 63 |
-
|
| 64 |
-
def ounce_to_gram(ounces):
|
| 65 |
-
return ounces * 28.3495
|
| 66 |
-
|
| 67 |
-
def gram_to_ounce(grams):
|
| 68 |
-
return grams * 0.035274
|
| 69 |
-
|
| 70 |
-
# Time conversions
|
| 71 |
-
def seconds_to_minutes(seconds):
|
| 72 |
-
return seconds / 60
|
| 73 |
-
|
| 74 |
-
def minutes_to_seconds(minutes):
|
| 75 |
-
return minutes * 60
|
| 76 |
-
|
| 77 |
-
def hours_to_minutes(hours):
|
| 78 |
-
return hours * 60
|
| 79 |
-
|
| 80 |
-
def minutes_to_hours(minutes):
|
| 81 |
-
return minutes / 60
|
| 82 |
-
|
| 83 |
-
def days_to_hours(days):
|
| 84 |
-
return days * 24
|
| 85 |
-
|
| 86 |
-
def hours_to_days(hours):
|
| 87 |
-
return hours / 24
|
| 88 |
-
|
| 89 |
-
# Temperature conversions
|
| 90 |
-
def celsius_to_fahrenheit(celsius):
|
| 91 |
-
return (celsius * 9/5) + 32
|
| 92 |
-
|
| 93 |
-
def fahrenheit_to_celsius(fahrenheit):
|
| 94 |
-
return (fahrenheit - 32) * 5/9
|
| 95 |
-
|
| 96 |
-
def celsius_to_kelvin(celsius):
|
| 97 |
-
return celsius + 273.15
|
| 98 |
-
|
| 99 |
-
def kelvin_to_celsius(kelvin):
|
| 100 |
-
return kelvin - 273.15
|
| 101 |
-
|
| 102 |
-
def fahrenheit_to_kelvin(fahrenheit):
|
| 103 |
-
return (fahrenheit - 32) * 5/9 + 273.15
|
| 104 |
-
|
| 105 |
-
def kelvin_to_fahrenheit(kelvin):
|
| 106 |
-
return (kelvin - 273.15) * 9/5 + 32
|
| 107 |
-
|
| 108 |
-
# Pressure conversions
|
| 109 |
-
def pascal_to_atm(pascals):
|
| 110 |
-
return pascals / 101325
|
| 111 |
-
|
| 112 |
-
def atm_to_pascal(atms):
|
| 113 |
-
return atms * 101325
|
| 114 |
-
|
| 115 |
-
def pascal_to_bar(pascals):
|
| 116 |
-
return pascals / 100000
|
| 117 |
-
|
| 118 |
-
def bar_to_pascal(bars):
|
| 119 |
-
return bars * 100000
|
| 120 |
-
|
| 121 |
-
def psi_to_pascal(psi):
|
| 122 |
-
return psi * 6894.76
|
| 123 |
-
|
| 124 |
-
def pascal_to_psi(pascals):
|
| 125 |
-
return pascals / 6894.76
|
| 126 |
-
|
| 127 |
-
# Energy conversions
|
| 128 |
-
def joule_to_calorie(joules):
|
| 129 |
-
return joules / 4.184
|
| 130 |
-
|
| 131 |
-
def calorie_to_joule(calories):
|
| 132 |
-
return calories * 4.184
|
| 133 |
-
|
| 134 |
-
def joule_to_kilojoule(joules):
|
| 135 |
-
return joules / 1000
|
| 136 |
-
|
| 137 |
-
def kilojoule_to_joule(kilojoules):
|
| 138 |
-
return kilojoules * 1000
|
| 139 |
-
|
| 140 |
-
def watt_hour_to_joule(watt_hours):
|
| 141 |
-
return watt_hours * 3600
|
| 142 |
-
|
| 143 |
-
def joule_to_watt_hour(joules):
|
| 144 |
-
return joules / 3600
|
| 145 |
-
|
| 146 |
-
# Power conversions
|
| 147 |
-
def watt_to_horsepower(watts):
|
| 148 |
-
return watts / 745.7
|
| 149 |
-
|
| 150 |
-
def horsepower_to_watt(horsepower):
|
| 151 |
-
return horsepower * 745.7
|
| 152 |
-
|
| 153 |
-
def watt_to_kilowatt(watts):
|
| 154 |
-
return watts / 1000
|
| 155 |
-
|
| 156 |
-
def kilowatt_to_watt(kilowatts):
|
| 157 |
-
return kilowatts * 1000
|
| 158 |
-
|
| 159 |
-
def joule_per_second_to_watt(joules_per_second):
|
| 160 |
-
return joules_per_second
|
| 161 |
-
|
| 162 |
-
def watt_to_joule_per_second(watts):
|
| 163 |
-
return watts
|
| 164 |
-
|
| 165 |
-
# Volume conversions
|
| 166 |
-
def liter_to_milliliter(liters):
|
| 167 |
-
return liters * 1000
|
| 168 |
-
|
| 169 |
-
def milliliter_to_liter(milliliters):
|
| 170 |
-
return milliliters / 1000
|
| 171 |
-
|
| 172 |
-
def gallon_to_liter(gallons):
|
| 173 |
-
return gallons * 3.78541
|
| 174 |
-
|
| 175 |
-
def liter_to_gallon(liters):
|
| 176 |
-
return liters / 3.78541
|
| 177 |
-
|
| 178 |
-
def cubic_meter_to_liter(cubic_meters):
|
| 179 |
-
return cubic_meters * 1000
|
| 180 |
-
|
| 181 |
-
def liter_to_cubic_meter(liters):
|
| 182 |
-
return liters / 1000
|
| 183 |
-
|
| 184 |
-
# Speed conversions
|
| 185 |
-
def kilometer_per_hour_to_meter_per_second(kmph):
|
| 186 |
-
return kmph / 3.6
|
| 187 |
-
|
| 188 |
-
def meter_per_second_to_kilometer_per_hour(mps):
|
| 189 |
-
return mps * 3.6
|
| 190 |
-
|
| 191 |
-
def mile_per_hour_to_kilometer_per_hour(mph):
|
| 192 |
-
return mph * 1.60934
|
| 193 |
-
|
| 194 |
-
def kilometer_per_hour_to_mile_per_hour(kmph):
|
| 195 |
-
return kmph / 1.60934
|
| 196 |
-
|
| 197 |
-
# Angle conversions
|
| 198 |
-
def radians_to_degrees(radians):
|
| 199 |
-
return radians * (180 / math.pi)
|
| 200 |
-
|
| 201 |
-
def degrees_to_radians(degrees):
|
| 202 |
-
return degrees * (math.pi / 180)
|
| 203 |
-
|
| 204 |
-
# Frequency conversions
|
| 205 |
-
def hertz_to_kilohertz(hertz):
|
| 206 |
-
return hertz / 1000
|
| 207 |
-
|
| 208 |
-
def kilohertz_to_hertz(kilohertz):
|
| 209 |
-
return kilohertz * 1000
|
| 210 |
-
|
| 211 |
-
def hertz_to_megahertz(hertz):
|
| 212 |
-
return hertz / 1e6
|
| 213 |
-
|
| 214 |
-
def megahertz_to_hertz(megahertz):
|
| 215 |
-
return megahertz * 1e6
|
| 216 |
|
| 217 |
# Streamlit UI
|
| 218 |
st.title("Unit Conversion App 🧮")
|
|
@@ -232,30 +22,109 @@ category = st.selectbox("Choose a conversion category", [
|
|
| 232 |
"Frequency 📶"
|
| 233 |
])
|
| 234 |
|
| 235 |
-
#
|
| 236 |
-
|
| 237 |
-
"
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
"
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
"
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
"
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import math
|
| 3 |
|
| 4 |
+
# Conversion functions (same as before)
|
| 5 |
+
# Ensure all conversion functions are defined.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Streamlit UI
|
| 8 |
st.title("Unit Conversion App 🧮")
|
|
|
|
| 22 |
"Frequency 📶"
|
| 23 |
])
|
| 24 |
|
| 25 |
+
# Conversion selection based on category
|
| 26 |
+
conversion_dict = {
|
| 27 |
+
"Length 📏": [
|
| 28 |
+
"Micrometers to Nanometers", "Angstroms to Nanometers",
|
| 29 |
+
"Light-Years to Kilometers", "Astronomical Units (AU) to Kilometers",
|
| 30 |
+
"Parsecs to Light-Years", "Chains to Meters",
|
| 31 |
+
"Furlongs to Meters", "Meter to Millimeter",
|
| 32 |
+
"Millimeter to Meter", "Kilometer to Meter",
|
| 33 |
+
"Meter to Kilometer", "Centimeter to Meter",
|
| 34 |
+
"Meter to Centimeter"
|
| 35 |
+
],
|
| 36 |
+
"Weight ⚖️": [
|
| 37 |
+
"Gram to Kilogram", "Kilogram to Gram",
|
| 38 |
+
"Milligram to Gram", "Gram to Milligram",
|
| 39 |
+
"Pound to Kilogram", "Kilogram to Pound",
|
| 40 |
+
"Ounce to Gram", "Gram to Ounce"
|
| 41 |
+
],
|
| 42 |
+
"Time ⏰": [
|
| 43 |
+
"Seconds to Minutes", "Minutes to Seconds",
|
| 44 |
+
"Hours to Minutes", "Minutes to Hours",
|
| 45 |
+
"Days to Hours", "Hours to Days"
|
| 46 |
+
],
|
| 47 |
+
"Temperature 🌡️": [
|
| 48 |
+
"Celsius to Fahrenheit", "Fahrenheit to Celsius",
|
| 49 |
+
"Celsius to Kelvin", "Kelvin to Celsius",
|
| 50 |
+
"Fahrenheit to Kelvin", "Kelvin to Fahrenheit"
|
| 51 |
+
],
|
| 52 |
+
"Pressure 💨": [
|
| 53 |
+
"Pascal to ATM", "ATM to Pascal",
|
| 54 |
+
"Pascal to Bar", "Bar to Pascal",
|
| 55 |
+
"PSI to Pascal", "Pascal to PSI"
|
| 56 |
+
],
|
| 57 |
+
"Energy ⚡": [
|
| 58 |
+
"Joule to Calorie", "Calorie to Joule",
|
| 59 |
+
"Joule to Kilojoule", "Kilojoule to Joule",
|
| 60 |
+
"Watt Hour to Joule", "Joule to Watt Hour"
|
| 61 |
+
],
|
| 62 |
+
"Power 💪": [
|
| 63 |
+
"Watt to Horsepower", "Horsepower to Watt",
|
| 64 |
+
"Watt to Kilowatt", "Kilowatt to Watt",
|
| 65 |
+
"Joule per second to Watt", "Watt to Joule per second"
|
| 66 |
+
],
|
| 67 |
+
"Volume 🧃": [
|
| 68 |
+
"Liter to Milliliter", "Milliliter to Liter",
|
| 69 |
+
"Gallon to Liter", "Liter to Gallon",
|
| 70 |
+
"Cubic Meter to Liter", "Liter to Cubic Meter"
|
| 71 |
+
],
|
| 72 |
+
"Speed 🚗": [
|
| 73 |
+
"Kilometer per Hour to Meter per Second",
|
| 74 |
+
"Meter per Second to Kilometer per Hour",
|
| 75 |
+
"Mile per Hour to Kilometer per Hour",
|
| 76 |
+
"Kilometer per Hour to Mile per Hour"
|
| 77 |
+
],
|
| 78 |
+
"Angle 🔺": [
|
| 79 |
+
"Radians to Degrees", "Degrees to Radians"
|
| 80 |
+
],
|
| 81 |
+
"Frequency 📶": [
|
| 82 |
+
"Hertz to Kilohertz", "Kilohertz to Hertz",
|
| 83 |
+
"Hertz to Megahertz", "Megahertz to Hertz"
|
| 84 |
+
]
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
conversion_type = st.selectbox("Choose a conversion", conversion_dict[category])
|
| 88 |
+
|
| 89 |
+
# Value input
|
| 90 |
+
value = st.number_input("Enter the value to convert", min_value=0.0, value=0.0)
|
| 91 |
+
|
| 92 |
+
# Conversion logic
|
| 93 |
+
result = None
|
| 94 |
+
|
| 95 |
+
# Call the corresponding conversion function
|
| 96 |
+
try:
|
| 97 |
+
if category == "Length 📏":
|
| 98 |
+
if conversion_type == "Micrometers to Nanometers":
|
| 99 |
+
result = micrometers_to_nanometers(value)
|
| 100 |
+
elif conversion_type == "Angstroms to Nanometers":
|
| 101 |
+
result = angstroms_to_nanometers(value)
|
| 102 |
+
elif conversion_type == "Light-Years to Kilometers":
|
| 103 |
+
result = light_years_to_kilometers(value)
|
| 104 |
+
elif conversion_type == "Astronomical Units (AU) to Kilometers":
|
| 105 |
+
result = au_to_kilometers(value)
|
| 106 |
+
elif conversion_type == "Parsecs to Light-Years":
|
| 107 |
+
result = parsecs_to_light_years(value)
|
| 108 |
+
elif conversion_type == "Chains to Meters":
|
| 109 |
+
result = chains_to_meters(value)
|
| 110 |
+
elif conversion_type == "Furlongs to Meters":
|
| 111 |
+
result = furlongs_to_meters(value)
|
| 112 |
+
elif conversion_type == "Meter to Millimeter":
|
| 113 |
+
result = meter_to_millimeter(value)
|
| 114 |
+
elif conversion_type == "Millimeter to Meter":
|
| 115 |
+
result = millimeter_to_meter(value)
|
| 116 |
+
elif conversion_type == "Kilometer to Meter":
|
| 117 |
+
result = kilometer_to_meter(value)
|
| 118 |
+
elif conversion_type == "Meter to Kilometer":
|
| 119 |
+
result = meter_to_kilometer(value)
|
| 120 |
+
elif conversion_type == "Centimeter to Meter":
|
| 121 |
+
result = centimeter_to_meter(value)
|
| 122 |
+
elif conversion_type == "Meter to Centimeter":
|
| 123 |
+
result = meter_to_centimeter(value)
|
| 124 |
+
# Continue for other categories
|
| 125 |
+
except Exception as e:
|
| 126 |
+
st.error(f"Error during conversion: {str(e)}")
|
| 127 |
+
|
| 128 |
+
# Display the result
|
| 129 |
+
if result is not None:
|
| 130 |
+
st.markdown(f"**Result:** `{result}`")
|