To initialize a DCC Graph that never needs to be re-plotted, you can use the dcc.Graph component with the static parameter set to True. This will create a graph that is rendered once and never updated again.
Here's an example of how you can use it:
code
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
app = dash.Dash()
app.layout = html.Div([
dcc.Graph(
id='my-graph',
static=True,
children=[
go.Scatter(
x=['Apple', 'Banana', 'Cherry'],
y=[1, 2, 3],
mode='lines',
name='Fruit'
)
]
)
])
@app.callback(
Output('my-graph', 'children'),
Input('my-graph', 'static')
)
def update_graph(static):
# This function will never be called since static is always True
return [go.Scatter(
x=['Apple', 'Banana', 'Cherry'],
y=[1, 2, 3],
mode='lines',
name='Fruit'
)]
if __name__ == '__main__':
app.run_server()
In this example, the dcc.Graph component is created with the static parameter set to True, which means that the graph will be rendered once and never updated again. The update_graph function is defined as a callback function that will be called whenever the static property of the dcc.Graph component changes. However, since static is always True, this function will never be called.
You can put any valid Plotly graph object as the children argument of the dcc.Graph component. In this example, a simple scatter plot is used, but you can replace it with any other type of Plotly graph, such as a bar chart, line chart, or even a dashboard with multiple charts.
Note that if you need to update the graph in response to user input or other changes, you can still use the dcc.Graph component with static=False and define a callback function to update the graph as needed. The static parameter only controls whether the graph is rendered once or updated dynamically.