2

我是 dash 的新手,在寻找在回调中使用数据框的示例时遇到问题。我创建了一个每周单选按钮和一个每月单选按钮。

When the monthly radio button is selected I would like the graph to pull data from df_monthlywhere each bar would be a monthly sum of pay. 当每周单选按钮被选中时,我希望看到图表每周填充每个条形图,这将是数据框中的每一行,因为我每周获得一次报酬。

我不确定哪里出错了,但我不断收到一条错误消息TypeError: update_fig() takes 0 positional arguments but 1 was given

该图填充没有数据,如下图所示。感谢您对此事的任何帮助。

在此处输入图像描述

import dash
import dash_core_components as dcc 
import dash_html_components as html 
import plotly.plotly as py
import plotly.graph_objs as go
import sqlite3
import pandas as pd
from functools import reduce
import datetime

conn = sqlite3.connect('paychecks.db')

df_ct = pd.read_sql('SELECT * FROM CheckTotal',conn)
df_earn = pd.read_sql('SELECT * FROM Earnings', conn)
df_whold = pd.read_sql('SELECT * FROM Withholdings', conn)

data_frames = [df_ct, df_earn, df_whold]
df_paystub = reduce(lambda  left,right: pd.merge(left,right,on=['Date'], how='outer'), data_frames)

def date_extraction(df):
    df['Date'] = pd.to_datetime(df['Date'])
    df['Year'] = df['Date'].dt.strftime('%Y')
    df['Month'] = df['Date'].dt.strftime('%B')
    df['Day'] = df['Date'].dt.strftime('%d')
    return df

date_extraction(df_paystub)

df_monthly = df_paystub.groupby(['Month']).sum()

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.css.append_css({'external_url': 'https://codepen.io/amyoshino/pen/jzXypZ.css'})

app.layout = html.Div(children=[

    html.Div([
        html.Div([
            dcc.RadioItems(
                        id='data-view',
                        options=[
                            {'label': 'Weekly', 'value': 'Weekly'},
                            {'label': 'Monthly', 'value': 'Monthly'},
                        ],
                        value='',
                        labelStyle={'display': 'inline-block'}
                    ),
        ], className = 'two columns'),

        html.Div([    
            dcc.Dropdown(
                id='year-dropdown',
                options=[
                        {'label': i, 'value': i} for i in df_paystub['Year'].unique()
                ],
                placeholder="Select a year",
            ),
        ], className='five columns'),

        html.Div([    
            dcc.Dropdown(
                id='month-dropdown',
                options=[
                  {'label': i, 'value': i} for i in df_paystub['Month'].unique()
                ],
                placeholder="Select a month(s)",
                multi=True,
            ),
        ], className='five columns'),
    ], className  = 'row'),


    # HTML ROW CREATED IN DASH
    html.Div([
        # HTML COLUMN CREATED IN DASH
        html.Div([
            # PLOTLY BAR GRAPH        
            dcc.Graph(
                id='pay',
            )
        ], className  = 'six columns'),

        # HTML COLUMN CREATED IN DASH
        html.Div([
            # PLOTLY LINE GRAPH
            dcc.Graph(
                id='hours',
                figure={
                    'data': [
                        go.Scatter(
                            x = df_earn['Date'],
                            y = df_earn['RegHours'],
                            mode = 'lines',
                            name = 'Regular Hours',
                        ),
                        go.Scatter(
                            x = df_earn['Date'],
                            y = df_earn['OtHours'],
                            mode = 'lines',
                            name = 'Overtime Hours',
                        )
                    ]
                }
            )
        ], className='six columns')
    ], className='row')
], className='ten columns offset-by-one')

@app.callback(dash.dependencies.Output('pay', 'figure'),
              [dash.dependencies.Input('data-view', 'value')])

def update_fig():
    figure={
        'data': [
            go.Bar(
                x = df_monthly['Month'],
                y = df_monthly['CheckTotal'],
                name = 'Take Home Pay',
            ),
                go.Bar(
                x = df_monthly['Month'],
                y = df_monthly['EarnTotal'],
                name = 'Earnings',
            )
        ],
        'layout': go.Layout(
            title = 'Take Home Pay vs. Earnings',
            barmode = 'group',
            yaxis = dict(title = 'Pay (U.S. Dollars)'),
            xaxis = dict(title = 'Date Paid')
        )
    }
    return figure

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

1 回答 1

0

嗨@prime90,欢迎来到 Dash。

看一眼你的回调签名,看起来update_fig()函数需要接受Input你给它的(使用dash.dependencies.Input)。

回调将向您发送Input您指定的应用程序中的哪些更改。因此,它会将您提供给您的函数的valueof发送#data-view给您的函数update_fig(),该函数当前不接受任何变量,从而导致错误消息。

只需更新您的函数签名并添加几个布尔变量即可摆脱错误并获得潜在的功能:


def update_fig(dataview_value):
    # define your weekly OR monthly dataframe 
    # you'll need to supply df_weekly similarly to df_monthly
    # though DO NOT modify these, see note below!
    df = df_weekly if dataview == 'weekly' else df_monthly
    dfkey = 'Week' if 'week' in df.columns else 'Month' # eh, worth a shot!
    figure={
        'data': [
            go.Bar(
                x = df[dfkey],
                y = df['CheckTotal'],
                name = 'Take Home Pay',
            ),
                go.Bar(
                x = df[dfkey],
                y = df['EarnTotal'],
                name = 'Earnings',
            )
        ],
        'layout': go.Layout(
            title = 'Take Home Pay vs. Earnings',
            barmode = 'group',
            yaxis = dict(title = 'Pay (U.S. Dollars)'),
            xaxis = dict(title = 'Date Paid')
        )
    }
    return figure

正如上面评论中所写,您需要进行某种类型的事先操作来创建 df_weekly,就像您当前的 df_monthly 一样。

此外,我编写的代码片段假定 df 列被命名为“周”和“月”——显然需要更新这些。

Dash 中的数据操作:

确保您阅读了数据共享文档,因为它们强调了数据不应该被修改超出范围

我希望这有帮助 :-)

于 2019-04-02T21:25:10.650 回答