首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有破折号参数问题的仪表板dcc.graph()

带有破折号参数问题的仪表板dcc.graph()
EN

Stack Overflow用户
提问于 2021-07-15 00:35:18
回答 1查看 240关注 0票数 0

我正在尝试使用dash在Pycharm中创建一个仪表板。这是我一直收到的错误,

代码语言:javascript
复制
 html.Div(dcc.Graph(id='line-plot')),
TypeError: Graph() takes no arguments

下面是我发现错误的代码片段(代码底部)。这段代码运行得很好,我打算填充仪表板,而不会在IBM的python环境中收到任何错误。我想我得调整一些东西

代码语言:javascript
复制
   # TASK 3 - UPDATE LAYOUT COMPONENETS
# html.H1 tag for title  , style, and overall font size
# html.Div  & dcc.Input() tag to set inputs of the dashboard
# Update output componenent 2nd html.Div to layout the graph dcc.Graph()
app.layout = html.Div(children=[html.H1('Airline Performance Dashboard',
                                        style={'textAlign': 'center', 'color': '#503D36',
                                               'font-size': 40}),
                                html.Div(["Input Year: ", dcc.Input(id='input-year', value='2010',
                                                                    type='number',
                                                                    style={'height': '50px', 'font-size': 35}), ],
                                         style={'font-size': 40}),
                                html.Br(),
                                html.Br(),
                                html.Div(dcc.Graph(id='line-plot')),
                                ])

下面是代码的其余部分,

代码语言:javascript
复制
# TASK 4 - ADD APPLICATION CALL BACK FUNCTION and outputs / inputs
# add callback decorator
@app.callback(Output(component_id='line-plot', component_property='figure'),
              Input(component_id='input-year', component_property='value'))
# Add computation to callback function and return graph
def get_graph(entered_year):
    # Select 2019 data
    df = airline_data[airline_data['Year'] == int(entered_year)]

    # Group the data by Month and compute average over arrival delay time.
    line_data = df.groupby('Month')['ArrDelay'].mean().reset_index()
    # TASK 5 - UPDATE CALL BACK FUNCTION go.Figure(data=) and update fig.update_layout()
    fig = go.Figure(
        data=go.Scatter(x=line_data['Month'], y=line_data['ArrDelay'], mode='lines', marker=dict(color='green')))
    fig.update_layout(title='Month vs Average Flight Delay Time', xaxis_title='Month', yaxis_title='ArrDelay')
    return fig


# Run the app
if __name__ == '__main__':
    app.run_server()

可以肯定地说,我需要一个成年人。

EN

回答 1

Stack Overflow用户

发布于 2021-07-15 15:57:36

  • 已经添加了导入和数据帧模拟
  • 所有其他代码都可以在plotly 5.1.0

上正常运行

代码语言:javascript
复制
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.graph_objects as go
from jupyter_dash import JupyterDash
import numpy as np

app = JupyterDash(__name__)

# simulate data...
dr = pd.date_range("1-jan-2010", freq="W", periods=200)
airline_data = pd.DataFrame({"Year":dr.year, "Month":dr.month, "ArrDelay":np.random.uniform(2,5,len(dr))})

# TASK 3 - UPDATE LAYOUT COMPONENETS
# html.H1 tag for title  , style, and overall font size
# html.Div  & dcc.Input() tag to set inputs of the dashboard
# Update output componenent 2nd html.Div to layout the graph dcc.Graph()
app.layout = html.Div(children=[html.H1('Airline Performance Dashboard',
                                        style={'textAlign': 'center', 'color': '#503D36',
                                               'font-size': 40}),
                                html.Div(["Input Year: ", dcc.Input(id='input-year', value='2010',
                                                                    type='number',
                                                                    style={'height': '50px', 'font-size': 35}), ],
                                         style={'font-size': 40}),
                                html.Br(),
                                html.Br(),
                                html.Div(dcc.Graph(id='line-plot')),
                                ])

# TASK 4 - ADD APPLICATION CALL BACK FUNCTION and outputs / inputs
# add callback decorator
@app.callback(Output(component_id='line-plot', component_property='figure'),
              Input(component_id='input-year', component_property='value'))
# Add computation to callback function and return graph
def get_graph(entered_year):
    # Select 2019 data
    df = airline_data[airline_data['Year'] == int(entered_year)]

    # Group the data by Month and compute average over arrival delay time.
    line_data = df.groupby('Month')['ArrDelay'].mean().reset_index()
    # TASK 5 - UPDATE CALL BACK FUNCTION go.Figure(data=) and update fig.update_layout()
    fig = go.Figure(
        data=go.Scatter(x=line_data['Month'], y=line_data['ArrDelay'], mode='lines', marker=dict(color='green')))
    fig.update_layout(title='Month vs Average Flight Delay Time', xaxis_title='Month', yaxis_title='ArrDelay')
    return fig


app.run_server(mode="inline")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68381965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档