Skip to content

Store callback Output value in persistence storage #3279

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

Merged
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
All notable changes to `dash` will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).


## [UNRELEASED]

## Fixed
- [#3279](https://github.com/plotly/dash/pull/3279) Fix an issue where persisted values were incorrectly pruned when updated via callback. Now, callback returned values are correctly stored in the persistence storage. Fix [#2678](https://github.com/plotly/dash/issues/2678)

## [3.0.4] - 2025-04-24

## Fixed
Expand Down
14 changes: 12 additions & 2 deletions dash/dash-renderer/src/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {once} from 'ramda';
import {once, path} from 'ramda';
import {createAction} from 'redux-actions';
import {addRequestedCallbacks} from './callbacks';
import {getAppState} from '../reducers/constants';
Expand All @@ -7,6 +7,7 @@ import cookie from 'cookie';
import {validateCallbacksToLayout} from './dependencies';
import {includeObservers, getLayoutCallbacks} from './dependencies_ts';
import {computePaths, getPath} from './paths';
import {recordUiEdit} from '../persistence';

export const onError = createAction(getAction('ON_ERROR'));
export const setAppLifecycle = createAction(getAction('SET_APP_LIFECYCLE'));
Expand All @@ -17,10 +18,19 @@ export const setHooks = createAction(getAction('SET_HOOKS'));
export const setLayout = createAction(getAction('SET_LAYOUT'));
export const setPaths = createAction(getAction('SET_PATHS'));
export const setRequestQueue = createAction(getAction('SET_REQUEST_QUEUE'));
export const updateProps = createAction(getAction('ON_PROP_CHANGE'));
export const insertComponent = createAction(getAction('INSERT_COMPONENT'));
export const removeComponent = createAction(getAction('REMOVE_COMPONENT'));

export const onPropChange = createAction(getAction('ON_PROP_CHANGE'));

export function updateProps(payload) {
return (dispatch, getState) => {
const component = path(payload.itempath, getState().layout);
recordUiEdit(component, payload.props, dispatch);
dispatch(onPropChange(payload));
};
}

export const addComponentToLayout = payload => (dispatch, getState) => {
const {paths} = getState();
dispatch(insertComponent(payload));
Expand Down
5 changes: 4 additions & 1 deletion dash/dash-renderer/src/observers/executedCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import {
pathOr
} from 'ramda';

import {ThunkDispatch} from 'redux-thunk';
import {AnyAction} from 'redux';

import {IStoreState} from '../store';

import {
Expand Down Expand Up @@ -63,7 +66,7 @@ const observer: IStoreObserverDefinition<IStoreState> = {
// In case the update contains whole components, see if any of
// those components have props to update to persist user edits.
const {props} = applyPersistence({props: updatedProps}, dispatch);
dispatch(
(dispatch as ThunkDispatch<any, any, AnyAction>)(
updateProps({
itempath,
props,
Expand Down
62 changes: 22 additions & 40 deletions dash/dash-renderer/src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,14 @@ export function recordUiEdit(layout, newProps, dispatch) {
persisted_props,
persistence_type
} = getProps(layout);
if (!canPersist || !persistence) {

// if the "persistence" property is changed as a callback output,
// skip the persistence storage overwriting.
const isPersistenceMismatch =
newProps?.persistence !== undefined &&
newProps.persistence !== persistence;

if (!canPersist || !persistence || isPersistenceMismatch) {
return;
}

Expand Down Expand Up @@ -501,46 +508,21 @@ export function prunePersistence(layout, newProps, dispatch) {
depersistedProps = mergeRight(props, update);
}

if (finalPersistence) {
if (finalPersistence && persistenceChanged) {
const finalStorage = getStore(finalPersistenceType, dispatch);

if (persistenceChanged) {
// apply new persistence
forEach(
persistedProp =>
modProp(
getValsKey(id, persistedProp, finalPersistence),
finalStorage,
element,
depersistedProps,
persistedProp,
update
),
filter(notInNewProps, finalPersistedProps)
);
}

// now the main point - clear any edit of a prop that changed
// note that this is independent of the new prop value.
const transforms = element.persistenceTransforms || {};
for (const propName in newProps) {
const propTransforms = transforms[propName];
if (propTransforms) {
for (const propPart in propTransforms) {
finalStorage.removeItem(
getValsKey(
id,
`${propName}.${propPart}`,
finalPersistence
)
);
}
} else {
finalStorage.removeItem(
getValsKey(id, propName, finalPersistence)
);
}
Comment on lines -525 to -542
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did this do? It's inclusion/removal doesn't seem to have an effect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with this code is that it clears the persisted value before the new selection is written via recordUiEdit, once the callback is triggered. For example, if the storage initially contains [[1, 2], []] and callback_select_all (which selects [1, 2, 3]) is triggered:

  1. The removed code clears the persistence storage prematurely which causes the original value (originalVal) to be lost, which is problematic.
  2. Then recordUiEdit writes [[1, 2, 3], [1, 2]] into storage.

This is incorrect because what should be persisted is [[1, 2, 3], []].

I might need to handle this differently. What do you think?

}
// apply new persistence
forEach(
persistedProp =>
modProp(
getValsKey(id, persistedProp, finalPersistence),
finalStorage,
element,
depersistedProps,
persistedProp,
update
),
filter(notInNewProps, finalPersistedProps)
);
}
return persistenceChanged ? mergeRight(newProps, update) : newProps;
}
5 changes: 0 additions & 5 deletions dash/dash-renderer/src/wrapper/DashWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {DashLayoutPath, UpdatePropsPayload} from '../types/component';
import {DashConfig} from '../config';
import {notifyObservers, onError, updateProps} from '../actions';
import {getWatchedKeys, stringifyId} from '../actions/dependencies';
import {recordUiEdit} from '../persistence';
import {
createElement,
getComponentLayout,
Expand Down Expand Up @@ -132,10 +131,6 @@ function DashWrapper({
const watchedKeys = getWatchedKeys(id, keys(changedProps), graphs);

batch(() => {
// setProps here is triggered by the UI - record these changes
// for persistence
recordUiEdit(renderComponent, newProps, dispatch);

// Only dispatch changes to Dash if a watched prop changed
if (watchedKeys.length) {
dispatch(
Expand Down