Skip to content

Pattern-match callbacks #47

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 3 commits into from
Jul 16, 2020
Merged

Pattern-match callbacks #47

merged 3 commits into from
Jul 16, 2020

Conversation

waralex
Copy link
Collaborator

@waralex waralex commented Jul 15, 2020

Pattern-match callbacks.

The functionality is the same as in Python, with a single nuance - NamedTuple is used as the id:

callback!(app,
    Output((type = "output", index = MATCH), "children"),
    Input((type = "dropdown", index = MATCH), "value"),
    State((type = "dropdown", index = MATCH), "id"),
) do value, id
    return html_div("Dropdown $(id.index) = $(value)")
end

@waralex waralex requested a review from alexcjohnson July 15, 2020 14:05
@waralex waralex linked an issue Jul 15, 2020 that may be closed by this pull request
@github-actions github-actions bot added enhancement New feature or request tests labels Jul 15, 2020
end

#The regular unique works using Set and will not work correctly, because in our case "equal" dependencies have different hashes
#This implementation is not algorithmically efficient (O(n^2)), but it is quite suitable for checking the uniqueness of outputs
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, that should be fine - this only happens when starting the server anyway, we only really care about efficiency during callback requests. Readability and simplicity are more important here.

@@ -37,8 +37,8 @@ using Dash
response = HTTP.handle(handler, request)
@test response.status == 200
resp_obj = JSON2.read(String(response.body))
@test !in(:multi, keys(resp_obj))
@test resp_obj.response.props.children == "test"
@test in(:multi, keys(resp_obj))
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI I see a few trailing whitepaces added in this PR (and a few others removed) - it's a little easier to review if we never have these, do you use an editor that can be set to automatically strip these on saving?

@alexcjohnson
Copy link
Contributor

Is the resource refactoring here solving a problem, or just simplifying the structure?

@waralex
Copy link
Collaborator Author

waralex commented Jul 15, 2020

@alexcjohnson

Is the resource refactoring here solving a problem, or just simplifying the structure?

Initially, I took resources from the R version, where they were grouped into folders. Now it turned out that this version of resources does not support pattern-matching, so I updated the resources to those used in Python dash 1.13.2. Python dash does not split them into directories, and it seemed to me that this is more convenient.

Copy link
Contributor

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

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

Beautiful. I don't know all the data type options in Julia, but I can't really imagine a simpler syntax than what you have here with NamedTuples. It looks like this is handled self-consistently, meaning that when you get a pattern-matching ID in callback_context it's also a NamedTuple? If I'm interpreting that right, then I'd say let's do it! 💃

@waralex
Copy link
Collaborator Author

waralex commented Jul 15, 2020

@alexcjohnson

It looks like this is handled self-consistently, meaning that when you get a pattern-matching ID in callback_context it's also a NamedTuple?

It depends on which context parameter we are talking about. inputs_list, states_list, and outputs_list- Yes, id is NamedTuple. In triggered and inputs and states dictionaries, the keys are like in Python-strings from json with sorted keys + .<property>. On the one hand, for consistency with the Python api, on the other, because in this form they come to changedProps from the front end:

triggered : [(prop_id = "{\"action\":\"done\",\"item\":3}.value", value =["done"])]
inputs:  Dict("{\"action\":\"done\",\"item\":3}.value" => ["done"])
inputs_list: [(id = (action = "done", item = 3), property = "value", value = ["done"])]
outputs_list: [(id = (item = 3,), property = "style")]

In principle, there is no big problem to make the keys also NamedTuple. Just keep in mind that the order of fields is important in NamedTuple, i.e. (a=1, b=2) != (b=2, a=1)

@alexcjohnson
Copy link
Contributor

On the one hand, for consistency with the Python api, on the other, because in this form they come to changedProps from the front end

Perfect. 💃 still applies.

@waralex waralex merged commit 0524f46 into dev Jul 16, 2020
@rpkyle rpkyle deleted the pattern_match branch July 22, 2020 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pattern matching callbacks
2 participants