import pandas as pd
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objs as go
import os
app = dash.Dash()
print(dcc.__version__) # 0.6.0 or above is required
app.config.supress_callback_exceptions = True
filepath = os.path.join(os.path.dirname(__file__),"SharksDatabase.xlsx")
match_df = pd.read_excel(filepath, 'Match')
mp_df = pd.read_excel(filepath, 'MatchPlayer')
mp_df['Appearances'] = 1 # add appearances column to match player df
mp_df = mp_df.merge(match_df[['MatchID', 'Season', 'Competition Type']], on='MatchID', how='left')
ps_df = mp_df.groupby(['Player']).sum().reset_index() # stats by player (player stats)
pscs_df = mp_df.groupby(['Player', 'Season', 'Competition Type']).sum().reset_index()
seasons = match_df['Season'].unique()
comps = match_df['Competition Type'].unique()
#appearance processing - this can be optimised with showlegend=False
apps_sorted_df = ps_df.sort_values('Appearances').dropna(subset=['Appearances'])
players_ordered = apps_sorted_df['Player'].values
iterables = [players_ordered, seasons, comps]
app_plot_df = pd.Series(0, index=pd.MultiIndex.from_product(iterables, names=['Player', 'Season', 'Competition Type']))
app_plot_df = app_plot_df.reset_index()
app_plot_df.drop([0], axis=1, inplace=True)
app_plot_df = app_plot_df.merge(pscs_df, on=['Player', 'Season', 'Competition Type'], how='left')
app_plot_df['Appearances'].fillna(0, inplace=True)
app.layout = html.Div(children=[
dcc.Location(id='location', refresh=False),
dcc.Link('Player', href='/player'),
html.Br(),
dcc.Link('Match', href='/match'),
html.Div(children=[
html.Div(children=[
# html.Label('Page'),
dcc.RadioItems(
id='page',
options=[{'label': i, 'value': i} for i in ['Player', 'Match']],
value='Player',
# labelStyle={'display': 'inline-block'}
),
], ), # style={'padding': 10}),
html.Div(children=[
html.Label('Group By'),
dcc.RadioItems(
id='grouping',
options=[{'label': i, 'value': i} for i in ['Season', 'Competition']],
value='Season',
# labelStyle={'display': 'inline-block'}
),
], style={'display': 'inline-block', 'padding': 10}),
html.Div(children=[
html.Label('Competition'),
dcc.RadioItems(
id='comp-type',
options=[{'label': i, 'value': i} for i in ['All', 'League', 'Cup']],
value='All',
# labelStyle={'display': 'inline-block'}
),
], style={'display': 'inline-block', 'padding': 10}),
html.Div(children=[
html.Label('Season'),
dcc.Checklist(
id='season',
options=[{'label': season, 'value': season} for season in seasons],
values=list(seasons),
# labelStyle={'display': 'inline-block'}
),
], style={'display': 'inline-block', 'padding': 10}),
], style={'textAlign': 'center'}),
html.Div(id='page-body'),
dcc.Graph(id='app-graph'),
])
@app.callback(
dash.dependencies.Output('app-graph', 'figure'),
[dash.dependencies.Input('grouping', 'value'),
dash.dependencies.Input('comp-type', 'value'),
dash.dependencies.Input('season', 'values'),
dash.dependencies.Input('page', 'value')])
def update_app_figure(grouping_value, comp_type_value, season_values, page_value):
if comp_type_value == 'All':
comp_selector = ['League', 'Cup']
elif comp_type_value == 'League':
comp_selector = ['League']
elif comp_type_value == 'Cup':
comp_selector = ['Cup']
season_selector = season_values
user_app_plot_df = app_plot_df[(app_plot_df['Competition Type'].isin(comp_selector))
& (app_plot_df['Season'].isin(season_selector))]
if grouping_value == 'Season':
group_by_season = True
group_by_comp = False
elif grouping_value == 'Competition':
group_by_season = False
group_by_comp = True
traces = []
if group_by_season:
user_app_plot_df = user_app_plot_df.groupby(['Player', 'Season'], sort=False).sum().reset_index()
for season in seasons:
traces.append(
go.Bar(
x=user_app_plot_df[user_app_plot_df['Season'] == season]['Player'],
y=user_app_plot_df[user_app_plot_df['Season'] == season]['Appearances'],
name=season
)
)
elif group_by_comp:
user_app_plot_df = user_app_plot_df.groupby(['Player', 'Competition Type'], sort=False).sum().reset_index()
for comp in comps:
traces.append(
go.Bar(
x=user_app_plot_df[user_app_plot_df['Competition Type'] == comp]['Player'],
y=user_app_plot_df[user_app_plot_df['Competition Type'] == comp]['Appearances'],
name=comp
)
)
xaxis_template = dict(# title='Name',
zeroline=False)
yaxis_template = dict(title='Appearances',
zeroline=False)
layout = go.Layout(title='Appearances by Player',
xaxis=xaxis_template,
yaxis=yaxis_template,
barmode='stack',
legend=dict(orientation="h", x=0, y=1),)
data = traces
fig = go.Figure(
data=data,
layout=layout)
return fig
if __name__ == '__main__':
app.run_server(debug=True)