页面破折号补充工具栏的回调不起作用

时间:2020-03-21 07:25:27

标签: python plotly-dash

我的侧边栏没有用sql的值回调和更新表。 我希望表格在我的页面中刷新 app1.py上的数据表已死并且未使用新值更新 有什么建议吗? 我相信我的apps / 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中获取udpated数据表值。

apps \ 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()

1 个答案:

答案 0 :(得分:0)

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

我修剪了SQL部分,因为我没有您的数据库,并放入了一个虚拟数据框。当我添加一个ID为dcc.Interval的{​​{1}}组件时,该应用正确运行。