2 years ago

#60128

test-img

ivica

Getting an error with Dash Python -- Invalid argument `figure` passed into Graph with ID "bar_line_1". Expected `object`. Was supplied type `number`

**I work one project in python, with plotly dash and apache spark.But i have this error. How to fix it? I have this error: Invalid argument figure passed into Graph with ID "bar_line_1". Expected object. Was supplied type number. Value provided: 0

**

app.layout = html.Div([
        
            html.Div([
                html.Div([
                    html.Div([
                        html.H3('Spark Visual app', style = {"margin-bottom": "0px", 
   'color': 'white'}),
                       
        
                    ]),
                ], className = "six column", id = "title")
        
            ], id = "header", className = "row flex-display", style = {"margin-bottom": 
        "25px"}),
        
            html.Div([
                html.Div([
                    dcc.Graph(id = 'map_1',
                              config = {'displayModeBar': 'hover'}),
        
                ], className = "create_container 12 columns"),
        
            ], className = "row flex-display"),
        
            html.Div([
                html.Div([
                    html.P('Izaberi kategoriju:', className = 'fix_label', style = {'color': 
          'white'}),
                    dcc.Dropdown(id = 'w_countries',
                                 multi = False,
                                 clearable = True,
                                 disabled = False,
                                 style = {'display': True},
                                 value = 'Economy',
                                 placeholder = 'Select category',
                                 options = [{'label': c, 'value': c}
                                            for c in region], className = 'dcc_compon'),
        
                    html.P('Izaberi podkategoriju:', className = 'fix_label', style = {'color': 'white'}),
                    dcc.Dropdown(id = 'w_countries1',
                                 multi = False,
                                 clearable = True,
                                 disabled = False,
                                 style = {'display': True},
                                 placeholder = 'Select subcategory',
                                 options = [], className = 'dcc_compon'),
                    
                    html.P('Izaberi zemlju:', className = 'fix_label', style = {'color': 
         'white'}),
                    dcc.Dropdown(id = 'w_countries2',
                                 multi = True,
                                 clearable = True,
                                 disabled = False,
                                 style = {'display': True},
                                 value = ['European Union','Belgium'],
                                 placeholder = 'Select country',
                                 options = [{'label': c, 'value': c}
                                            for c in countries], className = 'dcc_compon'),
        
                    html.P('Izaberi godinu:', className = 'fix_label', style = {'color': 
            'white', 'margin-left': '1%'}),
                    dcc.RangeSlider(id = 'select_years',
                                    min = 2019,
                                    max = 2021,
                                    dots = False,
                                    value = [2020, 2021]),
        
                ], className = "create_container three columns"),
        
                
         html.Div([
               
                dcc.Graph(id='bar_line_1', figure={}, clickData=None, hoverData=None, 
                          config={
                              'staticPlot': False,     # True, False
                              'scrollZoom': True,      # True, False
                              'doubleClick': 'reset',  # 'reset', 'autosize' or 
         'reset+autosize', False
                              'showTips': False,       # True, False
                              'displayModeBar': True,  # True, False, 'hover'
                              'watermark': True,
                              # 'modeBarButtonsToRemove': ['pan2d','select2d'],
                                })
            ]),
        
                html.Div([
                    dcc.Graph(id = 'pie',
                              config = {'displayModeBar': 'hover'})
        
                ], className = "create_container three columns"),
        
            ], className = "row flex-display"),
        
        ], id = "mainContainer", style = {"display": "flex", "flex-direction": "column"})
        
        @app.callback(
            Output('w_countries1', 'options'),
            Input('w_countries', 'value'))
        def get_country_options(w_countries):
            terr3 = terr2[terr2['products'] == w_countries]
            return [{'label': i, 'value': i} for i in terr3['subproducts'].unique()]
        
        @app.callback(
            Output('w_countries1', 'value'),
            Input('w_countries1', 'options'))
        def get_country_value(w_countries1):
            return [k['value'] for k in w_countries1][0]
      
        #get Data for Economy Inflation
        def getArr(countryName):
            value=[]
            value1=[]
            data2=data.where(data.GEOLABEL==countryName).collect()
            for i in range(2,31):
                value.append(float(data2[0][i]))
            value1= np.array(value)
            return value1

        # Create line  chart 
        @app.callback(Output('bar_line_1', 'figure'),
                      [Input('w_countries', 'value')],
                      [Input('w_countries1', 'value')],
                      [Input('w_countries2', 'value')])
        def update_graph(w_countries, w_countries1,country_chosen):
            
           
                mon=np.array(terr2)
                mon1=mon[5][1]
            
                coun=[]
                coun=np.array(country_chosen)
                #
                # ECONOMY INFLATION
                #
                
                if(w_countries=='Economy'):
                    
                    if(w_countries1=='Inflation - annual growth rate'):
                        valueEu1=[]
                        if ('European Union' in coun):
                            valueEu1=getArr('European Union')
                            
                        valueSer1=[]
                        if ('Serbia' in coun):
                            valueSer1=getArr('Serbia')
                            
                        valueBel1=[]
                        if ('Belgium' in coun):
                            valueBel1=getArr('Belgium')
                            
                        return {
                            'data': [go.Scatter(x=lab,
                                                y=valueEu1,
                                                mode='lines+markers',
                                                name='European Union',
                                                line=dict(shape="spline", smoothing=1.3, width=3, color= '#E6D1D1'),
                                                marker= dict(size =5, symbol='circle', color='lightblue', line=dict(color='#E6D1D1', width=2)),
                                                hoverinfo='text',
                                                hovertext='<b>Country</b>: ' + 'European Union'+'<br>'
                                ),
                                     go.Scatter(x=lab,
                                                y=valueBel1,
                                                mode='lines+markers',
                                                name='Belgium',
                                                line=dict(shape="spline", smoothing=1.3, width=3, color='#FF00FF'),
                                                marker=dict(size=5, symbol='circle', color='lightblue', line=dict(color='#FF00FF', width=2)),
                                                hoverinfo='text',
                                                hovertext='<b>Country</b>: ' + 'Belgium'+'<br>'
                                         
                                         
                                         )
                                     
                                ],
                            'layout': go.Layout(
                                barmode = 'stack',
                                plot_bgcolor = '#808080',
                                paper_bgcolor = '#A8A8A8',
                                title = {
                                    'text': 'Inflation - annual growth rate'+ '  ' + '<br>' + 
                                    "(change compared with same month of previous year)" + '</br>',
        
                                'y': 0.93,
                                'x': 0.5,
                                'xanchor': 'center',
                                'yanchor': 'top'},
                                titlefont = {
                                    'color': 'white',
                                    'size': 20},
        
                                hovermode = 'closest',
                                showlegend = True,
        
                                xaxis = dict(title = '<b>Year</b>',
                                         spikemode  = 'toaxis+across',
                                         spikedash = 'solid',
                                         tick0 = 0,
                                         dtick = 1,
                                         color = 'white',
                                         showline = True,
                                         showgrid = True,
                                         showticklabels = True,
                                         linecolor = 'white',
                                         linewidth = 2,
                                         ticks = 'outside',
                                         tickfont = dict(
                                             family = 'Arial',
                                             size = 12,
                                             color = 'white'
                                         )
        
                                         ),
        
                                yaxis = dict(title = '<b>%</b>',
                                         color = 'white',
                                         showline = True,
                                         showgrid = True,
                                         showticklabels = True,
                                         linecolor = 'white',
                                         linewidth = 2,
                                         ticks = 'outside',
                                         tickfont = dict(
                                             family = 'Arial',
                                             size = 12,
                                             color = 'white'
                                         )
        
                                         ),
        
                                legend = {
                                    'orientation': 'h',
                                    'bgcolor': '#010915',
                                    'xanchor': 'center', 'x': 0.5, 'y': -0.3},
                                font = dict(
                                    family = "sans-serif",
                                    size = 12,
                                    color = 'white')
                                
                                )
                            }
                            
                        
                    
                    elif(w_countries1=='GDP – quarterly growth rate'):
                        return 0;
                    elif(w_countries1=='Monthly industrial production'):
                        return 0;
                    elif(w_countries1=='Monthly volume of retail trade'):
                        return 0;
                    else:
                        return 0;
                    
                    
                elif(w_countries =='Population and health'):
                    
                    if(w_countries1 =='Monthly excess mortality'):
                        valueEu1=[]
                        if ('European Union' in coun):
                            valueEu1=getPAHMEM('European Union')    
                    
                        valueBel1=[]
                        if ('Belgium' in coun):
                            valueBel1=getPAHMEM('Belgium')
                            
                        return {
                            'data': [go.Scatter(x=labPAHMEM,
                                                y=valueEu1,
                                                mode='lines+markers',
                                                name='European Union',
                                                line= dict(shape="spline", smoothing=1.3, width=3, color='#E6D1D1'),
                                                marker= dict(size=5, symbol='circle', color='lightblue', line=dict(color='#E6D1D1', width=2)),
                                                hoverinfo='text',
                                                hovertext='<b>Country</b>: ' + 'European Union' +'<br>'
                                
                                
                                ),
                                     go.Scatter(x=labPAHMEM,
                                                y=valueBel1,
                                                mode='lines+markers',
                                                name='Belgium',
                                                line=dict(shape="spline", smoothing=1.3, width=3, color='#FF0000'),
                                                marker=dict(size=5, symbol='circle', color='lightblue', line=dict(color='#FF0000', width=2)),
                                                hoverinfo='text',
                                                hovertext='<b>Country</b>: '+ 'Belgium' + '<br>'
                                         
                                         
                                         )
                                
                                ],
                            'layout': go.Layout(
                                barmode = 'stack',
                                plot_bgcolor = '#808080',
                                paper_bgcolor = '#A8A8A8',
                                title = {
                                    'text': 'Monthly excess mortality'+ '  ' + '<br>' + 
                                    "(% of additional deaths compared with average monthly deaths in 2016-2019)" + '</br>',
        
                                'y': 0.93,
                                'x': 0.5,
                                'xanchor': 'center',
                                'yanchor': 'top'},
                                titlefont = {
                                'color': 'white',
                                'size': 20},
        
                                hovermode = 'closest',
                                showlegend = True,
        
                                xaxis = dict(title = '<b>Year</b>',
                                         spikemode  = 'toaxis+across',
                                         spikedash = 'solid',
                                         tick0 = 0,
                                         dtick = 1,
                                         color = 'white',
                                         showline = True,
                                         showgrid = True,
                                         showticklabels = True,
                                         linecolor = 'white',
                                         linewidth = 2,
                                         ticks = 'outside',
                                         tickfont = dict(
                                             family = 'Arial',
                                             size = 12,
                                             color = 'white'
                                         )
        
                                         ),
        
                               yaxis = dict(title = '<b>% of additional deaths</b>',
                                         color = 'white',
                                         showline = True,
                                         showgrid = True,
                                         showticklabels = True,
                                         linecolor = 'white',
                                         linewidth = 2,
                                         ticks = 'outside',
                                         tickfont = dict(
                                             family = 'Arial',
                                             size = 12,
                                             color = 'white'
                                         )
        
                                         ),
        
                              legend = {
                                'orientation': 'h',
                                'bgcolor': '#010915',
                                'xanchor': 'center', 'x': 0.5, 'y': -0.3},
                              font = dict(
                                family = "sans-serif",
                                size = 12,
                                color = 'white')
                                
                                
                                
                                )
                            
                            
                            }
                        
                    
                    
                    elif(w_countries1=='Number of deaths by week'):
                        return 0;
                    else:
                        return 0;
                    
                elif(w_countries=='Society and work'):
                    
                    if(w_countries1=='Monthly unemployment rate'):
                        return 0;
                    
                    elif(w_countries1=='Monthly youth unemployment rate'):
                        return 0;
                    elif(w_countries1=='Quarterly employment'):
                        return 0;
                    elif(w_countries1=='Quarterly labour market slack'):
                        return 0;
                    elif(w_countries1=='Quarterly job vacancy rate'):
                        return 0;
                    else:
                        return 0;
                    
                else:
                    if(w_countries1=='Monthly air passenger transport'):
                        return 0;
                    elif(w_countries1=='Monthly commercial air flights'):
                        return 0;
                    elif(w_countries1=='Monthly arrivals at tourist accommodation'):
                        return 0;
                    elif(w_countries1=='Monthly nights spent at tourist accommodation'):
                        return 0;
                    else:
                        return 0;

python

csv

plotly-dash

invalid-argument

0 Answers

Your Answer

Accepted video resources