socrTemplate='''
''' import pandas as pd import json #TO DO - option for square axes, or axis limits? def socrdata(df,STUB,title,key,x,y,size,color,category,xscale='linear',yscale='linear'): PATH='./' txtlist=[df.columns.tolist()] for row in df.iterrows(): txtlist.append(row[1].tolist()) with open(PATH+"data/"+STUB+"_data.js", "w") as f: f.write('var data='+json.dumps(txtlist, indent=4,sort_keys=True)) cols=df.columns with open(PATH+STUB+".html", "w") as f: f.write(socrTemplate.format( stub=STUB,title=title, key=df.columns.get_loc(key), x=df.columns.get_loc(x), y=df.columns.get_loc(y), size=df.columns.get_loc(size), color=df.columns.get_loc(color), category=df.columns.get_loc(category), xscale=xscale, yscale=yscale )) !head -n 5 ../../bahrainPop.csv dfb=pd.read_csv('../../bahrainPop.csv',skip_footer=2) tmp=dfb[['Year','Age','Sex','Value']].set_index(['Year','Age','Sex']).unstack("Sex").reset_index() tmp.columns = [' '.join(col).strip() for col in tmp.columns.values] tmp[:4] socrdata(tmp[(~tmp['Age'].isin([' 5-9', ' 10-14', ' 15-19', ' 20-24',' 35-39', ' 40-44', ' 45-49', ' 50-54', ' 55-59', ' 60-64', ' 65-69', ' 70-74', ' 75-79', ' 80+', ' 80-84', ' 85-89', ' 90-94', ' 95-99', ' 100+'])) & (tmp['Year']<2020) ], 'bahrainPopx','Bahrain','Year','Value Female','Value Male','Value Total','Age','Age') !ls