From ffdde8b761d5c34d3384f4e7c668cf315bb090a3 Mon Sep 17 00:00:00 2001 From: mehallhm Date: Mon, 10 Jun 2024 09:11:16 +0200 Subject: [PATCH] ui: improved ui consistency --- app/src/pages/01_userhome.py | 37 ++-- app/src/pages/02_userSurvey.py | 167 ++++++++-------- app/src/pages/04_userMatch.py | 1 + app/src/pages/05_userSettings.py | 178 ++++++++--------- app/src/pages/10_enterprisehome.py | 20 +- app/src/pages/11_enterpriseSurvey.py | 282 +++++++++++++-------------- app/src/pages/20_NGOhome.py | 19 +- app/src/pages/21_NGOInfo.py | 257 ++++++++++++------------ 8 files changed, 474 insertions(+), 487 deletions(-) diff --git a/app/src/pages/01_userhome.py b/app/src/pages/01_userhome.py index 55efd42..4c567ed 100644 --- a/app/src/pages/01_userhome.py +++ b/app/src/pages/01_userhome.py @@ -6,22 +6,21 @@ st.header(f"Welcome, {st.session_state['first_name']}") -st.write("### Navigation") - -col1, col2, col3 = st.columns(3) -with col1: - if st.button("## Take Emissions Survey"): - st.switch_page("pages/02_userSurvey.py") - - st.write('') - - if st.button("## View Settings"): - st.switch_page("pages/05_userSettings.py") - -with col2: - if st.button("## View Emissions History"): - st.switch_page("pages/03_userHistory.py") - -with col3: - if st.button("## View NGO Recommendations"): - st.switch_page("pages/04_userMatch.py") +with st.container(border=True): + st.write("### Navigation") + col1, col2, col3, col4 = st.columns(4) + with col1: + if st.button("## Take Emissions Survey"): + st.switch_page("pages/02_userSurvey.py") + + with col2: + if st.button("## View Emissions History"): + st.switch_page("pages/03_userHistory.py") + + with col3: + if st.button("## View NGO Recommendations"): + st.switch_page("pages/04_userMatch.py") + + with col4: + if st.button("## View Settings"): + st.switch_page("pages/05_userSettings.py") diff --git a/app/src/pages/02_userSurvey.py b/app/src/pages/02_userSurvey.py index 6c698a0..346f63a 100644 --- a/app/src/pages/02_userSurvey.py +++ b/app/src/pages/02_userSurvey.py @@ -17,107 +17,106 @@ ) # expandable section that is expanded by default -st.divider() -st.write("### Energy Questions") -# user input section -household_members = st.number_input( - "How many people live in your household?", 1, None, 2 -) - -electricity_usage = ( - 0.0000036 - * 12 - * st.number_input( - "How much electricity does your household use per month (kWh)?", - 0.0, - None, - 6320.0, +with st.container(border=True): + st.write("### Energy Questions") + # user input section + household_members = st.number_input( + "How many people live in your household?", 1, None, 2 ) -) -heating = ( - 0.0000036 - * 12 - * st.number_input( - "How much heating does your household use per month (kWh)?", - 0.0, - None, - 3000.0, + electricity_usage = ( + 0.0000036 + * 12 + * st.number_input( + "How much electricity does your household use per month (kWh)?", + 0.0, + None, + 6320.0, + ) ) -) -water_heating = ( - 0.0000036 - * 12 - * st.number_input( - "How much water heating does your household use per month (kWh)?", - 0.0, - None, - 2000.0, + heating = ( + 0.0000036 + * 12 + * st.number_input( + "How much heating does your household use per month (kWh)?", + 0.0, + None, + 3000.0, + ) ) -) -cooking_gas = ( - 0.0000036 - * 12 - * st.number_input( - "Per month, how much energy is used cooking? (kWh)?", 0.0, None, 2000.0 + water_heating = ( + 0.0000036 + * 12 + * st.number_input( + "How much water heating does your household use per month (kWh)?", + 0.0, + None, + 2000.0, + ) ) -) - -kWh_total = (electricity_usage + heating + water_heating + cooking_gas) / ( - household_members * 0.0000036 * 12 -) -st.write("Your Total Residential Usage (kWh): ", round(kWh_total, 2)) -st.write( - "That's equivalent to running a 60w lightbulb for ", - round((kWh_total * 1000) / 60), - " hours!", -) - -st.divider() -st.write("### Transportation Questions") - -fuel_type = st.select_slider( - "Fuel Type", options=["Gasoline/Hybrid", "Diesel", "Electric"] -) -if fuel_type == "Gasoline/Hybrid": - fuel_capacity = st.number_input( - "How many liters of gasoline does your vehicle hold?", 0.0, None, 50.0 + cooking_gas = ( + 0.0000036 + * 12 + * st.number_input( + "Per month, how much energy is used cooking? (kWh)?", 0.0, None, 2000.0 + ) ) - fuel_used_monthly = st.slider( - "How many times a month do you fill up your tank?", 0, 10, 5 + + kWh_total = (electricity_usage + heating + water_heating + cooking_gas) / ( + household_members * 0.0000036 * 12 ) - fuel_used = fuel_capacity * fuel_used_monthly * 12 * 1.11302e-6 - st.write("Approx. fuel used per year (liters): ", round(fuel_used / 1.11302e-6)) + st.write("Your Total Residential Usage (kWh): ", round(kWh_total, 2)) st.write( - "That's equivalent to ", - round((fuel_used / 1.11302e-6) / 302, 2), - "bathtubs!", + "That's equivalent to running a 60w lightbulb for ", + round((kWh_total * 1000) / 60), + " hours!", ) -elif fuel_type == "Diesel": - fuel_capacity = st.number_input( - "How many liters of diesel does your vehicle hold?", 0.0, None, 50.0 - ) - fuel_used_monthly = st.slider( - "How many times a month do you fill up your tank?", 0, 10, 5 - ) - fuel_used = fuel_capacity * fuel_used_monthly * 12 * 1.1571e-6 - st.write("Approx. fuel used per year (liters): ", fuel_used / 1.1571e-6) - st.write( - "That's equivalent to ", - round((fuel_used / 1.1571e-6) / 302, 2), - "bathtubs!", +with st.container(border=True): + st.write("### Transportation Questions") + + fuel_type = st.select_slider( + "Fuel Type", options=["Gasoline/Hybrid", "Diesel", "Electric"] ) -elif fuel_type == "Electric": - st.write("Please include charging data in residential data.") + if fuel_type == "Gasoline/Hybrid": + fuel_capacity = st.number_input( + "How many liters of gasoline does your vehicle hold?", 0.0, None, 50.0 + ) + fuel_used_monthly = st.slider( + "How many times a month do you fill up your tank?", 0, 10, 5 + ) + fuel_used = fuel_capacity * fuel_used_monthly * 12 * 1.11302e-6 + st.write("Approx. fuel used per year (liters): ", round(fuel_used / 1.11302e-6)) + st.write( + "That's equivalent to ", + round((fuel_used / 1.11302e-6) / 302, 2), + "bathtubs!", + ) + + elif fuel_type == "Diesel": + fuel_capacity = st.number_input( + "How many liters of diesel does your vehicle hold?", 0.0, None, 50.0 + ) + fuel_used_monthly = st.slider( + "How many times a month do you fill up your tank?", 0, 10, 5 + ) + fuel_used = fuel_capacity * fuel_used_monthly * 12 * 1.1571e-6 + st.write("Approx. fuel used per year (liters): ", fuel_used / 1.1571e-6) + st.write( + "That's equivalent to ", + round((fuel_used / 1.1571e-6) / 302, 2), + "bathtubs!", + ) + + elif fuel_type == "Electric": + st.write("Please include charging data in residential data.") -st.divider() -if st.button("View Prediction"): +if st.button("View Prediction", use_container_width=True): country_data = requests.get( "http://api:4000/u/UserCountryCarbon", timeout=10 ).json() diff --git a/app/src/pages/04_userMatch.py b/app/src/pages/04_userMatch.py index 2f457f7..4e3c29c 100644 --- a/app/src/pages/04_userMatch.py +++ b/app/src/pages/04_userMatch.py @@ -8,6 +8,7 @@ matches = requests.get("http://api:4000/n/NGOMatch/1?q=100", timeout=500).json() st.header("My Matches") +st.write("These Organizations have been matched to you base on your bio! Please make sure to update your bio to ensure the best recommendations") st.write("") for match in matches: diff --git a/app/src/pages/05_userSettings.py b/app/src/pages/05_userSettings.py index 6a41643..033d87b 100644 --- a/app/src/pages/05_userSettings.py +++ b/app/src/pages/05_userSettings.py @@ -55,99 +55,99 @@ "http://api:4000/u/UserCountryCarbon", timeout=200 ).json() country_id = current_country_data[0]["id"] -country = st.selectbox("Country :flag-eu:", COUNTRY_FLAGS, index=country_id) -country_id = COUNTRY_FLAGS.index(country) -try: - data = {"country_id": country_id} - response = requests.put("http://api:4000/u/UserCountry", json=data, timeout=300) -except Exception as e: - st.error(f"An error occurred: {e}") - -current_bio = requests.get("http://api:4000/u/UserBio", timeout=200).json()["bio"] - -bio = st.text_area( - "Bio", - help="Enter a quick bio here. Mention some environmental interests if any", - height=100, - max_chars=200, - value=current_bio, -) - -consent = st.toggle( - "NGO Recommendations", - value=1, - help="By selecting this option, you consent to your name and description being shared with organizations that also use CarbonConnect", -) - -user_data = {"consent": consent, "bio": bio} - -if st.button("Submit"): - response = requests.put( - "http://api:4000/u/UserUpdateInfo", json=user_data, timeout=300 - ) - -st.divider() - -def fetch_tag_descriptions(): +with st.container(border=True): + country = st.selectbox("Country :flag-eu:", COUNTRY_FLAGS, index=country_id) + country_id = COUNTRY_FLAGS.index(country) try: - response = requests.get("http://api:4000/u/tags") - response.raise_for_status() - return response.json() - except requests.exceptions.RequestException as e: - st.error(f"Error fetching data: {e}") - return [] - - -data = fetch_tag_descriptions() -st.write("### Tag Settings") -if data: - tags = [tag["description"] for tag in data] - selected = pills("Current tags", tags, [EMOJI_MAP[tag] for tag in tags], index=None) -else: - tags = [] - st.info("No tags available to display.") - -col1, col2 = st.columns(2) - -with col1: - st.write("#### Add Tags") - - options = ["Transport", "Flights", "Energy", "Heat"] - options = [option for option in options if option not in tags] - - selected_tags = st.selectbox("Select your associated tags", options) - - if st.button("Add Tags"): - add_tags_api_url = "http://api:4000/u/TagAdd" - tag_data = {"tag": selected_tags} - try: - response = requests.post(add_tags_api_url, json=tag_data) - if response.status_code == 200: - st.success("Tags successfully added!") - else: - st.error(f"Failed to add tags. Status code: {response.status_code}") - except Exception as e: - st.error(f"An error occurred while adding tags: {e}") - - -with col2: - st.write("#### Delete Tags") + data = {"country_id": country_id} + response = requests.put("http://api:4000/u/UserCountry", json=data, timeout=300) + except Exception as e: + st.error(f"An error occurred: {e}") + + current_bio = requests.get("http://api:4000/u/UserBio", timeout=200).json()["bio"] + + bio = st.text_area( + "Bio", + help="Enter a quick bio here. Mention some environmental interests if any", + height=100, + max_chars=200, + value=current_bio, + ) - options = ["Transport", "Flights", "Energy", "Heat"] - options = [option for option in options if option in tags] + consent = st.toggle( + "NGO Recommendations", + value=1, + help="By selecting this option, you consent to your name and description being shared with organizations that also use CarbonConnect", + ) - selected_tag = st.selectbox("Select tags to delete", options) + user_data = {"consent": consent, "bio": bio} - if st.button("Delete Tags"): - delete_tags_api_url = "http://api:4000/u/TagDelete" - tag_data = {"tag": selected_tag} + if st.button("Submit"): + response = requests.put( + "http://api:4000/u/UserUpdateInfo", json=user_data, timeout=300 + ) +with st.container(border=True): + def fetch_tag_descriptions(): try: - response = requests.delete(delete_tags_api_url, json=tag_data) - if response.status_code == 200: - st.success("Tags successfully deleted!") - else: - st.error(f"Failed to delete tags. Status code: {response.status_code}") - except Exception as e: - st.error(f"An error occurred while deleting tags: {e}") + response = requests.get("http://api:4000/u/tags") + response.raise_for_status() + return response.json() + except requests.exceptions.RequestException as e: + st.error(f"Error fetching data: {e}") + return [] + + + data = fetch_tag_descriptions() + st.write("### Tag Settings") + if data: + tags = [tag["description"] for tag in data] + selected = pills("Current tags", tags, [EMOJI_MAP[tag] for tag in tags], index=None) + else: + tags = [] + st.info("No tags available to display.") + + col1, col2 = st.columns(2) + + with col1: + st.write("#### Add Tags") + + options = ["Transport", "Flights", "Energy", "Heat"] + options = [option for option in options if option not in tags] + + selected_tags = st.selectbox("Select your associated tags", options) + + if st.button("Add Tags"): + add_tags_api_url = "http://api:4000/u/TagAdd" + tag_data = {"tag": selected_tags} + try: + response = requests.post(add_tags_api_url, json=tag_data) + if response.status_code == 200: + st.success("Tags successfully added!") + else: + st.error(f"Failed to add tags. Status code: {response.status_code}") + except Exception as e: + st.error(f"An error occurred while adding tags: {e}") + + + with col2: + st.write("#### Delete Tags") + + options = ["Transport", "Flights", "Energy", "Heat"] + options = [option for option in options if option in tags] + + selected_tag = st.selectbox("Select tags to delete", options) + + if st.button("Delete Tags"): + delete_tags_api_url = "http://api:4000/u/TagDelete" + tag_data = {"tag": selected_tag} + + try: + response = requests.delete(delete_tags_api_url, json=tag_data) + if response.status_code == 200: + st.success("Tags successfully deleted!") + else: + st.error(f"Failed to delete tags. Status code: {response.status_code}") + except Exception as e: + st.error(f"An error occurred while deleting tags: {e}") diff --git a/app/src/pages/10_enterprisehome.py b/app/src/pages/10_enterprisehome.py index fc7c888..e624b9b 100644 --- a/app/src/pages/10_enterprisehome.py +++ b/app/src/pages/10_enterprisehome.py @@ -7,15 +7,13 @@ ("# Welcome, ", f"{st.session_state['first_name']}") -st.write("### Navigation") -col1, col2 = st.columns(2) -with col1: - if st.button("## Complete Emissions Survey"): - st.switch_page("pages/11_enterpriseSurvey.py") - -with col2: - if st.button("## View NGO Recommendations"): - st.switch_page("pages/12_enterpriseMatch.py") - -("#### Role:", f"{st.session_state['role']}") +with st.container(border=True): + st.write("### Navigation") + col1, col2 = st.columns(2) + with col1: + if st.button("## Complete Emissions Survey"): + st.switch_page("pages/11_enterpriseSurvey.py") + with col2: + if st.button("## View NGO Recommendations"): + st.switch_page("pages/12_enterpriseMatch.py") diff --git a/app/src/pages/11_enterpriseSurvey.py b/app/src/pages/11_enterpriseSurvey.py index 04cc070..588eec2 100644 --- a/app/src/pages/11_enterpriseSurvey.py +++ b/app/src/pages/11_enterpriseSurvey.py @@ -11,156 +11,146 @@ st.write("# Enterprise survey") -emission = st.text_input("Emissions in kilotonnes") - -if st.button("Submit Emission"): - url = "http://api:4000/e/EntResult" - data = {"emission": emission} +with st.container(border=True): + emission = st.text_input("Emissions in kilotonnes") + + if st.button("Submit Emission"): + url = "http://api:4000/e/EntResult" + data = {"emission": emission} + try: + response = requests.post(url, json=data) + if response.status_code == 200: + st.success("Successfully Added!") + else: + st.error(f"Failed, Status Code: {response.status_code}") + except Exception as e: + st.error(f"An Error Occurred: {e}") + +with st.container(border=True): + def fetch_tag_descriptions(): + try: + response = requests.get("http://api:4000/e/tags") + response.raise_for_status() + return response.json() + except requests.exceptions.RequestException as e: + st.error(f"Error fetching data: {e}") + return [] + + + data = fetch_tag_descriptions() + + emoji_map = { + "Heat": "🔥", + "Flights": "🛫", + "Energy": "💡", + "Transport": "🚗", + } + + if data: + tags = [tag["description"] for tag in data] + selected = pills("Current tags", tags, [emoji_map[tag] for tag in tags], index=None) + else: + tags = [] + st.info("No tags available to display.") + + col1, col2 = st.columns(2) + + with col1: + + def add_tags(): + st.write("### Add New Enterprise Tags") + + options = ["Transport", "Flights", "Energy", "Heat"] + options = [option for option in options if option not in tags] + + selected_tags = st.selectbox("Select Your Associated Tags", options) + + if st.button("Add Tags"): + add_tags_api_url = "http://api:4000/e/TagAdd" + tag_data = {"tag": selected_tags} + try: + response = requests.post(add_tags_api_url, json=tag_data) + if response.status_code == 200: + st.success("Tags Successfully Added!") + else: + st.error(f"Failed To Add Tags. Status Code: {response.status_code}") + except Exception as e: + st.error(f"An Error Occurred While Adding Tags: {e}") + + add_tags() + + with col2: + + def delete_tags(): + st.write("### Delete NGO Tags") + + options = ["Transport", "Flights", "Energy", "Heat"] + options = [option for option in options if option in tags] + + selected_tag = st.selectbox("Select Tags To Delete", options) + + if st.button("Delete Tags"): + delete_tags_api_url = "http://api:4000/e/TagDelete" + tag_data = {"tag": selected_tag} + + try: + response = requests.delete(delete_tags_api_url, json=tag_data) + if response.status_code == 200: + st.success("Tags Successfully Deleted!") + else: + st.error( + f"Failed To Delete Tags. Status Code: {response.status_code}" + ) + except Exception as e: + st.error(f"An Error Occurred While Deleting Tags: {e}") + + delete_tags() + +with st.container(border=True): + data = {} try: - response = requests.post(url, json=data) - if response.status_code == 200: - st.success("Successfully Added!") - else: - st.error(f"Failed, Status Code: {response.status_code}") - except Exception as e: - st.error(f"An Error Occurred: {e}") - + data = requests.get("http://api:4000/e/EntCompare").json() + except: + st.write("**Important**: Could not connect to sample api, so using dummy data.") + data = {"a": {"b": "123", "c": "hello"}, "z": {"b": "456", "c": "goodbye"}} + + avg_emission = data[0]["AVG Emission (by Country in kilotonnes)"] + country = data[0]["Country"] + your_emissions = data[0]["Your Emissions (in kilotonnes)"] + multiplier = your_emissions / avg_emission + + st.write("") + + st.write( + "In", + country + ", your emissions in kilotonnes is ", + round(your_emissions, 2), + ", while the average enterprise's is ", + round(avg_emission, 2), + ) + st.write( + "That means your emissions are", + round(multiplier, 2), + "times the average enterprise's emissions in " + country, + ) + + st.write("### Enterprise Emission Result History") + + data = [] -def fetch_tag_descriptions(): try: - response = requests.get("http://api:4000/e/tags") + response = requests.get("http://api:4000/e/EnterpriseHistory", timeout=100) response.raise_for_status() - return response.json() + data = response.json() except requests.exceptions.RequestException as e: - st.error(f"Error fetching data: {e}") - return [] - - -data = fetch_tag_descriptions() - -emoji_map = { - "Heat": "🔥", - "Flights": "🛫", - "Energy": "💡", - "Transport": "🚗", -} - -if data: - tags = [tag["description"] for tag in data] - selected = pills("Current tags", tags, [emoji_map[tag] for tag in tags], index=None) -else: - tags = [] - st.info("No tags available to display.") - -col1, col2 = st.columns(2) - -with col1: - - def add_tags(): - st.write("### Add New Enterprise Tags") - - options = ["Transport", "Flights", "Energy", "Heat"] - options = [option for option in options if option not in tags] + st.error(f"An error occurred: {e}") - selected_tags = st.selectbox("Select Your Associated Tags", options) - - if st.button("Add Tags"): - add_tags_api_url = "http://api:4000/e/TagAdd" - tag_data = {"tag": selected_tags} - try: - response = requests.post(add_tags_api_url, json=tag_data) - if response.status_code == 200: - st.success("Tags Successfully Added!") - else: - st.error(f"Failed To Add Tags. Status Code: {response.status_code}") - except Exception as e: - st.error(f"An Error Occurred While Adding Tags: {e}") - - add_tags() - -with col2: - - def delete_tags(): - st.write("### Delete NGO Tags") - - options = ["Transport", "Flights", "Energy", "Heat"] - options = [option for option in options if option in tags] - - selected_tag = st.selectbox("Select Tags To Delete", options) - - if st.button("Delete Tags"): - delete_tags_api_url = "http://api:4000/e/TagDelete" - tag_data = {"tag": selected_tag} - - try: - response = requests.delete(delete_tags_api_url, json=tag_data) - if response.status_code == 200: - st.success("Tags Successfully Deleted!") - else: - st.error( - f"Failed To Delete Tags. Status Code: {response.status_code}" - ) - except Exception as e: - st.error(f"An Error Occurred While Deleting Tags: {e}") - - delete_tags() - -data = {} -try: - data = requests.get("http://api:4000/e/EntCompare").json() -except: - st.write("**Important**: Could not connect to sample api, so using dummy data.") - data = {"a": {"b": "123", "c": "hello"}, "z": {"b": "456", "c": "goodbye"}} - -avg_emission = data[0]["AVG Emission (by Country in kilotonnes)"] -country = data[0]["Country"] -your_emissions = data[0]["Your Emissions (in kilotonnes)"] -multiplier = your_emissions / avg_emission - -st.write("") - -st.write( - "#### In", - country + ", your emissions in kilotonnes is ", - round(your_emissions, 2), - ", while the average enterprise's is ", - round(avg_emission, 2), -) -st.write( - "#### That means your emissions are", - round(multiplier, 2), - "times the average enterprise's emissions in " + country, -) - -# st.write('### Display Enterprise Survey Result History') + if data: + df = pd.DataFrame(data) -# data = {} - -# try: -# data = requests.get('http://api:4000/e/EnterpriseHistory', timeout=100).json() -# except requests.exceptions.RequestException as e: -# st.error(f"An error occurred: {e}") - -# st.dataframe(data) - - -st.write("### Enterprise Emission Result History") - -data = [] - -try: - response = requests.get("http://api:4000/e/EnterpriseHistory", timeout=100) - response.raise_for_status() - data = response.json() -except requests.exceptions.RequestException as e: - st.error(f"An error occurred: {e}") - -if data: - df = pd.DataFrame(data) - - # Automatically display each history result in an expander - for index, row in df.iterrows(): - with st.expander(f"Emission result {index + 1}"): - st.write(row["emission_history"]) -else: - st.write("No data available to display.") + # Automatically display each history result in an expander + for index, row in df.iterrows(): + with st.expander(f"Emission result {index + 1}"): + st.write(row["emission_history"]) + else: + st.write("No data available to display.") diff --git a/app/src/pages/20_NGOhome.py b/app/src/pages/20_NGOhome.py index 20def89..7f6b26b 100644 --- a/app/src/pages/20_NGOhome.py +++ b/app/src/pages/20_NGOhome.py @@ -8,13 +8,12 @@ ("# Welcome, ", f"{st.session_state['first_name']}") -st.write("### Navigation") -col1, col2, col3 = st.columns(3) -with col1: - if st.button("## Update Profile Info"): - st.switch_page("pages/21_NGOInfo.py") -with col2: - if st.button("## User/Enterprise Matches"): - st.switch_page("pages/22_NGOMatch.py") - -("#### Role:", f"{st.session_state['role']}") +with st.container(border=True): + st.write("### Navigation") + col1, col2, col3 = st.columns(3) + with col1: + if st.button("## Update Profile Info"): + st.switch_page("pages/21_NGOInfo.py") + with col2: + if st.button("## User/Enterprise Matches"): + st.switch_page("pages/22_NGOMatch.py") \ No newline at end of file diff --git a/app/src/pages/21_NGOInfo.py b/app/src/pages/21_NGOInfo.py index 5980f91..088258a 100644 --- a/app/src/pages/21_NGOInfo.py +++ b/app/src/pages/21_NGOInfo.py @@ -13,136 +13,137 @@ st.write("# Account Info") st.write("#### Edit Organization Information Below") -ngo_name = st.text_input("Organization Name") - -def url_check(url, tld_list): - if not url.startswith(("http://", "https://")): - url = "http://" + url - if validators.url(url): - domain = url.split("/")[2] - for tld in tld_list: - if domain.endswith(tld): - return True - return False - -website_link = st.text_input("Website Link:") -if website_link: - if url_check( - website_link, - [".com", ".gov", ".net", ".org", ".tv", ".cz", ".jp", ".de", ".br", ".edu"], - ): - st.success("Valid URL!") - else: - st.error("Invalid URL or TLD") - -contact_email = st.text_input("Head Contact Email:") -if contact_email: - if validators.email(contact_email): - st.success("Valid Email Address!") - else: - st.error("Invalid Email Address. Please Enter A Valid Email.") - -current_bio = requests.get("http://api:4000/n/Bio", timeout=200).json()["bio"] - -bio = st.text_area( - "Bio", - help="Enter a quick bio here. Mention some environmental interests if any", - height=100, - value=current_bio, -) - -if st.button("Submit"): - if (ngo_name - and url_check( +with st.container(border=True): + ngo_name = st.text_input("Organization Name") + def url_check(url, tld_list): + if not url.startswith(("http://", "https://")): + url = "http://" + url + if validators.url(url): + domain = url.split("/")[2] + for tld in tld_list: + if domain.endswith(tld): + return True + return False + + website_link = st.text_input("Website Link:") + if website_link: + if url_check( website_link, [".com", ".gov", ".net", ".org", ".tv", ".cz", ".jp", ".de", ".br", ".edu"], - ) - and validators.email(contact_email) - ): - - data = {"name": ngo_name, "website": website_link, "email": contact_email, "bio": bio} - - try: - response = requests.put("http://api:4000/n/NGOUpdate", json=data, timeout=200) - - if response.status_code == 201 or response.status_code == 200: - st.success("Data Successfully Inserted!") - else: - try: - error_message = response.json().get( - "error", "No Error Message Provided" + ): + st.success("Valid URL!") + else: + st.error("Invalid URL or TLD") + + contact_email = st.text_input("Head Contact Email:") + if contact_email: + if validators.email(contact_email): + st.success("Valid Email Address!") + else: + st.error("Invalid Email Address. Please Enter A Valid Email.") + + current_bio = requests.get("http://api:4000/n/Bio", timeout=200).json()["bio"] + + bio = st.text_area( + "Bio", + help="Enter a quick bio here. Mention some environmental interests if any", + height=100, + value=current_bio, + ) + + if st.button("Submit"): + if (ngo_name + and url_check( + website_link, + [".com", ".gov", ".net", ".org", ".tv", ".cz", ".jp", ".de", ".br", ".edu"], + ) + and validators.email(contact_email) + ): + + data = {"name": ngo_name, "website": website_link, "email": contact_email, "bio": bio} + + try: + response = requests.put("http://api:4000/n/NGOUpdate", json=data, timeout=200) + + if response.status_code == 201 or response.status_code == 200: + st.success("Data Successfully Inserted!") + else: + try: + error_message = response.json().get( + "error", "No Error Message Provided" + ) + except json.JSONDecodeError: + error_message = response.text # Raw response if not JSON + + st.error( + f"Failed To Insert Data. Status Code: {response.status_code}, Error: {error_message}" ) - except json.JSONDecodeError: - error_message = response.text # Raw response if not JSON - - st.error( - f"Failed To Insert Data. Status Code: {response.status_code}, Error: {error_message}" - ) - except Exception as e: - st.error(f"An Error Occurred: {e}") - else: - st.error( - "Please Fill In All The Fields Before Submitting And Have Valid Emails/Urls" - ) - -st.write("### My Current Data") - -try: - response = requests.get("http://api:4000/n/tags") - response.raise_for_status() - data = response.json() -except requests.exceptions.RequestException as e: - st.error(f"Error fetching data: {e}") - - -emoji_map = { - "Heat": "🔥", - "Flights": "🛫", - "Energy": "💡", - "Transport": "🚗", -} + except Exception as e: + st.error(f"An Error Occurred: {e}") + else: + st.error( + "Please Fill In All The Fields Before Submitting And Have Valid Emails/Urls" + ) + +with st.container(border=True): + st.write("### My Tags") + + try: + response = requests.get("http://api:4000/n/tags") + response.raise_for_status() + data = response.json() + except requests.exceptions.RequestException as e: + st.error(f"Error fetching data: {e}") + + + emoji_map = { + "Heat": "🔥", + "Flights": "🛫", + "Energy": "💡", + "Transport": "🚗", + } -tags = [tag["description"] for tag in data] - -if data: tags = [tag["description"] for tag in data] - selected = pills("Current tags", tags, [emoji_map[tag] for tag in tags], index=None) -else: - tags = [] - st.info("No tags available to display.") - - -col1, col2 = st.columns(2) -with col1: - st.write("### Add New NGO Tags") - options = ["Transport", "Flights", "Energy", "Heat"] - options = [option for option in options if option not in tags] - - selected_tags = st.selectbox("Select Your Associated Tags", options) - if st.button("Add Tags"): - tag_data = {"tag": selected_tags} - try: - response = requests.post("http://api:4000/n/NGOAdd", json=tag_data, timeout=200) - if response.status_code == 200: - st.success("Tags Successfully Added!") - else: - st.error(f"Failed To Add Tags. Status Code: {response.status_code}") - except Exception as e: - st.error(f"An Error Occurred While Adding Tags: {e}") -with col2: - st.write("### Delete NGO Tags") - options = ["Transport", "Flights", "Energy", "Heat"] - options = [option for option in options if option in tags] - selected_tag = st.selectbox("Select Tags To Delete", options) - if st.button("Delete Tags"): - tag_data = {"tag": selected_tag} - try: - response = requests.delete("http://api:4000/n/TagDelete", json=tag_data, timeout=200) - if response.status_code == 200: - st.success("Tags Successfully Deleted!") - else: - st.error( - f"Failed To Delete Tags. Status Code: {response.status_code}" - ) - except Exception as e: - st.error(f"An Error Occurred While Deleting Tags: {e}") \ No newline at end of file + + if data: + tags = [tag["description"] for tag in data] + selected = pills("Current tags", tags, [emoji_map[tag] for tag in tags], index=None) + else: + tags = [] + st.info("No tags available to display.") + + + col1, col2 = st.columns(2) + with col1: + st.write("### Add New NGO Tags") + options = ["Transport", "Flights", "Energy", "Heat"] + options = [option for option in options if option not in tags] + + selected_tags = st.selectbox("Select Your Associated Tags", options) + if st.button("Add Tags"): + tag_data = {"tag": selected_tags} + try: + response = requests.post("http://api:4000/n/NGOAdd", json=tag_data, timeout=200) + if response.status_code == 200: + st.success("Tags Successfully Added!") + else: + st.error(f"Failed To Add Tags. Status Code: {response.status_code}") + except Exception as e: + st.error(f"An Error Occurred While Adding Tags: {e}") + with col2: + st.write("### Delete NGO Tags") + options = ["Transport", "Flights", "Energy", "Heat"] + options = [option for option in options if option in tags] + selected_tag = st.selectbox("Select Tags To Delete", options) + if st.button("Delete Tags"): + tag_data = {"tag": selected_tag} + try: + response = requests.delete("http://api:4000/n/TagDelete", json=tag_data, timeout=200) + if response.status_code == 200: + st.success("Tags Successfully Deleted!") + else: + st.error( + f"Failed To Delete Tags. Status Code: {response.status_code}" + ) + except Exception as e: + st.error(f"An Error Occurred While Deleting Tags: {e}") \ No newline at end of file