From e100c0f437f4ca489fded06f8fa206ba885aa8b8 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Wed, 7 Jun 2023 19:33:54 +0200 Subject: [PATCH 1/6] add index by id --- src/app/dashapp.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/app/dashapp.jl b/src/app/dashapp.jl index 4e687a3..fccae83 100644 --- a/src/app/dashapp.jl +++ b/src/app/dashapp.jl @@ -40,6 +40,24 @@ mutable struct DashApp end +const VecChildTypes = Union{NTuple{N, DashBase.Component} where {N}, Vector{<:DashBase.Component}} +function Base.getindex(app::Dash.DashApp, id::AbstractString) + app.layout[id] +end +function Base.getindex(component::DashBase.Component, id::AbstractString) + component.id == id && return component + hasproperty(component, :children) || return nothing + cc = component.children + cc isa Union{VecChildTypes, DashBase.Component} ? cc[id] : nothing +end +function Base.getindex(children::VecChildTypes, id::AbstractString) + for element in children + element.id == id && return element + el = element[id] + el !== nothing && return el + end +end + #only name, index_string and layout are available to set function Base.setproperty!(app::DashApp, property::Symbol, value) property == :index_string && return set_index_string!(app, value) From 13bb2068bcba78a46805e25d2a17a7044a0f26ed Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Wed, 7 Jun 2023 21:00:39 +0200 Subject: [PATCH 2/6] add test, handle Vector{Any} --- src/app/dashapp.jl | 10 +++++++++- test/core.jl | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/app/dashapp.jl b/src/app/dashapp.jl index fccae83..b4e73b8 100644 --- a/src/app/dashapp.jl +++ b/src/app/dashapp.jl @@ -48,7 +48,7 @@ function Base.getindex(component::DashBase.Component, id::AbstractString) component.id == id && return component hasproperty(component, :children) || return nothing cc = component.children - cc isa Union{VecChildTypes, DashBase.Component} ? cc[id] : nothing + cc isa Union{VecChildTypes, DashBase.Component, Vector{Any}} ? cc[id] : nothing end function Base.getindex(children::VecChildTypes, id::AbstractString) for element in children @@ -57,6 +57,14 @@ function Base.getindex(children::VecChildTypes, id::AbstractString) el !== nothing && return el end end +function Base.getindex(children::AbstractVector, id::AbstractString) + for element in children + hasproperty(element, :id) || continue + element.id == id && return element + el = element[id] + el !== nothing && return el + end +end #only name, index_string and layout are available to set function Base.setproperty!(app::DashApp, property::Symbol, value) diff --git a/test/core.jl b/test/core.jl index 25c942e..e0d5220 100644 --- a/test/core.jl +++ b/test/core.jl @@ -200,3 +200,17 @@ end end @test_throws ErrorException make_handler(app) end + +@testset "Index by id" begin + app = dash() + app.layout = html_div() do + dcc_input(id = "my-id", value="initial value", type = "text"), + html_div(id = "my-div", children = [ + html_div(), + "string", + html_div(id = "target") + ]), + html_div(id = "my-div2") + end + @test app["target"].id == "target" +end From 291f81cebafd092d371f352a03b54014d1bd255e Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Thu, 8 Jun 2023 11:03:02 +0200 Subject: [PATCH 3/6] remove type piracy --- src/app/dashapp.jl | 16 +++++++--------- test/core.jl | 1 + 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/app/dashapp.jl b/src/app/dashapp.jl index b4e73b8..778024a 100644 --- a/src/app/dashapp.jl +++ b/src/app/dashapp.jl @@ -48,7 +48,13 @@ function Base.getindex(component::DashBase.Component, id::AbstractString) component.id == id && return component hasproperty(component, :children) || return nothing cc = component.children - cc isa Union{VecChildTypes, DashBase.Component, Vector{Any}} ? cc[id] : nothing + return if cc isa Union{VecChildTypes, DashBase.Component} + cc[id] + elseif cc isa AbstractVector + identity.(filter(x->hasproperty(x, :id), cc))[id] + else + nothing + end end function Base.getindex(children::VecChildTypes, id::AbstractString) for element in children @@ -57,14 +63,6 @@ function Base.getindex(children::VecChildTypes, id::AbstractString) el !== nothing && return el end end -function Base.getindex(children::AbstractVector, id::AbstractString) - for element in children - hasproperty(element, :id) || continue - element.id == id && return element - el = element[id] - el !== nothing && return el - end -end #only name, index_string and layout are available to set function Base.setproperty!(app::DashApp, property::Symbol, value) diff --git a/test/core.jl b/test/core.jl index e0d5220..be0befb 100644 --- a/test/core.jl +++ b/test/core.jl @@ -213,4 +213,5 @@ end html_div(id = "my-div2") end @test app["target"].id == "target" + @test app["ups"] === nothing end From ca9e9508c45fa7209ae833f1cb33dfaf8239f700 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 9 Jun 2023 08:14:22 +0200 Subject: [PATCH 4/6] Update label.yml --- .github/workflows/label.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/label.yml b/.github/workflows/label.yml index c01c356..251a28c 100644 --- a/.github/workflows/label.yml +++ b/.github/workflows/label.yml @@ -7,6 +7,6 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/labeler@v2 + - uses: actions/labeler@v4 with: repo-token: "${{ secrets.GITHUB_TOKEN }}" From dd6bd5227611dda23d9fd44203735ccc35877ceb Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Tue, 11 Jul 2023 21:18:41 +0200 Subject: [PATCH 5/6] Remove getindex in app --- src/app/dashapp.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/app/dashapp.jl b/src/app/dashapp.jl index 778024a..92fccce 100644 --- a/src/app/dashapp.jl +++ b/src/app/dashapp.jl @@ -41,9 +41,7 @@ mutable struct DashApp end const VecChildTypes = Union{NTuple{N, DashBase.Component} where {N}, Vector{<:DashBase.Component}} -function Base.getindex(app::Dash.DashApp, id::AbstractString) - app.layout[id] -end + function Base.getindex(component::DashBase.Component, id::AbstractString) component.id == id && return component hasproperty(component, :children) || return nothing From e1854143770ec78e78a387521f62c4e64d985c2a Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Tue, 11 Jul 2023 21:19:55 +0200 Subject: [PATCH 6/6] Update tests --- test/core.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/core.jl b/test/core.jl index be0befb..fd9f030 100644 --- a/test/core.jl +++ b/test/core.jl @@ -212,6 +212,6 @@ end ]), html_div(id = "my-div2") end - @test app["target"].id == "target" - @test app["ups"] === nothing + @test app.layout["target"].id == "target" + @test app.layout["ups"] === nothing end