添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I’m new to Plotly and Dash, and trying to create an interactive app for friends.

When I try and run the app I get an attribute error saying ‘list’ object has no attribute ‘Div’.

I’ve been following the dash documentation but I’m unsure of what I’m doing wrong. I also can’t tell if the callback and dropdown I’ve added are working.

Any guidance would be greatly appreciated.

import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
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"
#getting data
player_data = []
season = list(reversed(range(2010,2022)))
for year in season:
  url = 'https://www.basketball-reference.com/leagues/NBA_' + str(year) + '_per_game.html'
  html = pd.read_html(url, header = 0)
  df = html[0]
  df['Season'] = year
  year -= 1
  df = df.drop(['Rk'], axis=1)
  player_data.append(df)
player_data = pd.concat(player_data)
#creating the app
app.layout = html.Div([
  dcc.Dropdown(
    id="season-filter",
    options=[
        {"label": season, "value": season}
        for season in np.sort(player_data.Season.unique())
    clearable=False,
    multi=True,
    className="dropdown",
dt.DataTable(
    id = 'player-table',
    columns = [{'name': i, 'id': i} for i in player_data.columns],
    data =  player_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,
    page_action = 'custom'
#creating callback to update table
@app.callback(
  Output('player-table','data'),
  Input('season-filter', 'value')
def update_table(player_data, season):
  dff = player_data[player_data.Season == season]
  return dff.to_dict("rows")
if __name__ == '__main__':
  app.run_server(debug = True)
              

It seems that you do not have the dataTable inside your layout. You can try this:

playerTable = dt.DataTable(
    id = 'player-table',
    columns = [{'name': i, 'id': i} for i in player_data.columns],
    data =  player_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,
    page_action = 'custom'
#creating the app
app.layout = html.Div([
  dcc.Dropdown(
    id="season-filter",
    options=[
        {"label": season, "value": season}
        for season in np.sort(player_data.Season.unique())
    clearable=False,
    multi=True,
    className="dropdown",
  playerTable

I did not get your code to run on my system, so I could not test it

Hi, thanks for your reply.

When I tried your changes I still got the same attribute error.

It always highlights the following as the source of the issue
app.layout = html.Div([

This is odd because based on the documentation there isn’t anything wrong with that, and “children” is optional.

html = pd.read_html(url, header = 0)

And then try to use the html keyword again in a different way:

app.layout = html.Div([

I would just change that first html to something like html_page instead