pandas DASH PLOTLY回调错误更新county_dropdown.value

wztqucjr  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(117)

在浏览器而不是终端或jupyter notebook中接收回调错误。我正在接收我想要的结果,但我得到了错误消息。我已经重新启动了计算机,并确保没有其他浏览器在端口8050上运行(另一个堆栈问题的建议)。

**结果我想:**如果在“选择州”下拉列表中选择了一个州,请在第二个下拉列表“选择县”中仅提供与该州相关的县。感谢所有帮助。谢谢!

dataframe大约有3200行。我已经检查了所有的states和counties元素都有数据。我还在jupyter notebook中运行了以下代码,没有收到任何索引错误:

filtered_state = df1[df1['STATE_NAME'] == state_dropdown]
[{'label': i, 'value': i} for i in filtered_state['COUNTY'].unique()]

[k['value'] for k in county_dropdown][0]

我尝试将下面函数中的'value'改为children和none。

@callback(
    Output('county_dropdown', 'value'),
    Input('county_dropdown', 'options'))
def get_county_value(county_dropdown):
    return [k['value'] for k in county_dropdown][0]

app.py

from dash import Dash, html, dash_table, dcc, callback, Output, Input
from dash import html
from dash import dcc
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import pandas as pd


df1 = pd.read_csv('main.csv')

state = df1['STATE_NAME'].unique()

app = Dash(__name__, )
app.layout = html.Div([

    html.Div([
        html.Div([
            html.Div([
                html.Div([
                    html.H3('Census Data', style = {"margin-bottom": "0px", 'color': 'white'}),
                    html.H5('2023', style={"margin-top": "0px", 'color': 'white'}),

                ]),
            ], className="six column", id = "title"),

        ], id="header", className="row flex-display", style={"margin-bottom": "25px"}),

            html.Div([

                html.H3('Select State:', className='fix_label', style={'color': 'white'}),
                dcc.Dropdown(id="state_dropdown",
                             multi=False,
                             clearable=True,
                             disabled=False,
                             style={'display': True},
                             placeholder='Select State',
                             options=[{'label': c, 'value': c}
                                        for c in state], className='dcc_compon'),

                html.H3('Select County:', className='fix_label', style={'color': 'white', 'margin-left': '1%'}),
                dcc.Dropdown(id="county_dropdown",
                             multi=False,
                             clearable=True,
                             disabled=False,
                             style={'display': True},
                             placeholder='Select County',
                             options=[], className='dcc-compon'),
                ], className="create_container three columns"),

        ], className="row flex-display"),
    ], id="mainContainer", style={"display": "flex", "flex-direction": "column"})

# CREATE CALLBACK TO GET UNIQUE COUNTY NAMES
@callback(
    Output('county_dropdown', 'options'),
    Input('state_dropdown', 'value')
)
def get_county_options(state_dropdown):
    filtered_state = df1[df1['STATE_NAME'] == state_dropdown]
    return [{'label': i, 'value': i} for i in filtered_state['COUNTY'].unique()]

# CREATE CALLBACK FOR COUNTY DROPDOWN
@callback(
    Output('county_dropdown', 'value'),
    Input('county_dropdown', 'options'))
def get_county_value(county_dropdown):
    return [k['value'] for k in county_dropdown][0]

if __name__ == '__main__':
    app.run_server(debug=True)

数据集:main.csv

STATE_NAME,COUNTY,POPULATION,HOUSEHOLDS,FAMILIES,SNAP
 Alabama,Autauga County,58239,21856,15321,2298
 Alabama,Baldwin County,227131,87190,58062,5839
 Alabama,Barbour County,25259,9088,5860,2331
 Alabama,Bibb County,22412,7083,5018,1198
 Alabama,Blount County,58884,21300,15213,2135
 Alabama,Bullock County,10386,3419,2077,972
 Alabama,Butler County,19181,6531,4317,1050
 Alabama,Calhoun County,116425,44295,27960,7720
 Alabama,Chambers County,34834,13123,9091,1982
 Alabama,Cherokee County,24975,9692,6430,1236
 Alabama,Chilton County,44857,16562,11814,2287
 Alabama,Choctaw County,12792,5211,3256,1077
 Alabama,Clarke County,23346,8250,5333,1653
 Alabama,Clay County,14184,5399,3721,741
 Alabama,Cleburne County,15046,5598,3755,812
 Alabama,Coffee County,53043,20478,13812,2421
 Alabama,Colbert County,56789,22640,14848,3302
 Alabama,Conecuh County,11778,4187,2498,503
 Alabama,Coosa County,10442,3824,2591,399
 Alabama,Covington County,37490,14296,9356,1764
 Alabama,Crenshaw County,13300,4653,2936,863
 Alabama,Cullman County,87129,32939,22795,3478
 Alabama,Dale County,49443,19470,12543,3529
 Alabama,Dallas County,39162,14385,8122,4045
 Alabama,DeKalb County,71554,25601,18329,4184
 Alabama,Elmore County,87146,31630,23240,3514
 Alabama,Escambia County,36879,12878,7916,1962
 Alabama,Etowah County,103468,38289,24686,5191
 Alabama,Fayette County,16365,6204,4141,1179
 Alabama,Franklin County,32034,10955,7449,2040
 Alabama,Geneva County,26604,10303,6884,1829
 Alabama,Greene County,7851,2849,1376,723
 Alabama,Hale County,14819,5133,3158,1283
 Alabama,Henry County,17165,6404,4294,929
 Alabama,Houston County,106355,41095,26907,5717
 Alabama,Jackson County,52548,20276,13951,2623
 Alabama,Jefferson County,672550,264105,162288,35046
 Alabama,Lamar County,13929,5351,3509,1038
 Alabama,Lauderdale County,93342,37928,24365,4373
 Alabama,Lawrence County,33089,12469,8791,1798
 Alabama,Lee County,172223,63122,39293,5595
 Alabama,Limestone County,101217,37987,26793,3408
 Alabama,Lowndes County,10334,3878,2367,1101
 Alabama,Macon County,19490,7136,4173,1519
 Alabama,Madison County,382149,156305,99835,14536
 Alabama,Marengo County,19397,7248,3797,1286
 Alabama,Marion County,29392,11131,7220,1626
 Alabama,Marshall County,97179,35439,24759,3820
 Alabama,Mobile County,414620,158045,102070,26083
 Alabama,Monroe County,20115,7333,4429,853
 Alabama,Montgomery County,229072,89134,54217,17165
 Alabama,Morgan County,122608,47459,31623,6243
 Alabama,Perry County,8702,2847,1515,893
 Alabama,Pickens County,19240,6993,4705,1065
 Alabama,Pike County,33176,11508,6294,1801
 Alabama,Randolph County,21984,8607,5812,1422
 Alabama,Russell County,58695,23141,14687,4514
 Alabama,St. Clair County,90412,33218,23988,3099
 Alabama,Shelby County,220780,82005,58302,3991
 Alabama,Sumter County,12482,4808,2871,1288
 Alabama,Talladega County,81850,31778,20945,5227
 Alabama,Tallapoosa County,41284,16380,10883,3092
 Alabama,Tuscaloosa County,223945,81790,52301,9014
 Alabama,Walker County,65194,24646,16466,3583
 Alabama,Washington County,15574,5261,3882,682
 Alabama,Wilcox County,10686,3626,2230,1133
 Alabama,Winston County,23650,9219,6511,1268
 Alaska,Aleutians East Borough,3409,914,568,108
 Alaska,Aleutians West Census Area,5251,1004,553,62
 Alaska,Anchorage Municipality,292545,106695,69003,9236
 Alaska,Bethel Census Area,18514,4520,3373,1796
 Alaska,Bristol Bay Borough,849,315,187,14
 Alaska,Chugach Census Area,7015,2592,1717,190
 Alaska,Copper River Census Area,2635,946,627,129
 Alaska,Denali Borough,2187,531,274,17
 Alaska,Dillingham Census Area,4899,1372,1059,399
 Alaska,Fairbanks North Star Borough,97149,35298,23400,2005
 Alaska,Haines Borough,2098,773,463,19
 Alaska,Hoonah-Angoon Census Area,2327,811,469,130
 Alaska,Juneau City and Borough,32240,12922,7414,1061
 Alaska,Kenai Peninsula Borough,58711,22768,13789,2164
 Alaska,Ketchikan Gateway Borough,13939,5487,3619,648
 Alaska,Kodiak Island Borough,13218,4416,3087,433
 Alaska,Kusilvak Census Area,8354,1815,1499,972
 Alaska,Lake and Peninsula Borough,986,319,232,96
 Alaska,Matanuska-Susitna Borough,106807,38056,27166,4054
 Alaska,Nome Census Area,10070,2714,2007,890
 Alaska,North Slope Borough,10865,2103,1561,411
 Alaska,Northwest Arctic Borough,7776,1756,1285,591
 Alaska,Petersburg Borough,3368,1211,761,97
 Alaska,Prince of Wales-Hyder Census Area,5886,2310,1445,356
 Alaska,Sitka City and Borough,8518,3439,2172,260
 Alaska,Skagway Municipality,1329,390,184,10
 Alaska,Southeast Fairbanks Census Area,6849,2127,1376,149
 Alaska,Wrangell City and Borough,2162,842,472,108
 Alaska,Yakutat City and Borough,562,216,154,10
 Alaska,Yukon-Koyukuk Census Area,5433,1899,1065,602
 Arizona,Apache County,66473,19443,12866,5585
 Arizona,Cochise County,125092,49239,30527,7209
 Arizona,Coconino County,144942,51037,30361,5194
 Arizona,Gila County,53211,22306,13770,3445
 Arizona,Graham County,38145,11577,8205,1392
 Arizona,Greenlee County,9542,3265,2262,269
 Arizona,La Paz County,16845,8678,5248,1289
f3temu5u

f3temu5u1#

默认情况下,回调在启动时调用。
您已经将county_dropdownoptions属性设置为空列表,但问题是您在get_county_value回调中这样做:

return [k['value'] for k in county_dropdown][0]

因为county_dropdown是一个空列表,所以得到一个IndexError
一种方法是将prevent_initial_call=True添加到两个回调中。然后get_county_value回调仅在get_county_options更新county_dropdownoptions属性后触发。
另一种方法是在尝试访问列表中的元素之前添加一个检查,以查看列表是否为空。如果列表为空,则可以运行raise PreventUpdate或返回其他值。

相关问题