-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expose setprops #2758
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
Changes from 20 commits
7eb975d
3c5bd45
8b17321
6b2f1de
647ee9f
78ccd87
05446a9
be4ac5a
96c1115
fc6c200
3dbf133
9f5ac40
1faf5e6
ca5eaf8
099e79a
2a0c9ab
7c11209
26bb305
107ea9f
1c8773a
daea03d
f9734a5
5bbfc4d
46c6d2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {updateProps, notifyObservers} from '../actions/index'; | ||
import {getPath} from '../actions/paths'; | ||
|
||
const set_props = (updates: []) => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const ds = (window.dash_stores = window.dash_stores || []); | ||
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 | ||
updates.forEach(({id, ...props}) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the api for this could be simpler without the batching of updates and extracting the id from the props into a function argument.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, unless we would want to be able to send batches of updates and not trigger the renderer until the props have been updated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For batching to work it needs a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, for this, it would be in the context of JS, however, I thought with the function coming from within the React lifecycle, this would still be a react response? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inside a clientside callback there is a react context, but since this is exposed globally it may be called from event handlers or anywhere really. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, yeah, I can change it, was just thinking in general how the callbacks work with being able to pass multiple at the same time. |
||
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; |
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( | ||
""" | ||
() => { | ||
window.dash_clientside.set_props(""" | ||
+ json.dumps(ids) | ||
+ """) | ||
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") |
There was a problem hiding this comment.
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-placedas any
or some such? @T4rk1n you're better at this than I am, can you comment?