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

Expose setprops #2758

Merged
merged 24 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7eb975d
exposing `clientSide_setProps` by utilizing an observer
BSd3v Feb 13, 2024
3c5bd45
Merge branch 'plotly:dev' into expose-setprops
BSd3v Feb 14, 2024
8b17321
adding changelog and contributing entries
BSd3v Feb 14, 2024
6b2f1de
adding changelog entry
BSd3v Feb 14, 2024
647ee9f
updating naming convention to `setProps`
BSd3v Feb 14, 2024
78ccd87
migrating `setProps` to be a util that gets imported and then created…
BSd3v Feb 14, 2024
05446a9
adjusting `setProps` to use a `window.dash_stores` array which builds…
BSd3v Feb 14, 2024
be4ac5a
attempt to fail `install dependencies`
BSd3v Feb 14, 2024
96c1115
adding ignore statements for the `window` object
BSd3v Feb 14, 2024
fc6c200
adjusting test for `dash_renderer.min.js` to be from script
BSd3v Feb 14, 2024
3dbf133
failing on purpose
BSd3v Feb 14, 2024
9f5ac40
adding ignore for `window` object again
BSd3v Feb 14, 2024
1faf5e6
adjusting `setProps.ts` -> `clientsideFunctions.ts` and adding test f…
BSd3v Feb 15, 2024
ca5eaf8
fixing for lint
BSd3v Feb 15, 2024
099e79a
fixing for lint
BSd3v Feb 15, 2024
2a0c9ab
fixing missing import
BSd3v Feb 15, 2024
7c11209
fixing for lint
BSd3v Feb 15, 2024
26bb305
testing to make sure the store doesnt exist yet
BSd3v Feb 15, 2024
107ea9f
fixing tyop and adjusting `setProps` -> `set_props`
BSd3v Feb 15, 2024
1c8773a
Merge branch 'dev' into expose-setprops
T4rk1n Feb 16, 2024
daea03d
making `set_props` work with just a single component at a time
BSd3v Feb 21, 2024
f9734a5
removing eslint/ts-ignore comments
BSd3v Feb 27, 2024
5bbfc4d
Merge branch 'dev' into expose-setprops
BSd3v Feb 27, 2024
46c6d2b
fixing for lint
BSd3v Feb 27, 2024
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 @@ -7,12 +7,18 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## Fixed

- [#2756](https://github.com/plotly/dash/pull/2756) Prevent false dangerous link warning. Fixes [#2743](https://github.com/plotly/dash/issues/2743)
- [#2752](https://github.com/plotly/dash/pull/2752) Fixed issue with Windows build, for first time build on Windows, the dev needs to use `npm run first-build`

## Changed

- [#2734](https://github.com/plotly/dash/pull/2734) Configure CI for Python 3.10 [#1863](https://github.com/plotly/dash/issues/1863)
- [#2735](https://github.com/plotly/dash/pull/2735) Configure CI for Python 3.8 and 3.12, drop support for Python 3.6 and Python 3.7 [#2736](https://github.com/plotly/dash/issues/2736)

## Added

- [#2758](https://github.com/plotly/dash/pull/2758)
- exposing `setProps` to `dash_clientside.clientSide_setProps` to allow for JS code to interact directly with the dash eco-system

## [2.15.0] - 2024-01-31

## Added
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ $ npm ci
#
$ npm run build # runs `renderer build` and `npm build` in dcc, html, table
# build and install components used in tests
# on windows, the developer will need to use `npm run first-build` this performs additional first steps
#
# Alternatively one could run part of the build process e.g.
$ dash-update-components "dash-core-components"
Expand Down
6 changes: 6 additions & 0 deletions dash/dash-renderer/renderer-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
file=./build/dash_renderer.min.js
if [ ! -f "$file" ]; then
echo "dash-renderer did not build correctly"
exit 1
fi
1 change: 1 addition & 0 deletions dash/dash-renderer/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {DashRenderer} from './DashRenderer';
import './utils/clientsideFunctions';

// make DashRenderer globally available
window.DashRenderer = DashRenderer;
6 changes: 6 additions & 0 deletions dash/dash-renderer/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export default class RendererStore {
private createAppStore = (reducer: any, middleware: any) => {
this.__store = createStore(reducer, middleware);
this.storeObserver.setStore(this.__store);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const ds = (window.dash_stores = window.dash_stores || []);
if (!ds.includes(this.__store)) {
ds.push(this.__store);
}
this.setObservers();
};

Expand Down
27 changes: 27 additions & 0 deletions dash/dash-renderer/src/utils/clientsideFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {updateProps, notifyObservers} from '../actions/index';
import {getPath} from '../actions/paths';

const set_props = (id: string | object, props: {[k: string]: any}) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const ds = (window.dash_stores = window.dash_stores || []);
Copy link
Collaborator

Choose a reason for hiding this comment

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

My TypeScript skills are pretty basic, but can't we avoid these comments (3 in this file and store.ts) with a few well-placed as any or some such? @T4rk1n you're better at this than I am, can you comment?

for (let y = 0; y < ds.length; y++) {
const {dispatch, getState} = ds[y];
const {paths} = getState();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const componentPath = getPath(paths, id);
dispatch(
updateProps({
props,
itempath: componentPath
})
);
dispatch(notifyObservers({id, props}));
}
};

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const dc = (window.dash_clientside = window.dash_clientside || {});
dc['set_props'] = set_props;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"private::initialize.renderer": "cd dash/dash-renderer && npm ci",
"private::cibuild.components": "python dash/development/update_components.py 'all' --ci True",
"private::build.components": "python dash/development/update_components.py 'all'",
"private::cibuild.renderer": "cd dash/dash-renderer && renderer build",
"private::build.renderer": "cd dash/dash-renderer && renderer build",
"private::cibuild.renderer": "cd dash/dash-renderer && rimraf build/dash_renderer.min.js && renderer build && sh renderer-test.sh",
"private::build.renderer": "cd dash/dash-renderer && rimraf build/dash_renderer.min.js && renderer build && sh renderer-test.sh",
"private::build.jupyterlab": "cd @plotly/dash-jupyterlab && jlpm install && jlpm build:pack",
"private::lint.black": "black dash tests --exclude metadata_test.py --check",
"private::lint.flake8": "flake8 --exclude=metadata_test.py dash tests",
Expand Down
62 changes: 62 additions & 0 deletions tests/integration/clientside/test_clientside_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from dash import Dash, html, Input, Output, no_update, State
import json
from multiprocessing import Value


def test_sp001_clientside_setprops(dash_duo):

call_count = Value("i", 0)

app = Dash(__name__)

ids = [
{"id": {"index": "1", "type": "test"}, "children": ["rawr"]},
{"id": "two", "children": "this is a test"},
{"id": "three", "children": "i see trees of green"},
]

app.layout = html.Div(
[
*[html.Div(id=x["id"]) for x in ids],
html.Div(id="four"),
html.Button(id="setup", children="test setprops"),
]
)

app.clientside_callback(
"""
() => {
"""
+ json.dumps(ids)
+ """.forEach(({id, ...props}) => window.dash_clientside.set_props(id, props))
return window.dash_clientside.no_update
}
""",
Output("setup", "id"),
Input("setup", "n_clicks"),
prevent_initial_call=True,
)

for x in ids:

@app.callback(
Output(x["id"], "id", allow_duplicate=True),
Output("four", "children", allow_duplicate=True),
Input(x["id"], "children"),
State(x["id"], "id"),
prevent_initial_call=True,
)
def prinout(c, id):
call_count.value += 1
for y in ids:
if y["id"] == id:
assert y["children"] == c
return no_update, call_count.value

dash_duo.start_server(app)

dash_duo.wait_for_text_to_equal("#setup", "test setprops")
dash_duo.find_element("#setup").click()
dash_duo.wait_for_text_to_equal("#two", "this is a test")
dash_duo.wait_for_text_to_equal("#three", "i see trees of green")
dash_duo.wait_for_text_to_equal("#four", "3")