import pandas as pd import gradio as gr import config as cf import gets as gs import animate as an 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] gs.getSpecies(cf.Globals.df_country) gs.getYearRange(cf.Globals.df_country) 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) # For the Maps accordion # 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])] gs.getAllSpecies(cf.Globals.df_all) cf.Globals.mSpecies = cf.Globals.mapSpeciesDefault[cf.Globals.datasetIndex] gs.getMapYearRange(cf.Globals.dfMap) #not sure if this should be dfMap or df_all 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"), 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() ) ]