import streamlit as st to_km_conversions = { 'Kilometer':lambda x:x, "Meter": lambda x: x / 1000, "Centimeter": lambda x: x / 100000, "Millimeter": lambda x: x / 1000000, "Mile": lambda x: x * 1.609, # Add more conversions to kilometers here } from_km_conversions = { 'Kilometer':lambda x:x, "Meter": lambda x: x * 1000, "Centimeter": lambda x: x * 100000, "Millimeter": lambda x: x * 1000000, "Mile": lambda x: x / 1.609, # Add more conversions from kilometers here } from_second={ 'Second': lambda x: x, 'Minute': lambda x: x / 60, 'Hour': lambda x: x / 3600, # Add more conversions to seconds here } to_second={ 'Second': lambda x: x, 'Minute': lambda x: x * 60, 'Hour': lambda x: x * 3600, # Add more conversions to seconds here } to_kg={ 'Kilogram': lambda x: x, 'Gram': lambda x: x / 1000, 'Milligram': lambda x: x / 1000000, # Add more conversions to kilograms here } from_kg={ 'Kilogram': lambda x: x, 'Gram': lambda x: x * 1000, 'Milligram': lambda x: x * 1000000, # Add more conversions to kilograms here } Length = [to_km_conversions,from_km_conversions] Time=[to_second,from_second] Mass=[to_kg,from_kg] def convert_units(value, from_unit, to_unit, unit_type): conversions = globals()[unit_type] # if from_unit not in conversions or to_unit not in conversions[from_unit]: # return "Invalid unit conversion" if from_unit != to_unit: a = conversions[0][from_unit](value) result=conversions[1][to_unit](a) result=round(result,8) else: result = value return result def app(): st.title("Unit Converter") # Get user input value = st.number_input("Enter a value", value=1.0, step=1.0) unit_type = st.selectbox("Unit type", ["Length", "Time", "Mass"]) from_unit = st.selectbox("From unit",list(globals()[unit_type][0].keys())) to_unit=st.selectbox("To unit",list(globals()[unit_type][1].keys())) # Perform conversion result = convert_units(value, from_unit, to_unit, unit_type) # Display the result st.write(f"{value} {from_unit} is equal to {result} {to_unit}") if __name__ == "__main__": app()