我试图在应用程序运行时不显示默认的 dcc.graph.我只想在应用运行时显示我的图表
这是我的代码,
dbc.Row([dbc.Col([dcc.Graph(id='datatable-upload-graph', responsive=True, style={显示":阻止"})], xs=10, sm=8, md=5, lg=6, xl=5)])
@app.callback(输出('数据表上传图','图'),输入('容器数据表','数据'))def display_gantt(container_datatable):df = pd.DataFrame(container_datatable)df['开始日期'] = pd.to_datetime(df['开始日期'], errors='强制')df['End Date'] = pd.to_datetime(df['End Date'], errors='coerce')fig = px.timeline(df, x_start="开始日期", x_end="结束日期", y="项目名称", color="状态")fig.update_yaxes(autorange="reversed")如果 container_datatable 为无:返回 []别的:返回无花果app.config['suppress_callback_exceptions'] = True如果 __name__ == '__main__':app.run_server(debug=True, use_reloader=False)
只要确保不要未指定 dcc.Graph
的 figure
属性,而是像这样:
dcc.Graph(id='datatable-upload-graph', figure = blank_figure())
其中 blank_figure()
是一个图形,它不仅像默认版本一样为空,而且去除了所有可见特征.
在您的应用布局中,您已经设置了
使用 figure = blank_figure()
应用将如下所示:
当模拟结束时,应用程序将如下所示:
现在您可以轻松查看使用不同模板的图形的外观,例如 'plotly_dark'
:
只需在注释掉这两行之间切换,即可查看下面完整代码段中的效果.
dcc.Graph(id="graph", figure = blank_fig())# dcc.Graph(id="graph")
将 numpy 导入为 np将熊猫导入为 pd将 plotly.express 导入为 px导入 plotly.graph_objects导入破折号将 dash_core_components 导入为 dcc将 dash_html_components 导入为 html从 jupyter_dash 导入 JupyterDash从 dash.dependencies 导入输入、输出模板= ['plotly','seaborn','simple_white','ggplot2','plotly_white'、'plotly_dark'、'演示文稿'、'xgridoff'、'ygridoff','gridon','无']定义空白图():fig = go.Figure(go.Scatter(x=[], y = []))fig.update_layout(模板=无)fig.update_xaxes(showgrid = False,showticklabels = False,zeroline=False)fig.update_yaxes(showgrid = False,showticklabels = False,zeroline=False)返回无花果# startfig = blank_fig()# 破折号应用程序 = JupyterDash(__name__)app.layout = html.Div([dcc.RadioItems(id='template_radio',options=[{'label': k, 'value': k} for k in templates],值=模板[0]),html.Hr(),html.Div(id='display_templates'),dcc.Graph(id="graph", figure = blank_fig())# dcc.Graph(id="graph")])# 用选定的模板制作一个图形@app.callback(输出('graph', 'figure'),[输入('template_radio','value')])def make_graph(模板):np.random.seed(1)开始 = 2021ncols = 50nrows = 100cols = [str(i) for i in np.arange(start, start+ncols)]df = pd.DataFrame(np.random.randint(-2,3, (nrows,ncols)), columns = cols).cumsum()df.iloc[0] = 0# 数字fig = px.line(df, x=df.index, y=cols)fig.update_layout(模板 = 模板)返回无花果app.run_server(mode='inline', 端口 = 8070, dev_tools_ui=True,dev_tools_hot_reload =真,线程=真)
I am trying not the show the defult dcc.graph when the app runs. I just want to show my graph when app runs
Here is my code,
dbc.Row([
dbc.Col([
dcc.Graph(id='datatable-upload-graph', responsive=True, style={
'display': 'block'
})
], xs=10, sm=8, md=5, lg=6, xl=5)
])
@app.callback(
Output('datatable-upload-graph', 'figure'),
Input('container-datatable', 'data')
)
def display_gantt(container_datatable):
df = pd.DataFrame(container_datatable)
df['Start Date'] = pd.to_datetime(df['Start Date'], errors='coerce')
df['End Date'] = pd.to_datetime(df['End Date'], errors='coerce')
fig = px.timeline(df, x_start="Start Date", x_end="End Date", y="Project Name", color="Status")
fig.update_yaxes(autorange="reversed")
if container_datatable is None:
return []
else:
return fig
app.config['suppress_callback_exceptions'] = True
if __name__ == '__main__':
app.run_server(debug=True, use_reloader=False)
Just make sure to not leave the figure
attribute of dcc.Graph
unspecified, but rather, for example, like this:
dcc.Graph(id='datatable-upload-graph', figure = blank_figure())
Where blank_figure()
is a figure that is not only empty like in the default version, but stripped of all visible features.
In your app layout you've set up your dcc.Graph as:
dcc.Graph(id='datatable-upload-graph', responsive=True, style={
'display': 'block'
})
What you're missing here is a specification for the figure
attribute. Your app will work perfectly fine without it, but you will end up with that empty figure until you've managed to populate the figure object through one of your callbacks. And for longer loading times the empty figure will become visible.
But you can remedy this by specifying a completely blank figure like:
dcc.Graph(id='datatable-upload-graph', figure = blank_figure())
where blank_figure()
is this:
def blank_fig():
fig = go.Figure(go.Scatter(x=[], y = []))
fig.update_layout(template = None)
fig.update_xaxes(showgrid = False, showticklabels = False, zeroline=False)
fig.update_yaxes(showgrid = False, showticklabels = False, zeroline=False)
return fig
The code snippet below will let you test this with a random data sample. The app itself is pretty neat as well (in all modesty). Nothing too fancy, but it will let you check out some templates available for your figures through fig.update_layout(template = <template>)
Without including figure = blank_figure()
in dcc.Graph
, the app will look like this for a brief moment:
And with figure = blank_figure()
the app will look like this:
And when the simulations have come to an end the app will look like this:
And now you can easily take a look at how the figure will look like using the different templates, like 'plotly_dark'
:
Just switch between commenting out these two lines to see the effects in the complete snippet below.
dcc.Graph(id="graph", figure = blank_fig())
# dcc.Graph(id="graph")
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import dash
import dash_core_components as dcc
import dash_html_components as html
from jupyter_dash import JupyterDash
from dash.dependencies import Input, Output
templates = ['plotly', 'seaborn', 'simple_white', 'ggplot2',
'plotly_white', 'plotly_dark', 'presentation', 'xgridoff',
'ygridoff', 'gridon', 'none']
def blank_fig():
fig = go.Figure(go.Scatter(x=[], y = []))
fig.update_layout(template = None)
fig.update_xaxes(showgrid = False, showticklabels = False, zeroline=False)
fig.update_yaxes(showgrid = False, showticklabels = False, zeroline=False)
return fig
# startfig = blank_fig()
# Dash
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.RadioItems(
id='template_radio',
options=[{'label': k, 'value': k} for k in templates],
value=templates[0]
),
html.Hr(),
html.Div(id='display_templates'),
dcc.Graph(id="graph", figure = blank_fig())
# dcc.Graph(id="graph")
])
# Make a figure with selected template
@app.callback(Output('graph', 'figure'),
[Input('template_radio', 'value')])
def make_graph(template):
np.random.seed(1)
start = 2021
ncols = 50
nrows = 100
cols = [str(i) for i in np.arange(start, start+ncols)]
df = pd.DataFrame(np.random.randint(-2,3, (nrows,ncols)), columns = cols).cumsum()
df.iloc[0] = 0
# figure
fig = px.line(df, x=df.index, y=cols)
fig.update_layout(template = template)
return fig
app.run_server(mode='inline', port = 8070, dev_tools_ui=True,
dev_tools_hot_reload =True, threaded=True)
这篇关于如何不在 Dash 中显示默认的 dcc.graph 模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!