Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Converter Program #97

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ SpeechRecognition==3.10.3
streamlit==1.36.0
tensorflow==2.16.1
qrcode==7.4.2
pint==0.24.2
49 changes: 49 additions & 0 deletions src/apps/pages/programs/ApiPrograms/unit_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import streamlit as st
from pint import UnitRegistry

def units_convert():
ureg = UnitRegistry()
Q_ = ureg.Quantity

UNIT_TYPES = {
"Length": ["meter", "kilometer", "centimeter", "millimeter", "micrometer", "nanometer", "mile", "yard", "foot", "inch", "lightyear"],
"Temperature": ["Celsius", "Kelvin", "Fahrenheit"],
"Area": ["square meter", "square kilometer", "square centimeter", "square millimeter", "hectare", "square mile", "square yard", "square foot", "square inch", "acre"],
"Volume": ["cubic meter", "cubic kilometer", "cubic centimeter", "cubic millimeter", "liter", "milliliter", "gallon", "quart", "pint", "cup", "fluid ounce", "tablespoon", "teaspoon", "cubic mile", "cubic yard", "cubic foot", "cubic inch"],
"Weight": ["kilogram", "gram", "milligram", "pound", "ounce", "carat"],
"Time": ["second", "millisecond", "microsecond", "nanosecond", "minute", "hour", "day", "week", "month", "year"]
}

UNIT_MAPPING = {
"Celsius": "degree_Celsius",
"Fahrenheit": "degree_Fahrenheit",
"Kelvin": "kelvin"
}

def convert_units(value, from_unit, to_unit):
try:
#mainly for temperature units
from_unit = UNIT_MAPPING.get(from_unit, from_unit)
to_unit = UNIT_MAPPING.get(to_unit, to_unit)

value = Q_(value, from_unit)
result = value.to(to_unit)
return result.magnitude, result.units
except Exception as e:
return None, str(e)

st.title("Unit Converter")

value = st.number_input("Enter value", min_value=0.0, format="%.2f")
unit_type = st.selectbox("Select unit type", options=list(UNIT_TYPES.keys()))
from_unit = st.selectbox("From unit", options=UNIT_TYPES[unit_type])
to_unit = st.selectbox("To unit", options=UNIT_TYPES[unit_type])
convert_button = st.button(label="Convert")

if convert_button:
result, error = convert_units(value, from_unit, to_unit)
if result is not None:
st.success(f"{value} {from_unit} = {result} {to_unit}")
else:
st.error(f"Conversion failed: {error}")

8 changes: 6 additions & 2 deletions src/apps/pages/programs/apiProgram.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

def apiPrograms():
st.title('API Programs')
choice = st.selectbox('Select a program to execute', [None, "Jokes", "General Facts", "Gemini ChatBot", "Quote of the Day", "Currency Convertor"])
choice = st.selectbox('Select a program to execute', [None, "Jokes", "General Facts", "Gemini ChatBot", "Quote of the Day", "Currency Convertor", "Unit Convertor"])

st.markdown('---')

Expand All @@ -20,7 +20,11 @@ def apiPrograms():
show_quote()
elif choice == "Currency Convertor":
from src.apps.pages.programs.ApiPrograms.currency import convert
convert()
convert()
elif choice == "Unit Convertor":
from src.apps.pages.programs.ApiPrograms.unit_converter import units_convert
units_convert()

else:
st.info("Star this project on [GitHub](https://github.com/Avdhesh-Varshney/Jarvis), if you like it!", icon='⭐')

Expand Down