Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Allow for persistence in Cytoscape component. #121

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions demos/usage-persistence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
An example to show how to use persistence. After loading data, refreshing
the page should not loose the cytoscape data nor the selection of nodes/edges.
Persistence should be set to:
- local
- session
- memory
Read more about persistence here: https://dash.plotly.com/persistence
"""
import dash
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
import dash_cytoscape as cyto
import dash_html_components as html


elements = [
{'data': {'id': 'one', 'label': 'Node 1'}, 'position': {'x': 50, 'y': 50}},
{'data': {'id': 'two', 'label': 'Node 2'}, 'position': {'x': 200, 'y': 200}},
{'data': {'source': 'one', 'target': 'two', 'label': 'Node 1 to 2'}}
]

layout = {'name': 'grid'}

app = dash.Dash(__name__)
app.layout = html.Div([
html.Button('Load data', id='bt-load'),
cyto.Cytoscape(
id='cytoscape',
layout=layout,
# persistence=True
),
])

@app.callback(
Output('cytoscape', 'elements'),
[Input('bt-load', 'n_clicks')]
)
def load_elements(_):
ctx = dash.callback_context

if not ctx.triggered:
raise PreventUpdate
return elements

if __name__ == '__main__':
app.run_server(debug=True)