from django.shortcuts import render
import pandas as pd
import matplotlib.pyplot as plt
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.urls import reverse
import numpy as np
import json

# Create your views here.
def index(request):
    global df
    if request.method == "POST":
        upload_file=request.FILES['document']
        if not upload_file.name.endswith('.csv'):
            messages.error(request, 'Please upload a .csv file.')
        df = pd.read_csv(upload_file)
        print(upload_file.name)
        print(upload_file.size)
        print(df)
        print(df.shape)
        json_records = df.reset_index().to_json(orient ='records') 
        data = [] 
        data = json.loads(json_records)
        context={
            'd':data
        }
    return render(request,'hello/index.html')
def preprocess(request):
        #df['Maths'].fillna(125, inplace=nan)
        print(df['Maths'].isnull().values.any())
        print(df['Physics'].isnull().values.any())
        print(df['Science'].isnull().values.any())
        df['Maths'].fillna(0, inplace=True)
        df['Physics'].fillna(0, inplace=True)
        df['Science'].fillna(0, inplace=True)
        json_records = df.reset_index().to_json(orient ='records') 
        data = [] 
        data = json.loads(json_records)
        context={
            'd':data
        }
        return render(request,'hello/preprocess.html',context)
def profile(request):
    fig = plt.figure()
    ax = fig.add_axes([0,0,1,1])
    ax.axis('equal')
    langs = ['Maths','Physics','Science']
    a=df['Maths']
    g=df['Physics']
    l=df['Science']
    c = np.array(a)
    h = np.array(g)
    m = np.array(l)
    print(c)
    i=6000
    e=6000
    n=6000
    o=sum(m)
    d=sum(c)
    j=sum(h)
    f=(e/d)*100 
    k=(i/j)*100 
    p=(n/o)*100
    print(j)
    print(f)
    print(k)
    y = np.array([f,k,p])
    ax.pie(y, labels = langs,autopct='%1.2f%%')
    fig.savefig('hello/static/foo.png')
    return render(request,'hello/view.html')
def profile1(request):
    fig1 = plt.figure()
    fig2 = plt.figure()
    fig3 = plt.figure()
    a=df['Maths']
    g=df['Physics']
    d=df['Science']
    e=df['id']
    plt.xlabel("Marks for each students") 
    plt.ylabel("No. of students") 
    plt.title("Students Mark Data analysis") 
    plt.bar(e[0:60],g[0:60]) 
    plt.bar(e[0:60],a[0:60]) 
    plt.bar(e[0:60],e[0:60]) 
    fig1.savefig('hello/static/bar.png')
    fig2.savefig('hello/static/bar1.png')
    fig3.savefig('hello/static/bar2.png')
    return render(request,'hello/view1.html')
def profile2(request):
    fig4 = plt.figure()
    fig5 = plt.figure()
    fig6 = plt.figure()
    a=df['Maths']
    g=df['Physics']
    d=df['Science']
    e=df['id']
    c = np.array(a)
    h = np.array(g)
    m = np.array(d)
    n = np.array(e)
    plt.xlabel("Marks for each students") 
    plt.ylabel("No. of students") 
    plt.title("Students Mark Data analysis") 
    plt.plot(c)
    plt.plot(h)
    plt.plot(m)
    fig4.savefig('hello/static/line.png')
    fig5.savefig('hello/static/line1.png')
    fig6.savefig('hello/static/line2.png')
    return render(request,'hello/view2.html')