Commit f17a6370 authored by Carlo Angelo Cordero's avatar Carlo Angelo Cordero
Browse files

Upload New File

parent 29c60aac
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as doc
from datetime import datetime
import plotly.graph_objs as go
import dash_table as dt
import dash_html_components as html
import dash_core_components as dcc
import io
import xlsxwriter
import flask
from flask import send_file
import pandas as pd
import numpy as np
from IPython.display import display, IFrame, HTML
import os
def show_app(app, port=9999, width=900, height=700):
host = 'localhost'
url = f'http://{host}:{port}'
display(HTML(f"<a href='{url}' target='_blank'>Open in new tab</a>"))
display(IFrame(url, width=width, height=height))
app.css.config.serve_locally = True
app.scripts.config.serve_locally = True
return app.run_server(debug=False, host=host, port=port)
# In[53]:
#1
df2 = pd.read_excel('data/par_summary.xlsx')
eda = (pd.DataFrame(df2.groupby(['PROGRAM', 'PRODUCT GROUP'])['Outstanding Balance', 'PAR']
.sum()/1000000)
.round(2)
.sort_values('PAR', ascending=False)
.reset_index())
eda = (eda.append(eda.sum(numeric_only=True), ignore_index=True)
.round(2)
.replace(np.nan, 'TOTAL', regex=True))
#4
df = pd.read_excel('data/par_final.xlsx', usecols="B:AM")
#2
df_count = pd.DataFrame(df.groupby('Classes')[
'Classes'].count().sort_values(ascending=False))
df_count.columns = ['Accounts']
df_PAR = ((df.groupby('Classes')['PAR', 'Classes']
.sum())
.sort_values('PAR', ascending=False))
df_merge = df_PAR.merge(df_count, left_on='Classes', right_on='Classes')
df_merge['PAR %'] = (((df_merge['PAR'] / df_merge['PAR']
.sum())*100))
df_merge['Accounts %'] = (((df_merge['Accounts'] / df_merge['Accounts']
.sum())*100))
df_merge = df_merge[['PAR', 'PAR %', 'Accounts', 'Accounts %']]
df_merge.columns = ['PAR Amount', 'PAR %', 'Accounts', 'Accounts %']
df_merge["PAR Amount"] = pd.to_numeric(df_merge["PAR Amount"]).astype(int)
df_merge = df_merge.reset_index()
df_merge = (df_merge.append(df_merge.sum(numeric_only=True), ignore_index=True)
.round(2)
.replace(np.nan, 'TOTAL', regex=True))
df_merge['PAR/Account'] = (df_merge['PAR Amount'] /
df_merge['Accounts']).astype(int)
df_merge['PAR Amount'] = (df_merge['PAR Amount'].round(-6)/1000000)
#3
df_pivot_amount = df.pivot_table(values='PAR',
index='BRANCH NAME',
columns='Classes',
aggfunc='sum',
fill_value=0)
df_pivot_amount.loc[:, 'Total'] = df_pivot_amount.sum(
numeric_only=True, axis=1)
df_pivot_amount['%'] = (
(df_pivot_amount['Total']/df_pivot_amount['Total'].sum())*100).round(2)
df_pivot_amount = df_pivot_amount.sort_values('Total', ascending=False)
cols = ['Almost lost customer, low PAR ', 'Lost loyal customer, low PAR ',
'Low chance of payment, high PAR ', 'Low chance of payment, low PAR',
'Loyal customers, high PAR ', 'More likely to pay, high PAR ',
'More likely to pay, low PAR', 'One month delay, high PAR ',
'One month delay, low PAR ', 'Total']
df_pivot_amount[cols] = df_pivot_amount[cols].div(1000000).round(2)
df_pivot_amount = df_pivot_amount.reset_index()
#eda, df_merge, df_pivot_amount, df
# In[62]:
app = dash.Dash(assets_folder='static/', )
app.callback_map = {}
app.layout = html.Div(children=[
#TITLE
html.Div([
html.H2(children='Summary of Loan Products under PAR as of August 2019 in Php Millions')]),
html.Div([
dt.DataTable(
id='product-datatable',
columns=[{"name": i, "id": i, "deletable": True, "selectable": True}
for i in eda.columns],
data=eda.to_dict('records'),
style_as_list_view=True,
style_header={
'backgroundColor': 'skyblue',
'fontWeight': 'bold'
},
style_cell={'width': 'auto'},
editable=True,
filter_action="native",
sort_action="native",
sort_mode="multi",
column_selectable="single",
row_selectable="multi",
row_deletable=True,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current=0,
export_format='xlsx',
export_headers='display',
virtualization=True,
),
html.Div(id='product-datatable-container')
]),
html.Br(),
html.Br(),
html.Div([
html.H2(children='Summary of Type of PAR Accounts from RFM Analysis')]),
html.Div([
dt.DataTable(
id='classes-datatable',
columns=[{"name": i, "id": i, "deletable": True, "selectable": True}
for i in df_merge.columns],
data=df_merge.to_dict('records'),
style_as_list_view=True,
style_header={
'backgroundColor': 'skyblue',
'fontWeight': 'bold'
},
style_cell={'width': 'auto'},
editable=True,
filter_action="native",
sort_action="native",
sort_mode="multi",
column_selectable="single",
row_selectable="multi",
row_deletable=True,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current=0,
export_format='xlsx',
export_headers='display',
virtualization=True,
),
html.Div(id='classes-datatable-container')
]),
html.Br(),
html.Br(),
html.Div([
html.H2(children='Summary of Classes at the Branch Level in Php Millions')]),
html.Div([
dt.DataTable(
id='branch-datatable',
columns=[{"name": i, "id": i, "deletable": True, "selectable": True}
for i in df_pivot_amount.columns],
data=df_pivot_amount.to_dict('records'),
style_as_list_view=True,
style_header={
'backgroundColor': 'skyblue',
'fontWeight': 'bold'
},
style_cell={'width': 'auto'},
editable=True,
filter_action="native",
sort_action="native",
sort_mode="multi",
column_selectable="single",
row_selectable="multi",
row_deletable=True,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current= 0,
export_format='xlsx',
export_headers='display',
virtualization=True,
),
html.Div(id='branch-datatable-container')
]),
html.Br(),
html.Br(),
html.Div([
html.H2(children='Complete list and details of PAR Accounts')]),
html.Div([
dt.DataTable(
id='complete-datatable',
columns=[{"name": i, "id": i, "deletable": True, "selectable": True}
for i in df.columns],
data=df.to_dict('records'),
style_as_list_view=True,
style_header={
'backgroundColor': 'skyblue',
'fontWeight': 'bold'
},
style_cell={'width': 'auto'},
editable=True,
filter_action="native",
sort_action="native",
sort_mode="multi",
column_selectable="single",
row_selectable="multi",
row_deletable=True,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current=0,
export_format='xlsx',
export_headers='display',
virtualization=True,
)
])
])
# In[63]:
@app.callback(
Output('product-datatable', 'style_data_conditional'),
[Input('product-datatable', 'selected_columns')]
)
def update_styles2(selected_columns):
return [{
'if': { 'column_id': i },
'background_color': '#D2F3FF'
} for i in selected_columns]
@app.callback(
Output('product-datatable-container', "children"),
[Input('product-datatable', "derived_virtual_data"),
Input('product-datatable', "derived_virtual_selected_rows")])
def update_graphs2(rows, derived_virtual_selected_rows):
if derived_virtual_selected_rows is None:
derived_virtual_selected_rows = []
dff = df if rows is None else pd.DataFrame(rows)
colors = ['#7FDBFF' if i in derived_virtual_selected_rows else '#0074D9'
for i in range(len(dff))]
return [
dcc.Graph(
id=column,
figure={
"data": [
{
"x": dff["PRODUCT GROUP"],
"y": dff[column],
"type": "bar",
"marker": {"color": colors},
}
],
"layout": {
"xaxis": {"automargin": True},
"yaxis": {
"automargin": True,
"title": {"text": column}
},
"height": 250,
"margin": {"t": 10, "l": 10, "r": 10},
},
},
)
for column in ["Outstanding Balance", "PAR"] if column in dff
]
@app.callback(
Output('classes-datatable', 'style_data_conditional'),
[Input('classes-datatable', 'selected_columns')]
)
def update_styles3(selected_columns):
return [{
'if': { 'column_id': i },
'background_color': '#D2F3FF'
} for i in selected_columns]
@app.callback(
Output('classes-datatable-container', "children"),
[Input('classes-datatable', "derived_virtual_data"),
Input('classes-datatable', "derived_virtual_selected_rows")])
def update_graphs3(rows, derived_virtual_selected_rows):
if derived_virtual_selected_rows is None:
derived_virtual_selected_rows = []
dff = df if rows is None else pd.DataFrame(rows)
colors = ['#7FDBFF' if i in derived_virtual_selected_rows else '#0074D9'
for i in range(len(dff))]
return [
dcc.Graph(
id=column,
figure={
"data": [
{
"x": dff["Classes"],
"y": dff[column],
"type": "bar",
"marker": {"color": colors},
}
],
"layout": {
"xaxis": {"automargin": True},
"yaxis": {
"automargin": True,
"title": {"text": column}
},
"height": 250,
"margin": {"t": 10, "l": 10, "r": 10},
},
},
)
for column in ["PAR Amount", "Accounts", "PAR/Account"] if column in dff
]
@app.callback(
Output('branch-datatable', 'style_data_conditional'),
[Input('branch-datatable', 'selected_columns')]
)
def update_styles4(selected_columns):
return [{
'if': { 'column_id': i },
'background_color': '#D2F3FF'
} for i in selected_columns]
@app.callback(
Output('branch-datatable-container', "children"),
[Input('branch-datatable', "derived_virtual_data"),
Input('branch-datatable', "derived_virtual_selected_rows")])
def update_graphs4(rows, derived_virtual_selected_rows):
if derived_virtual_selected_rows is None:
derived_virtual_selected_rows = []
dff = df if rows is None else pd.DataFrame(rows)
colors = ['#7FDBFF' if i in derived_virtual_selected_rows else '#0074D9'
for i in range(len(dff))]
return [
dcc.Graph(
id=column,
figure={
"data": [
{
"x": dff["BRANCH NAME"],
"y": dff[column],
"type": "bar",
"marker": {"color": colors},
}
],
"layout": {
"xaxis": {"automargin": True},
"yaxis": {
"automargin": True,
"title": {"text": column}
},
"height": 250,
"margin": {"t": 10, "l": 10, "r": 10},
},
},
)
for column in ["Total"] if column in dff
]
@app.callback(
Output('complete-datatable', 'style_data_conditional'),
[Input('complete-datatable', 'selected_columns')]
)
def update_styles(selected_columns):
return [{
'if': { 'column_id': i },
'background_color': '#D2F3FF'
} for i in selected_columns]
# In[64]:
# show_app(app)
# In[ ]:
if __name__ == '__main__':
app.run_server(debug=True, port=8080)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment