使用Dash Plotly无法使用多个输入返回输出

时间:2018-04-23 18:57:24

标签: python-3.x plotly plotly-dash

我看了一下python的Dash模块。 我正在尝试创建一个仪表板,我可以在其中选择不同的值并将行数与选择相关联。

我收到此错误消息volumetrie()缺少1个必需的位置参数。我找不到问题。当它是独立的时,代码就可以了。

拜托,你有什么想法解决这个问题吗?

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

app = dash.Dash()

table=pd.read_csv('xxxxx.csv',
                    sep=';', low_memory=True, memory_map=True)


app.layout = html.Div([
    html.H1(children='The Segments Project'),

html.Div(children='''
    Interactive Dashboard.
    '''),                   

html.Label('Coupons Activity - Activity Size'),
dcc.Dropdown(
    id='vCoupon_Activity_Size',
    options=[
        {'label': 'Never Used', 'value': 'Never Used'},
        {'label': 'Small', 'value': 'Small'},
        {'label': 'Medium', 'value': 'Medium'},
        {'label': 'Large', 'value': 'Large'},
        {'label': 'X-Large', 'value': 'X-Large'}
    ],
    value='Small'
),

html.Label('Coupons Activity - Prints Regularity'),
dcc.Dropdown(
    id='vCoupon_Activity_Prints_Regularity',
    options=[
        {'label': 'Inactive', 'value': 'Inactive'},
        {'label': 'Offline_Only', 'value': 'Offline_Only'},
        {'label': 'Churner', 'value': 'Churner'},
        {'label': 'Regular-', 'value': 'Regular-'},
        {'label': 'Regular', 'value': 'Regular'},
        {'label': 'Regular+', 'value': 'Regular+'},
        {'label': 'TopRegular', 'value': 'TopRegular'}
    ],
    value='Regular'
),

html.Label('Coupons Activity - Usage Regularity'),
dcc.Dropdown(
    id='vCoupon_Activity_Usage_Regularity',
    options=[
        {'label': 'Inactive', 'value': 'Inactive'},
        {'label': 'Offline_Only', 'value': 'Offline_Only'},
        {'label': 'Churner', 'value': 'Churner'},
        {'label': 'Regular-', 'value': 'Regular-'},
        {'label': 'Regular', 'value': 'Regular'},
        {'label': 'Regular+', 'value': 'Regular+'},
        {'label': 'TopRegular', 'value': 'TopRegular'}
    ],
    value='Regular'
),

html.Label('Coupons Activity - Converter Type'),
dcc.Dropdown(
    id='vCoupon_Activity_Converter_Type',
    options=[
        {'label': 'Offline_Only', 'value': 'Offline_Only'},
        {'label': 'Never Converted', 'value': 'Never Converted'},
        {'label': 'Small', 'value': 'Small'},
        {'label': 'Medium', 'value': 'Medium'},
        {'label': 'High', 'value': 'High'},
        {'label': 'Multicanal User', 'value': 'Multicanal User'}
    ],
    value='Regular'
),

html.Label('Gender'),
dcc.Dropdown(
    id='vCRM_Activity_Gender',
    options=[
        {'label': 'Female', 'value': 'Female'},
        {'label': 'Male', 'value': 'Male'},
        {'label': 'Unknown', 'value': 'Unknown'}
    ],
),

html.Label('CRM Activity - User Type'),
dcc.Dropdown(
    id='vCRM_Activity_User_Type',
    options=[
        {'label': 'Exclusive Print', 'value': 'Exclusive Print'},
        {'label': u'Multichannel', 'value': 'Multichannel'},
        {'label': 'Exclusive Web', 'value': 'Exclusive Web'}
    ],
    value='Multichannel'
),   

html.Div(id='output-volumetrie')

], style={'columnCount': 2})


@app.callback(
              dash.dependencies.Output('output-volumetrie','children'),
              [dash.dependencies.Input('vCRM_Activity_User_Type','value'),
               dash.dependencies.Input('vCRM_Activity_Gender','value'),
               dash.dependencies.Input('vCoupon_Activity_Converter_Type','value'),
               dash.dependencies.Input('vCoupon_Activity_Usage_Regularity','value'),
               dash.dependencies.Input('vCoupon_Activity_Prints_Regularity','value'),
               dash.dependencies.Input('vCoupon_Activity_Size','value')])

def volumetrie(table,vCRM_Activity_Gender='All',
       vCRM_Activity_User_Type='All',vCoupon_Activity_Converter_Type='All',vCoupon_Activity_Prints_Regularity='All',
       vCoupon_Activity_Usage_Regularity='All',vCoupon_Activity_Size='All'):

def filtre(table,vCRM_Activity_Gender,
       vCRM_Activity_User_Type,
       vCoupon_Activity_Converter_Type,
       vCoupon_Activity_Prints_Regularity,
       vCoupon_Activity_Usage_Regularity,
       vCoupon_Activity_Size):

    if vCoupon_Activity_Size!='All' : condition1=(table['Coupon_Activity_Size']==vCoupon_Activity_Size)
    else: condition1=True

    if vCoupon_Activity_Prints_Regularity!='All' : condition2=(table['Coupon_Activity_Prints_Regularity']==vCoupon_Activity_Prints_Regularity)
    else : condition2=True

    if vCoupon_Activity_Usage_Regularity!='All' : condition3=(table['Coupon_Activity_Usage_Regularity']==vCoupon_Activity_Usage_Regularity)
    else : condition3=True

    if vCoupon_Activity_Converter_Type!='All' : condition4=(table['Coupon_Activity_Converter_Type']==vCoupon_Activity_Converter_Type)
    else : condition4=True

    if vCRM_Activity_Gender!='All' : condition5=(table['CRM_Activity_Gender']==vCRM_Activity_Gender)
    else: condition5=True

    if vCRM_Activity_User_Type!='All' : condition6=(table['CRM_Activity_User_Type']==vCRM_Activity_User_Type)
    else : condition6=True


    res=table[condition1 & condition2 & condition3 & condition4 & condition5 & condition6]

    return(res)

return(len(filtre(table,vCRM_Activity_Gender,
   vCRM_Activity_User_Type,vCoupon_Activity_Converter_Type,


    vCoupon_Activity_Prints_Regularity,vCoupon_Activity_Usage_Regularity,vCoupon_Activity_Size)))



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

1 个答案:

答案 0 :(得分:0)

我犯的错误是将volumetrie函数与回调放在一起。 此代码现在正在运行。

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

app = dash.Dash()

table=pd.read_csv(xxxxx.csv)

def volumetrie(vCoupon_Activity_Usage_Regularity,vCoupon_Activity_Prints_Regularity,vCoupon_Activity_Size,
               vCoupon_Activity_Converter_Type):  

    if (vCoupon_Activity_Usage_Regularity!='All' and vCoupon_Activity_Prints_Regularity!='All'  and
        vCoupon_Activity_Size!='All'  and vCoupon_Activity_Converter_Type!='All' ):
        res=table    

    else:            
        if vCoupon_Activity_Usage_Regularity!='All' : condition1=(table['Coupon_Activity_Usage_Regularity']==vCoupon_Activity_Usage_Regularity)
        else : condition1=True
        if vCoupon_Activity_Prints_Regularity!='All' : condition2=(table['Coupon_Activity_Prints_Regularity']==vCoupon_Activity_Prints_Regularity)
        else : condition2=True
        if vCoupon_Activity_Size!='All' : condition3=(table['Coupon_Activity_Size']==vCoupon_Activity_Size)   
        else : condition3=True
        if vCoupon_Activity_Converter_Type!='All' : condition4=(table['Coupon_Activity_Converter_Type']==vCoupon_Activity_Converter_Type)    
        else : condition4=True
        res=table[condition1 & condition2 & condition3 & condition4]
        return(len(res))

app.layout = html.Div([
    html.H1(children='The X Project'),

    html.Div(children='''
        Your Data Visualization Tool.
        '''),                   

    html.Label('Coupons Activity - Usage Regularity'),
        dcc.RadioItems(
            id='vCoupon_Activity_Usage_Regularity',
            options=[
                {'label': 'All', 'value': 'All'},
                {'label': 'Inactive', 'value': 'Inactive'},
                {'label': 'Offline_Only', 'value': 'Offline_Only'},
            ],
            value='All'
        ),
    html.Label('Coupons Activity - Prints Regularity'),
        dcc.RadioItems(
            id='vCoupon_Activity_Prints_Regularity',
            options=[
                {'label': 'All', 'value': 'All'},
                {'label': 'Inactive', 'value': 'Inactive'},
                {'label': 'Offline_Only', 'value': 'Offline_Only'},
                {'label': 'Churner', 'value': 'Churner'},
            ],
            value='All'
        ),
    html.Label('Coupons Activity - Activity Size'),
        dcc.RadioItems(
            id='vCoupon_Activity_Size',
            options=[
                {'label': 'All', 'value': 'All'},
                {'label': 'Large', 'value': 'Large'},
                {'label': 'X-Large', 'value': 'X-Large'}
            ],
            value='All'
    ),
    html.Label('Coupons Activity - Converter Type'),
        dcc.RadioItems(
            id='vCoupon_Activity_Converter_Type',
            options=[
                {'label': 'All', 'value': 'All'},
                {'label': 'Offline_Only', 'value': 'Offline_Only'},
                {'label': 'Never Converted', 'value': 'Never Converted'},
                {'label': 'Small', 'value': 'Small'},
                {'label': 'Medium', 'value': 'Medium'},
                {'label': 'High', 'value': 'High'},
                {'label': 'Multicanal User', 'value': 'Multicanal User'}
            ],
            value='All'
    ),


    html.Hr(),
    html.Div('Volume'),
    html.Div(
         id='output'
         )

], style={'columnCount': 1})


@app.callback(dash.dependencies.Output('output','children'),
              [dash.dependencies.Input('vCoupon_Activity_Usage_Regularity','value'),
               dash.dependencies.Input('vCoupon_Activity_Prints_Regularity','value'),
                dash.dependencies.Input('vCoupon_Activity_Size','value'),
                dash.dependencies.Input('vCoupon_Activity_Converter_Type','value')])   
def output1(vCoupon_Activity_Usage_Regularity,vCoupon_Activity_Prints_Regularity,vCoupon_Activity_Size,
            vCoupon_Activity_Converter_Type):   
    vol=volumetrie(vCoupon_Activity_Usage_Regularity,vCoupon_Activity_Prints_Regularity,vCoupon_Activity_Size,
                   vCoupon_Activity_Converter_Type)
    return vol


app.config.supress_callback_exceptions = True

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