Incorporating mask into app.callback for Dash

  Kiến thức lập trình

wanted to open by saying that I’m new to python (and to coding overall), so apologies if this question is bad.

I’m running into an issue using Dash. What I’m trying to do is create a Mask that returns True only for the firm the user has selected, however when I run the code I get no errors, but it sets every firm to be true, and whatever the user selects does nothing. What’s weird is that I have code that does essentially the same thing, and runs without issue. I’ve taken this code from the plotly documentation on building line charts, and edited it slightly to fit my needs.

I tried running the code below, and it returns a dashboard without error. Users are given a dropdown on which firm they want to display, and it should only display whichever firm (or firms) they’ve picked. However the graph it returns displays EVERY firm, and whatever the user picks doesn’t change the plotly figure displayed

     app = Dash('Affil Sales')

     app.layout = html.Div([
            html.H4('Affil vs Non-Affil price'),
            dcc.Graph(id = "graph"),
             options= [
                {'label': i, 'value': i} for i in list(df2['z_seller_clean'].unique())],
             multi = True,
             placeholder = 'Select a firm'


Here I used “{‘label’: i, ‘value’: i} for i in list(df2[‘z_seller_clean’].unique())]” to set the options. There are to many firms listed to be able to write out manually. This works to create the dropdown

   Output("graph", "figure"), 
    Input("dropdown", "value"))
 def update_line_chart(z_seller_clean):
       df3 = df2
       mask = df3.z_seller_clean.isin(list(df3['z_seller_clean'].unique())) # Remember df2[mask]
         fig1 = px.line(df3[mask], 
              x="TRADE_DATE", y="Z_PRICEINDOLPERMWH", color='Buyer_Is_Affiliate')
return fig1


This code runs, but it returns a graph with every firm displayed, How do I set up the mask function to actually work? If I remove the mask function, the code runs the exact same. The goal of the mask is to create a Boolean, it should return False for every firm not picked by the user, and return True for whatever firms have been picked. However it just returns True for every firm

I have another set of code that runs without issue, I’ll paste it below

   df_gb_q = df.groupby(['TRADE_DATE', 'Buyer_Is_Affiliate', 'Z_REGION'])[['Z_QUANTITYINMWH']].mean()


    app_region_mwh = Dash('Affil Sales')

        app_region_mwh.layout = html.Div([
           html.H4('Affil vs Non-Affil MWH'),
           dcc.Graph(id = "graph"),
              options= ["Florida", "Northwest", "Southeast","Southwest"],
              value = ["Florida"],

          Output("graph", "figure"), 
          Input("checklist", "value"))
    def update_line_chart(Z_REGION):
          df_new_q = df_gb_q
           mask = df_new_q.Z_REGION.isin(Z_REGION)
           fig_q_reg = px.line(df_new_q[mask], 
                 x="TRADE_DATE", y="Z_QUANTITYINMWH", color='Buyer_Is_Affiliate')
      return fig_q_reg


This code creates a dashboard the runs without error, and only displays whatever is on the checklist (i.e whatever the user has picked)

New contributor

Aidan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website