首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >页面的dash侧边栏的回调不起作用

问页面的dash侧边栏的回调不起作用
EN

Stack Overflow用户
提问于 2020-03-21 15:25:28
回答 1查看 824关注 0票数 0

我的侧边栏没有使用sql中的值回调和更新表。我希望我的页面中的表是刷新的,app1.py上的datatable是死的,并且没有用新值更新,有什么建议吗?我相信我的应用程序/app1.py没有问题

index.py:

代码语言:javascript
复制
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获取更新的DataTable值。

apps\app1.py

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2020-03-24 09:57:09

我不确定如果布局中没有ID为'graph-update'的组件,应用程序是如何运行的,但这就是缺失的地方。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60785424

复制
相关文章

相似问题

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