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



我的侧边栏没有用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中提取的udected数据表值。

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', 'DESKTOPSQLEXPRESS', '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()

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

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

最新更新