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

How to easily generate dynamic content #40

Open
cwiese opened this issue Dec 4, 2021 · 5 comments
Open

How to easily generate dynamic content #40

cwiese opened this issue Dec 4, 2021 · 5 comments

Comments

@cwiese
Copy link

cwiese commented Dec 4, 2021

I have a Dict in the model (that can change)

Stipple.@kwdef mutable struct Model <: ReactiveModel
  params::R{Dict} = Dict{String, Any}("T_max" => 310.0, "n_segs" => "10", "l_tube" => 10.0, "T_ambient_res" => "Ambient Temperature is 100.0K")
end 

in the UI - would like to create many textfield ..and rerender when model changes

[cell(class="q-pa-sm", textfield("", Symbol(""params['$k']"""), label="$k")) for (k,v) in @bind(:params)]  

Obviously @Bind(:params)] does not work.

I was hoping not to create a giant HTML string in the model - but rather use this like a template.

Any ideas

Chuck

@cwiese cwiese changed the title How to easily generate dynamic cpontent How to easily generate dynamic content Dec 4, 2021
@hhaensel
Copy link
Member

hhaensel commented Nov 8, 2023

With the latest changes of Stipple, this has become incredibly easy:

@app begin
    @in params = Dict{String, Any}("T_max" => 310.0, "n_segs" => "10", "l_tube" => 10.0, "T_ambient_res" => "Ambient Temperature is 100.0K")
end

function ui()
    [
        card(class = "q-pa-md",
            textfield(class = "q-ma-md", :k, R"params[k]", @for((v, k) in :params))
        )
    ]
end

@page("/", ui)
up()

image

EDIT:

  • It would have been possible already with earlier versions, but wouldn't have looked so nice. textfield(class = "q-ma-md", :k, R"params[k]", @recur("(v, k) in params"))
  • The above solution loops through all fields in the model field params. So if you add an entry, this will be reflected in the UI. With the latest changes you can also easily loop over static dicts or vectors
static_params = Dict{String, Any}("T_max" => 310.0, "n_segs" => "10", "l_tube" => 10.0, "T_ambient_res" => "T is 100.0K")
param_keys = keys(static_params)

textfield(:k, R"params[k]", @for((v, k) in static_params))
# "<q-input v-for=\"(v, k) in {'T_max':310.0,'n_segs':'10','l_tube':10.0,'T_ambient_res':'T is 100.0K'}\" :label=\"k\" v-model=\"params[k]\"></q-input>"
# note the inverted order of `(v, k)` in javascript compared with Julia syntax

textfield(:k, R"params[k]", @for(k in param_keys))
"<q-input v-for=\"k in [:a, :c]\" :label=\"k\" v-model=\"params(k)\"></q-input>"
# "<q-input v-for=\"(v, k) in ['T_max', 'n_segs', 'l_tube', 'T_ambient_res']\" :label=\"k\" v-model=\"params[k]\"></q-input>"

@essenciary
Copy link
Member

Beautiful!

@hhaensel
Copy link
Member

hhaensel commented Nov 8, 2023

I'm happy that you like the new syntax, although you were hesitant in the beginning. It took a while to find out how to best implement Julia expressions but now we are well set, I think.

I particularly like the automatic expansion of keys.

@hhaensel
Copy link
Member

hhaensel commented Nov 8, 2023

@cwiese Hope this is still helpful for you. You can close the issue if you are ok with the answer.
@essenciary We should document these kinds of use cases.

@hhaensel
Copy link
Member

We could close this, right?
@PGimenez Do we want to place this example in the docs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants