【问题标题】:callback for dash sidebar for page is not working页面破折号侧边栏的回调不起作用
【发布时间】:2023-03-18 09:28:01
【问题描述】:

我的侧边栏没有使用 sql 中的值回调和更新表。 我希望表格在我的页面中刷新 app1.py 上的数据表已经死了,没有用新值更新 有什么建议吗? 我相信我的应用程序/app1.py 没有任何问题

index.py:

import respective libraries
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
sidebar = html.Div(
    [
        html.H2("Sidebar", className="display-4"),
        html.Hr(),
        html.P(
            "A simple sidebar layout with navigation links", className="lead"
        ),
        dbc.Nav(
            [
                dbc.NavLink("Page 1", href="/app1", id="page-1-link"),
                dbc.NavLink("Page 2", href="/app2", id="page-2-link"),
            ],
            vertical=True,
            pills=True,
        ),
    ],
    style=SIDEBAR_STYLE,
)
content = html.Div(id="page-content", style=CONTENT_STYLE)
app.layout = html.Div([dcc.Location(id="url"), sidebar, content])
index_page = html.Div([
    html.H2(children='my-app-is-running', id='title'),
    html.Ul([
        html.Li(html.A('page-1', href='app1')),
        html.Li(html.A('page-2', href='app2')),
    ]),
])
# this callback uses the current pathname to set the active state of the
# corresponding nav link to true, allowing users to tell see page they are on
@app.callback([Output(f"page-{i}-link", "active") for i in range(1, 3)],    [Input("url", "pathname")],
)
def toggle_active_links(pathname):
    if pathname == "/":
        # Treat page 1 as the homepage / index
        return True, False, False
    return [pathname == f"/page-{i}" for i in range(1, 3)]
# Update the index
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def render_page_content(pathname):
    if pathname in ["/", "/page-1"]:
        return index_page
    elif pathname == "/app1":
        return app1.layout
if __name__ == '__main__':
    app.run_server(port=8888,debug=True)

这是链接到侧边栏的页面。但是,我无法从 sql 中获取更新后的数据表值。

应用程序\app1.py

import respective libraries
def connectSQLServer(driver, server, db):
    connSQLServer = pyodbc.connect(
        r'DRIVER={' + driver + '};'
        r'SERVER=' + server + ';'
        r'DATABASE=' + db + ';'
        r'Trusted_Connection=yes;',
        autocommit=True
    )
    return connSQLServer

def getData():
    df = pd.DataFrame()
    sql_conn = connectSQLServer(
        'ODBC Driver 13 for SQL Server', 'DESKTOP\SQLEXPRESS', 'display')
    cursor = sql_conn.cursor()
    d = {'x': [], 'y': [], 'z': []}
    sql = 'select * from dbo.data3'
    cursor.execute(sql)
    myresult = cursor.fetchall()
    for x in myresult:
        d['x'].append(x[0])
        d['y'].append(x[1])
        d['z'].append(x[2])
    data = pd.DataFrame(d)
    df = df.append(data, ignore_index=True)
    return df.to_dict('records')


tblcols = [{'name': 'position', 'id': 'x'},
           {'name': 'move', 'id': 'y'}, {'name': 'sync', 'id': 'z'}]

layout = html.Div(
    html.Div([
        html.H4('Dashboard'),
        dash_table.DataTable(
            id='table',
            data=getData(),
            editable=True,
            style_header=table_header_style,
            columns=tblcols),
            dcc.Interval(
            id='graph-update',
            interval=1*1000
        )
    ])
)
@app.callback(
    dash.dependencies.Output('table', 'data'),
    [dash.dependencies.Input('graph-update', 'n_intervals')])
def updateTable(n):
    return getData()

【问题讨论】:

  • 您似乎在使用多页应用程序。你能分享相关页面的所有代码吗?例如,您提到的表格不在此处。
  • @coralvanda,我已经更新了这个问题。谢谢!

标签: python plotly-dash


【解决方案1】:

我不确定在布局中没有 ID 为 'graph-update' 的组件的情况下,您是如何让应用程序运行的,但这就是缺少的。

我删除了 SQL 部分,因为我没有您的数据库,并放入了一个虚拟数据框。当我添加一个 ID 为 'graph-update'dcc.Interval 组件时,应用程序运行正常。

【讨论】:

  • 我已经更新了 dcc.Interval 并且它仍然没有获取和更新我的数据表):
  • 你能在你的回调中加入其他东西来确保它被触发吗? print('test') 例如。它对我来说只是将表格的一个单元格更新为一个随机数。如果您可以确认回调正常工作,那么我们可以开始查看下一部分。
猜你喜欢
  • 2018-01-25
  • 2019-04-20
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2021-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多