xhluca / dash-draggable

react-draggable in Python
Other
19 stars 5 forks source link

dcc.Store not saving property values on refresh #8

Open SterlingButters opened 5 years ago

SterlingButters commented 5 years ago
...
dcc.Store(id='position-store', storage_type='session', data={'lastXs': [], 'lastYs': []}),
...
# Commit last known position to memory
@app.callback(
    Output('position-store', 'data'),
    [Input('draggable', 'lastX'),
     Input('draggable', 'lastY')],
    [State('position-store', 'data')]
)
def remember_position(lastX, lastY, data):
    data = data or {}
    x_history = data['lastXs'] or []
    y_history = data['lastYs'] or []
    if lastX is not None:
        x_history.append(lastX)
    if lastY is not None:
        y_history.append(lastY)

    data = {'lastXs': x_history, 'lastYs': y_history}
    return data

@app.callback(
    Output('draggable', 'position'),
    [Input('position-store', 'modified_timestamp')],
    [State('position-store', 'data')]
)
def remember_position(timestamp, data):
    if timestamp is None:
        raise PreventUpdate

    x = data.get('lastXs')
    y = data.get('lastYs')
    print(data)
    if len(x) > 0:
        print(x[-1], y[-1])
        return {'x': x[-1], 'y': y[-1]}
    else:
        return {'x': None, 'y': None}
...
SterlingButters commented 5 years ago

Should perhaps consider simplifying returned property from lastX, lastY, ... to moved: Boolean