import dash
import plotly.graph_objects as go
import plotly.express as px
from dash.dependencies import Input, Output
from dash_table.Format import Format
import dash_bootstrap_components as dbc
import mysql.connector
import dash_core_components as dcc
import datetime
import pymssql
import pandas as pd
import math
import numpy as np
from datetime import datetime
from pandas.tseries.offsets import BDay
# getting last business day date
today = datetime.today()
yesterday = today - BDay(1)
app = Dash(__name__, suppress_callback_exceptions=True,external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div([
html.Hr(),
html.H3(id = 'H1', children = ['TD Flow'], style = {'textAlign':'center', 'color':'rgb(0, 128, 0)','fontSize':'25px'}),
dbc.Row([
# default date is last business day date
dbc.Col([
html.Label(id = 'Date', children = ' Date: ',style = {'color':'rgb(0, 128, 0)','marginLeft':10}),
dcc.Input(id ='datevalue',
value = datetime.strftime(yesterday, '%Y%m%d'),
style = {'textAlign':'center', 'marginLeft':'10px'},
type='text'
),
],width = 3),
# notional default is 500,000 thus only rows with notional>500,000 show up by default
dbc.Col([
html.Label(id = 'Ticker', children = ' Ticker: ',style = {'color':'rgb(0, 128, 0)'}),
dcc.Input(id ='ticker',
value = 'ECN',
style = {'textAlign':'center','marginLeft':'5px'},
type='text'
),
],width = {'size':4,'offset':2}),
# allows user to fetch the first or last batch of stock prices
dbc.Col([
html.Label(id = 'Currency', children = ' Currency: ',style = {'color':'rgb(0, 128, 0)','marginLeft':10}),
dcc.Input(id ='currency',
value='CAD',
style = {'textAlign':'center','marginLeft':'5px'},
type = 'text',
),
],width = 3),
]),
# lets user update input fields themselves to update output, includes a loading button
dbc.Spinner(children=[html.Button('Update', id='interval-component',style = {'marginLeft':'10px'}),
html.Div(id='pagecontent'),
], color = 'rgb(0,128,0)'),
])
# callback takes in 4 variables including one input variable and 3 state variables - so that output only updates once user presses 'Update'
@callback(
Output('pagecontent','children'),
[State('datevalue','value'),
State('ticker','value'),
State('currency','value'),
Input('interval-component', 'n_clicks')
])
# datatable is using the dataframe from the query
def update_output(dateval,symbol,curr,n):
try:
conn = ...
cursor = conn.cursor(buffered=True)
...
df = pd.read_sql(sql, conn)
# creating the summary as a pivot table
table = pd.pivot_table(df, values = 'Volume',index = ['Desk'],columns = ['TransactionType'], aggfunc=np.sum, fill_value=0, margins=True, margins_name='Grand Total')
# coverting pivot table to a dataframe
table = pd.DataFrame(table.to_records())
# converting the dataframe df to a dash_table
tableoutput = dash_table.DataTable(id='dataframe', columns=[{'id':c, 'name':c, 'type':'numeric', 'format':Format(group=True, groups=[3])} for c in df.columns],
data=df.to_dict('records'),editable=False,filter_action="native",sort_action="native",selected_rows=[],sort_mode="multi",page_current= 0,page_action="native",
css=[{'selector': '.dash-spreadsheet tr', 'rule': 'height: 10px;'}], fixed_rows={'headers': True},
style_cell_conditional=[
{
'if': {'column_id': c},
'textAlign': 'center','width': '8.33%'
} for c in df.columns
],
style_data={
'color': 'black',
'backgroundColor': 'white'
},
style_header={
'backgroundColor': 'rgb(0, 128, 0)',
'color': 'white',
'fontWeight': 'bold'
}
)
return tableoutput
except Exception as e:
cnx.close()
print(str(e))