Spaces:
Sleeping
Sleeping
import os | |
import sys | |
src_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..", "frontend")) | |
sys.path.append(src_directory) | |
import streamlit as st | |
import streamlit_functions | |
import pandas as pd | |
def load_data(file): | |
try: | |
data = pd.read_csv(file) | |
return data | |
except Exception as e: | |
st.error(f"Error loading file: {e}") | |
return None | |
st.title("World Population Data") | |
st.sidebar.title("Navigation") | |
choosen_data = streamlit_functions.choose_data_to_view(st) | |
if choosen_data == "Show All Continents": | |
option = choosen_data.replace(" ", "") | |
streamlit_functions.display_contents(option, st) | |
elif choosen_data == "Show All Countries": | |
option = choosen_data.replace(" ", "") | |
streamlit_functions.display_contents(option, st) | |
elif choosen_data == "Show Continent Stats": | |
streamlit_functions.display_stats(st, choosen_data) | |
elif choosen_data == "Show Country Stats": | |
streamlit_functions.display_stats(st, choosen_data) | |
elif choosen_data == "Show Continent Wise Data": | |
clean_choosen_data = choosen_data.replace(" ", "") | |
attribute = streamlit_functions.choose_attribute_to_view(st) | |
if attribute in ["Population","Area"]: | |
option = f"{clean_choosen_data}/{attribute}" | |
streamlit_functions.display_contents(option,st) | |
elif choosen_data == "Show Country Data": | |
clean_choosen_data = choosen_data.replace(" ", "") | |
country_list = streamlit_functions.get_list("ShowAllCountries") | |
choosen_country = streamlit_functions.choose_data_from_list(st,country_list) | |
clean_country = choosen_country.replace(" ","").capitalize() | |
attribute = streamlit_functions.choose_attribute_to_view(st) | |
if attribute in ["Population","Area"]: | |
option = f"{clean_choosen_data}/{clean_country}/{attribute}" | |
streamlit_functions.display_contents(option,st) | |
elif choosen_data == "Upload Custom CSV": | |
uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type=['csv']) | |
if uploaded_file is not None: | |
data = load_data(uploaded_file) | |
if data is not None: | |
st.write("Data Preview:", data.head()) | |
if "Continent" in data.columns and "Population" in data.columns: | |
continent_data = data.groupby("Continent")["Population"].sum().reset_index() | |
st.write("Continent-wise Population Summary:", continent_data) | |
else: | |
st.warning("The uploaded CSV does not contain 'Continent' or 'Population' columns.") | |
else: | |
st.warning("There was an error loading the file. Please check the format and try again.") | |
else: | |
st.write("Please choose an option from the sidebar to view the data.") |