Thanks for your reply.
I’ve read the documentation but haven’t seen any possible solutions to my issue.
To illustrate I’ve included my code below.
Code
import dash
from dash.dependencies import Input, Output
from dash import dcc
from dash import html
from dash import dash_table as dt
import plotly.express as px
import pandas as pd
import numpy as np
app = dash.Dash(__name__)
app.title = "Fantasy NBA dashboard: Understand multi-year player performance"
colors = {
'background': '#111111',
'text': '#7FDBFF'
}
def acquire_bbref_data():
player_data = []
season = list(reversed(range(2010, 2023)))
for year in season:
url = 'https://www.basketball-reference.com/leagues/NBA_' + str(year) + '_per_game.html'
data = pd.read_html(url, header=0)
df = data[0]
df['Season'] = year
year -= 1
df = df.drop(['Rk'], axis=1)
player_data.append(df)
player_data = pd.concat(player_data)
return player_data
season_data = acquire_bbref_data()
"""Creating the app"""
app = dash.Dash(__name__)
app.title = "NBA data"
app.layout = html.Div([
html.H1(
children="NBA data",
className="header-title"
),
html.P(
children="Placeholder text ",
className="header-description",
),
html.Div([
html.Div(children="Season", className="menu-title"),
dcc.Dropdown(
id="season-filter",
options=[
{"label": Season, "value": Season}
for Season in np.sort(season_data.Season.unique())
],
clearable=False,
multi=True,
className="dropdown",
placeholder="Choose a season",
), html.Br(),
]
),
html.Div([
html.Div(children="Team", className="menu-title"),
dcc.Dropdown(
id="team-filter",
options=[
{"label": Tm, "value": Tm}
for Tm in np.sort(season_data.Tm.unique())
],
clearable=False,
multi=True,
className="dropdown",
placeholder="Choose a team",
),
html.Br(),
]
),
html.Div([
html.Div(children="Position", className="menu-title"),
dcc.Dropdown(
id="pos-filter",
options=[
{"label": Pos, "value": Pos}
for Pos in np.sort(season_data.Pos.unique())
],
clearable=False,
multi=True,
className="dropdown",
placeholder="Choose a position",
),
html.Br(),
]
),
html.Div([
html.Div(children="Player", className="menu-title"),
dcc.Dropdown(
id="player-filter",
options=[
{"label": Player, "value": Player}
for Player in np.sort(season_data.Player.unique())
],
clearable=False,
multi=True,
className="dropdown",
placeholder="Choose a player",
),
html.Br(),
]
),
html.Div([
dt.DataTable(
id="player-table",
columns=[{"name": i, "id": i} for i in season_data.columns],
data=season_data.to_dict("records"),
style_table={"overflowX": "auto"},
style_cell={
"textAlign": "left",
"padding": "3px",
"whiteSpace": "normal",
"height": "auto",
},
style_header={
"backgroundColor": "teal",
"fontWeight": "bold",
},
page_current=0,
page_size=10,
),
]
),
]
)
"""Call to update the player filter based on other filters """
#The callback below currently works with one input. If I try and extend this out to the others it doesn't work
# I've tried separating it into multiple callbacks but Plotly doesn't allow for this
@app.callback(
Output("player-filter", "options"),
[
Input("season-filter", "value"),
# Input("team-filter", "value"),
# Input("pos-filter", "value")
])
def update_player_filter(d1):
if d1 is not None:
dff = season_data[season_data["Season"].isin(d1)]
return [{'label': i, 'value': i} for i in dff["Player"].unique()]
else:
return [] # returns an empty list as it's easier to check things are working
# will have it return the below player list once the above is resolved
# return [{'label': i, 'value': i} for i in season_data["Player"].unique()]
if __name__ == "__main__":
app.run_server(debug=True)
Hopefully the above makes it clearer what I’ve trying to achieve.