How to use pattern matching callbacks in Dash for Julia

Viewed 234

I'm new to julia, but not so new on Dash; I'm trying to build my first app with Dash for julia, but I can't seem to make a pattern matching callback work properly. Here's the part of the code that's giving me troubles:

callback!(
    app,
    Output((type= "filter_", index= ALL), "options"),
    Input("inputs", "data"),
    State((type= "filter_", index= ALL), "value"),
) do inputs, filters
    list_outs = []
    list_vals = []
    for i in 1:length(filters)
        push!(list_outs, [(label= input, value= input) for input in inputs])
    end
    return list_outs
end

What I'm trying to do here is to use the available inputs of the data set, already stored in "inputs", to set the filters' options, creating as many sets of options as there are dropdowns.

The problem here is, I guess, in the format of the output I'm returning: it says "Invalid number of output values for {"index":["ALL"],"type":"filter_"}.options. Expected 3, got 1"

Sadly, I found nothing of use about how to use pattern matching callbacks with julia; I tried passing the output both as an array and as a tuple, but to no avail.

Any help is welcomed, thank you all!

1 Answers

This is the error related to the fact that if the result is a single Output, the callback output is automatically represented as an array of what is returned for uniform further processing. I.e., in your case, as [list_outs]. The fact that the Output with the match pattern is also treated as a single one is my bug, I added the issue and try to fix it in the near future. Right now you can work around this problem by using Output as an array:

using Dash
using DashHtmlComponents
using DashCoreComponents
app = dash()

app.layout = html_div() do
    dcc_input(id = "input", value = "A,B,C"),
    dcc_dropdown(id = (type="filter_", index = 1)),
    dcc_dropdown(id = (type="filter_", index = 2)),
    dcc_dropdown(id = (type="filter_", index = 3)),
    dcc_dropdown(id = (type="filter_", index = 4))
end
callback!(
    app,
    [Output((type= "filter_", index= ALL), "options")], #This is multiple output in explicitly form
    Input("input", "value"),
    State((type= "filter_", index= ALL), "value"),
) do input, filters
    inputs = split(input, ",")
    list_outs = []
    list_vals = []
    for i in 1:length(filters)
        push!(list_outs, [(label= input, value= input) for input in inputs])
    end
    return [list_outs] # Accordingly, we return the result inside an additional array
end

run_server(app, debug = true)
Related