from flask import Flask, flash, request, redirect, url_for, render_template
import urllib.request
from werkzeug.utils import secure_filename
from fileinput import filename
import pandas as pd
import numpy as np
import os
import glob
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt


def read_all_csv(path='./rawdata_csv/', extension='csv'):
    result = glob.glob(path+'SRC_R*.{}'.format(extension))
    df=None
    
    for file in result:
        if(df is None):
            df=pd.read_csv(file)
        else:
            df=pd.concat([df,pd.read_csv(file)], ignore_index=True)
    
    df_master=pd.read_csv("SRC_MasterData.csv")
    df_master=df_master[['State_Code','State_Name','Total_Population']]
    final_df = pd.merge(df, df_master, on='State_Code', how='inner')
    final_df['total']=final_df[['Primary_Only','Primary_with_Upper_Primary','Primary with_Upper_Primary_Sec_H.Sec','Upper_Primary_Only','Upper_Primary_with_Sec_H.Sec','Primary_with_Upper_Primary_Sec','Upper_Primary_with_Sec']].sum(axis=1)
    final_df['total_by_population']=final_df['total']/final_df['Total_Population']
    final_df=final_df[["Year","State_Name","State_Code","Main_Dimension","Sub_Dimension","Total_Population",'total','total_by_population']]

    return final_df

final_df=read_all_csv()
latest_year = max(final_df['Year'])

dict_summary={"top3":[],"bottom3":[]}
form_value_to_sub_dimension_mapping = {
    'govt':'Government',
    'private':'Private',
    'others':'Madrasas & Unrecognised',
    'below_secondary':'Below Secondary',
    'secondary':'Secondary',
    'higher_secondary':'Higher Secondary',
    'graduate':'Graduate',
    'post_graduate':'Post Graduate',
    'mphil_phd':'M.Phil / Ph.D',
    'post_doctorate':'Post Doctorate',
    'no_response':'No Response',
    'good':'Good Condition',
    'major':'Need Major Repair',
    'minor':'Need Minor Repair',
    'enrolment_lt_50': 'Enrolment <= 50' ,
    'drinking_water': 'Schools with Drinking Water' ,
    'girls_toilet': 'Schools with Girls\' Toilet' ,
    'ramp': 'Schools with Ramp' ,
    'single_classroom': 'Single Classroom Schools' ,
    'single_teacher': 'Single Teacher Schools' 
}

form_value_to_main_dimension_mapping = {
    'elementary_enrolment_category': 'Elementary Enrolment by School Category',
    'rural_elementary_enrolment': 'Rural Elementary Enrolment',
    'schools_category':'School by Category',
    'schools_rural':'Rural Schools',
    'schools_gender':'School by Gender',
    'teachers_by_category': 'Teachers by School Category',
    'teachers_by_qualification':'Teachers by Educational Qualification'
}

default_state_code = 27 # for Maharashtra

# fig_size_w=16
# fig_size_h=13

fig_size_w, fig_size_h = 8, 6
fig_size_tr_w, fig_size_tr_h = 8,6

def statewise_distribution(output_filename, input_df, year, main_dimension, sub_dimension=''):
    if sub_dimension:
        df_filtered = input_df.loc[(input_df['Main_Dimension'] == main_dimension) & ((input_df['Sub_Dimension'] == sub_dimension))]
    else:
        df_filtered = input_df.loc[(input_df['Main_Dimension'] == main_dimension)]
    df_filtered = df_filtered.loc[df_filtered['Year'] == year]
    df_filtered = df_filtered[['Year','State_Name','total','total_by_population']]
    df_sum = df_filtered.groupby(['State_Name','Year'],as_index = False).sum().sort_values("total_by_population",ascending=False)
    
    fig = plt.figure(figsize=(fig_size_w,fig_size_h))
    plt.xticks(rotation=90, ha="right")
    plt.bar(df_sum['State_Name'],df_sum['total_by_population'], align='center')
    plt.ylabel('Enrollements/Population')
    list_states=list(df_sum['State_Name'])
    dict_summary['top3']=[list_states[0],list_states[1],list_states[2]]
    dict_summary['bottom3']=[list_states[-1],list_states[-2],list_states[-3]]
    plt.savefig(output_filename)
    plt.close(fig)
    
    return dict_summary

def total_enrolment_by_category(output_filename, input_df, main_dimension, state_code, ylabel):
    fig = plt.figure(figsize=(fig_size_tr_w,fig_size_tr_h))
    df_fil=input_df.loc[input_df['Main_Dimension'] == main_dimension]
    df_fil=df_fil.loc[(df_fil['State_Code'] == state_code)]
    min_year, max_year = min(df_fil['Year']), max(df_fil['Year'])
    df_sum = df_fil.groupby(['Sub_Dimension','State_Code','Year'],as_index = False).sum()
    df_sum=df_sum[['Sub_Dimension','State_Code','Year','total']]
    
    df_govt=df_sum.loc[(df_sum['Sub_Dimension'] == "Government")]
    df_pvt=df_sum.loc[(df_sum['Sub_Dimension'] == "Private")]
    plt.plot(df_govt['Year'],df_govt['total'],color='r', label='Goverment')
    plt.plot(df_pvt['Year'],df_pvt['total'],color='g', label='Private')
    if not 'Rural' in main_dimension:
        df_madr=df_sum.loc[(df_sum['Sub_Dimension'] == "Madrasas & Unrecognised")]
        plt.plot(df_madr['Year'],df_madr['total'],color='b', label='Madrasas')
    
    plt.legend()
    plt.xticks(np.arange(min_year, max_year+1, 1.0))
    plt.ylabel(ylabel)
    plt.xlabel('Year')
    plt.savefig(output_filename)
    plt.close(fig)

def total_teachers_by_main_dimension(output_filename, input_df, main_dimension, state_code):
    fig = plt.figure(figsize=(fig_size_tr_w,fig_size_tr_h))
    df_fil=input_df.loc[input_df['Main_Dimension'] == main_dimension]
    df_fil=df_fil.loc[(df_fil['State_Code'] == state_code)]
    min_year, max_year = min(df_fil['Year']), max(df_fil['Year'])
    df_sum = df_fil.groupby(['Sub_Dimension','State_Code','Year'],as_index = False).sum()
    df_sum=df_sum[['Sub_Dimension','State_Code','Year','total']]
    
    if 'Category' in main_dimension:
        df_govt=df_sum.loc[(df_sum['Sub_Dimension'] == "Government")]
        df_pvt=df_sum.loc[(df_sum['Sub_Dimension'] == "Private")]
        df_madr=df_sum.loc[(df_sum['Sub_Dimension'] == "Madrasas & Unrecognised")]
        
        plt.plot(df_govt['Year'],df_govt['total'],color='r', label='Goverment')
        plt.plot(df_pvt['Year'],df_pvt['total'],color='g', label='Private')
        plt.plot(df_madr['Year'],df_madr['total'],color='b', label='Madrasas')
    else:
        df_bel_sec=df_sum.loc[(df_sum['Sub_Dimension'] == "Below Secondary")]
        df_sec=df_sum.loc[(df_sum['Sub_Dimension'] == "Secondary")]
        df_hs=df_sum.loc[(df_sum['Sub_Dimension'] == "Higher Secondary")]
        df_grad=df_sum.loc[(df_sum['Sub_Dimension'] == "Graduate")]
        df_pg=df_sum.loc[(df_sum['Sub_Dimension'] == "Post Graduate")]
        df_phd=df_sum.loc[(df_sum['Sub_Dimension'] == "M.Phil / Ph.D")]

        plt.plot(df_bel_sec['Year'],df_bel_sec['total'],color='r', label='Below Secondary')
        plt.plot(df_sec['Year'],df_sec['total'],color='g', label='Secondary')
        plt.plot(df_hs['Year'],df_hs['total'],color='b', label='Higher Secondary')
        plt.plot(df_grad['Year'],df_grad['total'],color='y', label='Graduate')
        plt.plot(df_pg['Year'],df_pg['total'],color='c', label='Post Graduate')
        plt.plot(df_phd['Year'],df_phd['total'],color='m', label='M.Phil / Ph.D')

    plt.legend()
    plt.xticks(np.arange(min_year, max_year+1, 1.0))
    plt.ylabel('Number of Teachers')
    plt.xlabel('Year')
    plt.savefig(output_filename)
    plt.close(fig)

app = Flask(__name__)
print(' ------------ Server Started ------------ ')
UPLOAD_FOLDER = 'static/'
 
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config["CACHE_TYPE"] = "null"
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0

# app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
 
# ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
 
# def allowed_file(filename):
#     return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

def prepare_graph(filename):
    data=list(np.random.uniform(1, 10, size=(10,1)).reshape(-1))
    ind = np.arange(len(data))
#     plt.ioff()
    fig = plt.figure(figsize=(8,6))
    plt.plot(data, label='Number of Schools', ls='--', color='black', marker='o', markersize=9, mew=2, linewidth=2)
    plt.xlabel('Year')
    plt.ylabel('Number of Schools')
    plt.xticks(ind, [2000+i for i in ind])
    plt.legend(loc=0)
    plt.savefig('./static/'+filename)
    plt.close(fig)

@app.after_request
def add_header(request):
    """
    Add headers to both force latest IE rendering engine or Chrome Frame,
    and also to cache the rendered page for 10 minutes.
    """
    request.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    request.headers["Pragma"] = "no-cache"
    request.headers["Expires"] = "0"
    request.headers['Cache-Control'] = 'public, max-age=0'
    return request
    
@app.route('/')
def home():
    return render_template('index.html')

@app.route('/index.html')
def index():
    return render_template('index.html')

trend_elementary_enrolment = ''
distribution_rural_enrolment = ''
distribution_elementary_enrolment = ''
rural_enrolment_summary = {}
elementary_enrolment_summary = {}
rural_enrolment_category = ''
elementary_enrolment_category = ''
@app.route('/students.html')
def students():
    global trend_elementary_enrolment, distribution_elementary_enrolment, distribution_rural_enrolment, rural_enrolment_summary, elementary_enrolment_summary, rural_enrolment_category, elementary_enrolment_category
    elementary_enrolment_category, rural_enrolment_category = 'Government', 'Government' 
    distribution_rural_enrolment = "img/distribution_rural_enrolment_govt_{}.jpeg".format(latest_year)
    rural_enrolment_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_rural_enrolment), final_df, latest_year, main_dimension = "Rural Elementary Enrolment", sub_dimension=elementary_enrolment_category)
    distribution_elementary_enrolment = "img/distribution_elementary_enrolment_govt_{}.jpeg".format(latest_year)
    elementary_enrolment_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_elementary_enrolment), final_df, latest_year, main_dimension = "Elementary Enrolment by School Category", sub_dimension=rural_enrolment_category)
    trend_elementary_enrolment = "img/trend_elementary_enrolment_category_{}.jpeg".format(default_state_code)
    total_enrolment_by_category(os.path.join(app.config['UPLOAD_FOLDER'], trend_elementary_enrolment), final_df, main_dimension='Elementary Enrolment by School Category', state_code=default_state_code, ylabel='Enrolments')
    return render_template('students.html', trend_elementary_enrolment=trend_elementary_enrolment, distribution_rural_enrolment=distribution_rural_enrolment, distribution_elementary_enrolment=distribution_elementary_enrolment, rural_enrolment_summary=rural_enrolment_summary, elementary_enrolment_summary=elementary_enrolment_summary, rural_enrolment_category=rural_enrolment_category, elementary_enrolment_category=elementary_enrolment_category)

trend_teachers = ''
distribution_teachers_by_category = ''
distribution_teachers_by_educational_qualification = ''
teachers_by_category_summary = {}
teachers_by_educational_qualification_summary = {}
teachers_category = ''
teachers_qualification = ''
@app.route('/teachers.html')
def teachers():
    global trend_teachers, distribution_teachers_by_educational_qualification, distribution_teachers_by_category, teachers_by_category_summary, teachers_by_educational_qualification_summary, teachers_category, teachers_qualification
    teachers_category, teachers_qualification = 'Government', 'Below Secondary' 
    distribution_teachers_by_category = "img/distribution_teachers_by_category_govt_{}.jpeg".format(latest_year)
    teachers_by_category_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_teachers_by_category), final_df, latest_year, main_dimension = "Teachers by School Category", sub_dimension=teachers_category)
    distribution_teachers_by_educational_qualification = "img/distribution_teachers_by_educational_qualification_below_secondary_{}.jpeg".format(latest_year)
    teachers_by_educational_qualification_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_teachers_by_educational_qualification), final_df, latest_year, main_dimension = "Teachers by Educational Qualification", sub_dimension=teachers_qualification)
    trend_teachers="img/trend_teachers_by_category_{}.jpeg".format(default_state_code)
    total_teachers_by_main_dimension(os.path.join(app.config['UPLOAD_FOLDER'], trend_teachers), final_df, "Teachers by School Category", default_state_code)
    return render_template('teachers.html', trend_teachers=trend_teachers, distribution_teachers_by_category=distribution_teachers_by_category, distribution_teachers_by_educational_qualification=distribution_teachers_by_educational_qualification, teachers_by_category_summary=teachers_by_category_summary, teachers_by_educational_qualification_summary=teachers_by_educational_qualification_summary, teachers_category=teachers_category, teachers_qualification=teachers_qualification)

trend_schools = ''
distribution_schools_by_category = ''
distribution_rural_schools_by_category = ''
schools_by_category_summary = {}
rural_schools_by_category_summary = {}
school_category = ''
rural_school_category = ''
@app.route('/schools.html')
def schools():
    global trend_schools, distribution_rural_schools_by_category, distribution_schools_by_category, schools_by_category_summary, rural_schools_by_category_summary, school_category, rural_school_category
    school_category, rural_school_category = 'Government', 'Government' 
    distribution_schools_by_category = "img/distribution_schools_by_category_govt_{}.jpeg".format(latest_year)
    schools_by_category_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_schools_by_category), final_df, latest_year, main_dimension = "School by Category", sub_dimension=school_category)
    distribution_rural_schools_by_category = "img/distribution_rural_schools_by_category_below_secondary_{}.jpeg".format(latest_year)
    rural_schools_by_category_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_rural_schools_by_category), final_df, latest_year, main_dimension = "Rural Schools", sub_dimension=rural_school_category)
    trend_schools = "img/trend_schools_category_{}.jpeg".format(default_state_code)
    total_enrolment_by_category(os.path.join(app.config['UPLOAD_FOLDER'], trend_schools), final_df, main_dimension='School by Category', state_code=default_state_code, ylabel='Number of schools')
    return render_template('schools.html', trend_schools=trend_schools, distribution_schools_by_category=distribution_schools_by_category, distribution_rural_schools_by_category=distribution_rural_schools_by_category, schools_by_category_summary=schools_by_category_summary, rural_schools_by_category_summary=rural_schools_by_category_summary, school_category=school_category, rural_school_category=rural_school_category)

distribution_classroom_by_condition = ''
classroom_condition_summary = {}
classroom_condition = ''
@app.route('/classrooms.html')
def classrooms():
    global distribution_classroom_by_condition, classroom_condition_summary, classroom_condition
    classroom_condition = 'Good Condition'
    distribution_classroom_by_condition = "img/distribution_classroom_by_condition_good_{}.jpeg".format(latest_year)
    classroom_condition_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_classroom_by_condition), final_df, latest_year, main_dimension = "Total Classrooms", sub_dimension=classroom_condition)
    return render_template('classrooms.html', distribution_classroom_by_condition=distribution_classroom_by_condition, classroom_condition_summary=classroom_condition_summary,  classroom_condition=classroom_condition)

distribution_school_facility = ''
school_facility_summary = {}
school_facility = ''
@app.route('/facilities.html')
def facilities():
    global distribution_school_facility, school_facility_summary, school_facility
    school_facility = 'Enrolment <= 50'
    distribution_school_facility = "img/distribution_school_facility_enrolment_lt_50_{}.jpeg".format(latest_year)
    school_facility_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_school_facility), final_df, latest_year, main_dimension = "School Facilities", sub_dimension=school_facility)
    return render_template('facilities.html', distribution_school_facility=distribution_school_facility, school_facility_summary=school_facility_summary,  school_facility=school_facility)

# display selected image
@app.route('/get_card_from_index', methods=['POST'])
def get_card_from_index():    
    card_from_index = 'img/sample_trend_schools.jpeg'
    prepare_graph(card_from_index)
    return render_template('index.html', card_from_index=card_from_index)

@app.route('/get_facilities_from_index', methods=['POST'])
def get_facilities_from_index():    
    facilities_from_index = 'img/sample_trend_facilities.jpeg'
    prepare_graph(facilities_from_index)
    return render_template('index.html', facilities_from_index=facilities_from_index)

@app.route('/get_trend_for_elementary_enrolment', methods=['POST'])
def get_trend_for_elementary_enrolment():
    global form_value_to_main_dimension_mapping, trend_elementary_enrolment, distribution_elementary_enrolment, distribution_rural_enrolment, rural_enrolment_summary, elementary_enrolment_summary, rural_enrolment_category, elementary_enrolment_category
    main_dimension = form_value_to_main_dimension_mapping[request.form['type']]
    state_code = int(request.form['state_code'])
    trend_elementary_enrolment = "img/trend_{}_{}.jpeg".format(request.form['type'],state_code)
    total_enrolment_by_category(os.path.join(app.config['UPLOAD_FOLDER'], trend_elementary_enrolment), final_df, main_dimension, state_code, ylabel='Enrolments')
    return render_template('students.html', trend_elementary_enrolment=trend_elementary_enrolment, distribution_rural_enrolment=distribution_rural_enrolment, distribution_elementary_enrolment=distribution_elementary_enrolment, rural_enrolment_summary=rural_enrolment_summary, elementary_enrolment_summary=elementary_enrolment_summary, rural_enrolment_category=rural_enrolment_category, elementary_enrolment_category=elementary_enrolment_category)

@app.route('/get_distribution_for_elementary_enrolment', methods=['POST'])
def get_distribution_for_elementary_enrolment():
    global form_value_to_sub_dimension_mapping, distribution_elementary_enrolment, distribution_rural_enrolment, elementary_enrolment_summary, rural_enrolment_summary, elementary_enrolment_category, rural_enrolment_category
    elementary_enrolment_category = form_value_to_sub_dimension_mapping[request.form['category']]
    requested_year = int(request.form['year'])
    distribution_elementary_enrolment = "img/distribution_elementary_enrolment_{}_{}.jpeg".format(request.form['category'], requested_year)
    elementary_enrolment_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_elementary_enrolment), final_df, requested_year, main_dimension = "Elementary Enrolment by School Category", sub_dimension=elementary_enrolment_category)
    return render_template('students.html', distribution_rural_enrolment=distribution_rural_enrolment, distribution_elementary_enrolment=distribution_elementary_enrolment, rural_enrolment_summary=rural_enrolment_summary, elementary_enrolment_summary=elementary_enrolment_summary, rural_enrolment_category=rural_enrolment_category, elementary_enrolment_category=elementary_enrolment_category)

@app.route('/get_distribution_for_rural_enrolment', methods=['POST'])
def get_distribution_for_rural_enrolment():
    global form_value_to_sub_dimension_mapping, distribution_elementary_enrolment, distribution_rural_enrolment, elementary_enrolment_summary, rural_enrolment_summary, elementary_enrolment_category, rural_enrolment_category
    rural_enrolment_category = form_value_to_sub_dimension_mapping[request.form['category']]
    requested_year = int(request.form['year'])
    distribution_rural_enrolment = "img/distribution_elementary_enrolment_{}_{}.jpeg".format(request.form['category'], requested_year)
    rural_enrolment_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_rural_enrolment), final_df, requested_year, main_dimension = "Elementary Enrolment by School Category", sub_dimension=rural_enrolment_category)
    return render_template('students.html', distribution_rural_enrolment=distribution_rural_enrolment, distribution_elementary_enrolment=distribution_elementary_enrolment, rural_enrolment_summary=rural_enrolment_summary, elementary_enrolment_summary=elementary_enrolment_summary, rural_enrolment_category=rural_enrolment_category, elementary_enrolment_category=elementary_enrolment_category)

@app.route('/get_trend_for_teachers', methods=['POST'])
def get_trend_for_teachers():
    global form_value_to_main_dimension_mapping, trend_teachers, distribution_teachers_by_educational_qualification, distribution_teachers_by_category, teachers_by_category_summary, teachers_by_educational_qualification_summary, teachers_category, teachers_qualification
    main_dimension = form_value_to_main_dimension_mapping[request.form['type']]
    state_code = int(request.form['state_code'])
    trend_teachers="img/trend_{}_{}.jpeg".format(request.form['type'], state_code)
    total_teachers_by_main_dimension(os.path.join(app.config['UPLOAD_FOLDER'], trend_teachers), final_df, main_dimension, state_code)
    return render_template('teachers.html', trend_teachers=trend_teachers, distribution_teachers_by_category=distribution_teachers_by_category, distribution_teachers_by_educational_qualification=distribution_teachers_by_educational_qualification, teachers_by_category_summary=teachers_by_category_summary, teachers_by_educational_qualification_summary=teachers_by_educational_qualification_summary, teachers_category=teachers_category, teachers_qualification=teachers_qualification)

@app.route('/get_distribution_for_teachers_by_category', methods=['POST'])
def get_distribution_for_teachers_by_category():
    global distribution_teachers_by_educational_qualification, distribution_teachers_by_category, teachers_by_category_summary, teachers_by_educational_qualification_summary, teachers_category, teachers_qualification
    teachers_category = form_value_to_sub_dimension_mapping[request.form['category']]
    requested_year = int(request.form['year'])
    distribution_teachers_by_category = "img/distribution_teachers_by_category_{}_{}.jpeg".format(request.form['category'], requested_year)
    teachers_by_category_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_teachers_by_category), final_df, requested_year, main_dimension = "Teachers by School Category", sub_dimension=teachers_category)
    return render_template('teachers.html', distribution_teachers_by_category=distribution_teachers_by_category, distribution_teachers_by_educational_qualification=distribution_teachers_by_educational_qualification, teachers_by_category_summary=teachers_by_category_summary, teachers_by_educational_qualification_summary=teachers_by_educational_qualification_summary, teachers_category=teachers_category, teachers_qualification=teachers_qualification)

@app.route('/get_distribution_for_teachers_by_educational_qualification', methods=['POST'])
def get_distribution_for_teachers_by_educational_qualification():
    global distribution_teachers_by_educational_qualification, distribution_teachers_by_category, teachers_by_category_summary, teachers_by_educational_qualification_summary, teachers_category, teachers_qualification
    teachers_qualification = form_value_to_sub_dimension_mapping[request.form['qualification']]
    requested_year = int(request.form['year'])
    distribution_teachers_by_educational_qualification = "img/distribution_teachers_by_educational_qualification_{}_{}.jpeg".format(request.form['category'], requested_year)
    teachers_by_educational_qualification_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_teachers_by_educational_qualification), final_df, requested_year, main_dimension = "Teachers by Educational Qualification", sub_dimension=teachers_qualification)
    return render_template('teachers.html', distribution_teachers_by_category=distribution_teachers_by_category, distribution_teachers_by_educational_qualification=distribution_teachers_by_educational_qualification, teachers_by_category_summary=teachers_by_category_summary, teachers_by_educational_qualification_summary=teachers_by_educational_qualification_summary, teachers_category=teachers_category, teachers_qualification=teachers_qualification)

@app.route('/get_trend_for_schools', methods=['POST'])
def get_trend_for_schools():
    global form_value_to_main_dimension_mapping ,trend_schools, distribution_rural_schools_by_category, distribution_schools_by_category, schools_by_category_summary, rural_schools_by_category_summary, school_category, rural_school_category
    main_dimension = form_value_to_main_dimension_mapping[request.form['type']]
    state_code = int(request.form['state_code'])
    trend_schools = "img/trend_{}_{}.jpeg".format(request.form['type'], state_code)
    total_enrolment_by_category(os.path.join(app.config['UPLOAD_FOLDER'], trend_schools), final_df, main_dimension, state_code=default_state_code, ylabel='Number of schools')
    return render_template('schools.html', trend_schools=trend_schools, distribution_schools_by_category=distribution_schools_by_category, distribution_rural_schools_by_category=distribution_rural_schools_by_category, schools_by_category_summary=schools_by_category_summary, rural_schools_by_category_summary=rural_schools_by_category_summary, school_category=school_category, rural_school_category=rural_school_category)

@app.route('/get_distribution_of_schools_by_category', methods=['POST'])
def get_distribution_of_schools_by_category():
    global distribution_rural_schools_by_category, distribution_schools_by_category, schools_by_category_summary, rural_schools_by_category_summary, school_category, rural_school_category
    school_category = form_value_to_sub_dimension_mapping[request.form['category']]
    requested_year = int(request.form['year'])
    distribution_schools_by_category = "img/distribution_schools_by_category_{}_{}.jpeg".format(request.form['category'], requested_year)
    schools_by_category_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_schools_by_category), final_df, requested_year, main_dimension = "School by Category", sub_dimension=school_category)
    return render_template('schools.html', distribution_schools_by_category=distribution_schools_by_category, distribution_rural_schools_by_category=distribution_rural_schools_by_category, schools_by_category_summary=schools_by_category_summary, rural_schools_by_category_summary=rural_schools_by_category_summary, school_category=school_category, rural_school_category=rural_school_category)

@app.route('/get_distribution_of_rural_schools_by_category', methods=['POST'])
def get_distribution_of_rural_schools_by_category():
    global distribution_rural_schools_by_category, distribution_schools_by_category, schools_by_category_summary, rural_schools_by_category_summary, school_category, rural_school_category
    rural_school_category = form_value_to_sub_dimension_mapping[request.form['category']]
    requested_year = int(request.form['year'])
    distribution_rural_schools_by_category = "img/distribution_rural_schools_by_category_{}_{}.jpeg".format(request.form['category'], requested_year)
    rural_schools_by_category_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_rural_schools_by_category), final_df, requested_year, main_dimension = "Rural Schools", sub_dimension=rural_school_category)
    return render_template('schools.html', distribution_schools_by_category=distribution_schools_by_category, distribution_rural_schools_by_category=distribution_rural_schools_by_category, schools_by_category_summary=schools_by_category_summary, rural_schools_by_category_summary=rural_schools_by_category_summary, school_category=school_category, rural_school_category=rural_school_category)

@app.route('/get_trend_from_classrooms', methods=['POST'])
def get_trend_from_classrooms():
    trend_from_classrooms = 'img/sample_trend_classrooms_new.jpeg'
    prepare_graph(trend_from_classrooms)
    return render_template('classrooms.html', trend_from_classrooms=trend_from_classrooms)

@app.route('/get_distribution_of_classroom_conditions', methods=['POST'])
def get_distribution_of_classroom_conditions():
    global distribution_classroom_by_condition, classroom_condition_summary, classroom_condition
    classroom_condition = form_value_to_sub_dimension_mapping[request.form['condition']]
    requested_year = int(request.form['year'])
    distribution_classroom_by_condition = "img/distribution_classroom_by_condition_{}_{}.jpeg".format(request.form['condition'], requested_year)
    classroom_condition_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_classroom_by_condition), final_df, latest_year, main_dimension = "Total Classrooms", sub_dimension=classroom_condition)
    return render_template('classrooms.html', distribution_classroom_by_condition=distribution_classroom_by_condition, classroom_condition_summary=classroom_condition_summary,  classroom_condition=classroom_condition)

@app.route('/get_trend_from_facilities', methods=['POST'])
def get_trend_from_facilities():
    trend_from_facilities = 'img/sample_trend_facilities_new.jpeg'
    prepare_graph(trend_from_facilities)
    return render_template('facilities.html', trend_from_facilities=trend_from_facilities)

@app.route('/get_distribution_of_school_facilities', methods=['POST'])
def get_distribution_of_school_facilities():
    global distribution_school_facility, school_facility_summary, school_facility
    school_facility = form_value_to_sub_dimension_mapping[request.form['facility']]
    requested_year = int(request.form['year'])
    distribution_school_facility = "img/distribution_school_facility_{}_{}.jpeg".format(request.form['facility'], requested_year)
    school_facility_summary = statewise_distribution(os.path.join(app.config['UPLOAD_FOLDER'], distribution_school_facility), final_df, latest_year, main_dimension = "School Facilities", sub_dimension=school_facility)
    return render_template('facilities.html', distribution_school_facility=distribution_school_facility, school_facility_summary=school_facility_summary,  school_facility=school_facility)

# # @app.route('/display/<filename>')
# @app.route('/display/<filename>')
# def display_image(filename):
#     pass
# #     print('display_image filename: ' + filename)
#     return redirect(url_for('static', filename=filename), code=301)


if __name__ == "__main__":
    app.run()