Download gets.py from dastacey/population_dashboard_version1: direct link, hf CLI and curl.
- Browser
- Download file 5.41 kB
-
https://huggingface.co/spaces/dastacey/population_dashboard_version1/resolve/main/gets.py
- Command line
-
hf download hf://spaces/dastacey/population_dashboard_version1/gets.py
-
curl -L -o gets.py https://huggingface.co/spaces/dastacey/population_dashboard_version1/resolve/main/gets.py
5.41 kB
| import pandas as pd | |
| import gradio as gr | |
| import config as cf | |
| import animate as an | |
| # Get all country names and ISO3 codes | |
| def getCountries (): | |
| df_temp = cf.Globals.df_all[["iso3","country"]].drop_duplicates() | |
| df_countryNames = df_temp.sort_values(by='country') | |
| cf.Globals.countriesList = df_countryNames["country"].tolist() | |
| cf.Globals.iso3List = df_countryNames["iso3"].tolist() | |
| # Extract the species in all countries | |
| def getAllSpecies(df): | |
| cf.Globals.allSpecies = sorted(set(df['species'])) | |
| # Extract the species in a country | |
| def getSpecies(dfcountry): | |
| cf.Globals.speciesList = sorted(set(dfcountry['species'])) | |
| cf.Globals.species = cf.Globals.speciesList | |
| # Get Year Range for selected dataset | |
| def getYearRange(df): | |
| yearList = sorted(set(df['year'])) | |
| cf.Globals.startYear = yearList[0] | |
| cf.Globals.endYear = yearList[-1] | |
| # Get Year Range for Map | |
| def getMapYearRange(df): | |
| yearList = sorted(set(df['year'])) | |
| cf.Globals.mStartYear = yearList[0] | |
| cf.Globals.mEndYear = yearList[-1] | |
| def loadDefaultDataSet ( ): | |
| cf.Globals.datasetIndex = 0 | |
| fname = "Population/"+cf.Globals.datasetList[cf.Globals.datasetIndex]+".csv" | |
| #fname = cf.Globals.datasetList[cf.Globals.datasetIndex]+".csv" | |
| df = pd.read_csv(fname) | |
| cf.Globals.df_all = df[["iso3","country","species","year","population"]] | |
| # Set up a translation table for ISO3 - country | |
| df_temp = df[["iso3","country"]].drop_duplicates() | |
| df_countryNames = df_temp.sort_values(by='country') | |
| cf.Globals.countriesList = df_countryNames["country"].tolist() | |
| cf.Globals.iso3List = df_countryNames["iso3"].tolist() | |
| # For the Maps tab | |
| # Extract cattle for all countries and all years | |
| cf.Globals.dfMap = cf.Globals.df_all[(cf.Globals.df_all['species'] == cf.Globals.mapSpeciesDefault[cf.Globals.datasetIndex])] | |
| # Set the defaults | |
| cf.Globals.country = cf.Globals.countryDefault[0] | |
| cf.Globals.iso3 = cf.Globals.iso3Default[0] | |
| cf.Globals.df_country = cf.Globals.df_all[cf.Globals.df_all["country"]==cf.Globals.country] | |
| getSpecies(cf.Globals.df_country) | |
| getYearRange(cf.Globals.df_country) | |
| cf.Globals.dfplot = pd.DataFrame() | |
| for n in range (len(cf.Globals.species)): | |
| dfplot0 = cf.Globals.df_country[(cf.Globals.df_country['species'] == cf.Globals.species[n])] | |
| sframes = [cf.Globals.dfplot,dfplot0] | |
| cf.Globals.dfplot = pd.concat(sframes) | |
| def loadDataSet ( datasetDropdown ): | |
| # Load the selected dataset | |
| cf.Globals.datasetIndex = int(f"""{datasetDropdown}""") | |
| fname = "Population/"+cf.Globals.datasetList[cf.Globals.datasetIndex]+".csv" | |
| #fname = cf.Globals.datasetList[cf.Globals.datasetIndex]+".csv" | |
| df = pd.read_csv(fname) | |
| cf.Globals.df_all = df[["iso3","country","species","year","population"]] | |
| # Set up a translation table for ISO3 - country | |
| df_temp = df[["iso3","country"]].drop_duplicates() | |
| df_countryNames = df_temp.sort_values(by='country') | |
| cf.Globals.countriesList = df_countryNames["country"].tolist() | |
| cf.Globals.iso3List = df_countryNames["iso3"].tolist() | |
| # Set the defaults | |
| cf.Globals.country = cf.Globals.countryDefault[cf.Globals.datasetIndex] | |
| cf.Globals.iso3 = cf.Globals.iso3Default[cf.Globals.datasetIndex] | |
| cf.Globals.df_country = cf.Globals.df_all[cf.Globals.df_all["country"]==cf.Globals.country] | |
| getSpecies(cf.Globals.df_country) | |
| getYearRange(cf.Globals.df_country) | |
| getAllSpecies(cf.Globals.df_all) | |
| getMapYearRange(cf.Globals.df_all) | |
| yearList = sorted(set(cf.Globals.df_country['year'])) | |
| cf.Globals.startYear = yearList[0] | |
| cf.Globals.endYear = yearList[-1] | |
| cf.Globals.dfplot = pd.DataFrame() | |
| for n in range (len(cf.Globals.species)): | |
| dfplot0 = cf.Globals.df_country[(cf.Globals.df_country['species'] == cf.Globals.species[n])] | |
| sframes = [cf.Globals.dfplot,dfplot0] | |
| cf.Globals.dfplot = pd.concat(sframes) | |
| return [ gr.Dropdown( choices=cf.Globals.countriesList, type="index", value=cf.Globals.country, multiselect=False, label="Countries", info="Select Country" ), gr.Dropdown( choices=cf.Globals.speciesList, type="value", value=cf.Globals.species, multiselect=True, label="Species", info="Select Species" ), gr.Slider(minimum=cf.Globals.startYear, maximum=cf.Globals.endYear, step=1, value=cf.Globals.startYear, label="Start Year"), gr.Slider(minimum=cf.Globals.startYear, maximum=cf.Globals.endYear, step=1, value=cf.Globals.endYear, label="End Year") ] | |
| def loadDataSetMap ( datasetDropdown ): | |
| # For the Maps tab | |
| # Extract default species for all countries and all years | |
| cf.Globals.datasetIndex = int(f"""{datasetDropdown}""") | |
| cf.Globals.dfMap = cf.Globals.df_all[(cf.Globals.df_all['species'] == cf.Globals.mapSpeciesDefault[cf.Globals.datasetIndex])] | |
| getAllSpecies(cf.Globals.df_all) | |
| cf.Globals.mSpecies = cf.Globals.mapSpeciesDefault[cf.Globals.datasetIndex] | |
| getMapYearRange(cf.Globals.dfMap) | |
| return [ gr.Dropdown(choices=cf.Globals.allSpecies, type="value", value=cf.Globals.mSpecies, multiselect=False, label="Species", info="Select Species"), gr.Slider(minimum=cf.Globals.mStartYear, maximum=cf.Globals.mEndYear, step=1, value=cf.Globals.mStartYear, label="Map Start Year"), gr.Slider(minimum=cf.Globals.mStartYear, maximum=cf.Globals.mEndYear, step=1, value=cf.Globals.mEndYear, label="Map End Year"), gr.Plot( an.animate() ) ] | |