Commit 64deaaa0 authored by Carlo Angelo Cordero's avatar Carlo Angelo Cordero
Browse files

Upload New File

parent 582bd71c
#!/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)
# https://dash.plot.ly/dash-core-components
# https://dash.plot.ly/dash-html-components
# https://dash.plot.ly/datatable
# In[2]:
df = pd.read_excel('data/par_final.xlsx', usecols="B:AM")
df.head()
# In[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()
df_pivot_amount.head()
# In[7]:
app = dash.Dash(assets_folder='static/', )
datos = df_pivot_amount
app.layout = html.Div([
dt.DataTable(
id='datatable-interactivity',
columns=[{"name": i, "id": i, "deletable": True, "selectable": True}
for i in datos.columns],
data=datos.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,
# page_size= 10,
export_format='xlsx',
export_headers='display',
virtualization=True,
),
html.Div(id='datatable-interactivity-container')
])
@app.callback(
Output('datatable-interactivity', 'style_data_conditional'),
[Input('datatable-interactivity', 'selected_columns')]
)
def update_styles(selected_columns):
return [{
'if': { 'column_id': i },
'background_color': '#D2F3FF'
} for i in selected_columns]
@app.callback(
Output('datatable-interactivity-container', "children"),
[Input('datatable-interactivity', "derived_virtual_data"),
Input('datatable-interactivity', "derived_virtual_selected_rows")])
def update_graphs(rows, derived_virtual_selected_rows):
# When the table is first rendered, `derived_virtual_data` and
# `derived_virtual_selected_rows` will be `None`. This is due to an
# idiosyncracy in Dash (unsupplied properties are always None and Dash
# calls the dependent callbacks when the component is first rendered).
# So, if `rows` is `None`, then the component was just rendered
# and its value will be the same as the component's dataframe.
# Instead of setting `None` in here, you could also set
# `derived_virtual_data=df.to_rows('dict')` when you initialize
# the component.
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},
},
},
)
# check if column exists - user may have deleted it
# If `column.deletable=False`, then you don't
# need to do this check.
for column in ["Total"] if column in dff
]
# In[8]:
# show_app(app)
# In[ ]:
if __name__ == '__main__':
app.run_server(debug=True, port=8000)
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