目标:如果发生条件(即 URL 路径名中没有数据),我想弹出一个警报组件 否则渲染应用程序
到目前为止我尝试过的事情
app.layout = dbc.Container([
dbc.Alert([...], id='alert', is_open=False),
dbc.Container([rows & columns (all visuals are here)], id='page1')
])
@app.callback(
[Output('alert', 'is_open'),
Output('store-component', 'data'),
Output('page1', 'children')],
Input('url', 'pathname')
)
def extract_path_from_url(pathname):
if pathname name contains ... :
store the dataset in the Store component and **DONT SHOW THE ALERT BUTTON**
else:
**SHOW THE ALERT BUTTON** and that's it
... other callbacks generating the visuals based on the store-component ...
这不起作用,有没有正确的方法来实现这一目标?
编辑: 最新尝试:
这是我更新的回调和布局。到目前为止,当条件为 True 时,它工作正常,警报组件不会显示,并且一切都显示正常。问题是,当条件为假时,警报消息显示成功,但我们可以看到图表是空的。我想隐藏它们或者根本不渲染它们...
app.layout = dbc.Container([
dbc.Alert([...], id='alert', is_open=False),
dbc.Container([rows & columns (all visuals are here)], id='container')
])
@app.callback(
[Output('alert', 'is_open'),
Output('store-component', 'data'),
Output('container', 'style')],
Input('url', 'pathname')
)
def extract_path_from_url(pathname):
if pathname name contains ... :
return dash.no_update, data, dash.no_update
else:
return True, dash.no_update, {'display': 'none'}
是的,您可以创建它。只需简单地写下条件即可,例如(最高价格交叉 收盘价)。写完这篇文章后,当高价超过单支蜡烛的收盘价时,您将获得完美的警报时间。
dbc
“警告”警报的 URL 条件显示from dash import Dash, Input, Output
from dash import dcc, html
import dash_bootstrap_components as dbc
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
# Sample layout
app.layout = dbc.Container(
[
dcc.Location(id="url", refresh=False),
dbc.Alert(
[
html.H3("Warning!"),
html.Hr(),
html.P("No data found for this URL."),
],
id="alert",
is_open=False,
color="warning",
),
dbc.Container(
[
# Mock "rows & columns (all visuals are here)"
html.H1("Main Content"),
html.Div(
"This is the main content that"
" will be hidden or shown based on the URL."
),
dcc.Graph(
id="example-graph",
figure={
"data": [
{
"x": [1, 2, 3],
"y": [4, 1, 2],
"type": "bar",
"name": "Sample Data",
},
],
"layout": {"title": "Sample Data Visualization"},
},
),
],
id="container",
),
],
style={"margin": "10%"},
)
@app.callback(
[Output("alert", "is_open"), Output("container", "style")],
Input("url", "pathname"),
)
def extract_path_from_url(pathname):
# For demonstration, if pathname contains "nodata", assume no data is found
if "nodata" in pathname:
# Show the alert and hide the main content
return True, {"display": "none"}
else:
# Hide the alert and show the main content
return False, {"display": "block"}
if __name__ == "__main__":
app.run_server(debug=True)
产生: