justpy icon indicating copy to clipboard operation
justpy copied to clipboard

Teach add to work with iterables

Open TalAmuyal opened this issue 3 years ago • 3 comments

Hi, @elimintz, thanks for this project! I used JustPy in a hackathon a couple of weeks ago and I enjoy it very much :)

I'd like to suggest that add will support arguments that are iterables. I find it useful when generating things in a comprehension.

For example:

jp.Table().add(
    jp.Tr().add(
        ...
    ),
    [
        jp.Div(...)
        for user in users
    ],
)

What do you think?

TalAmuyal avatar Mar 19 '21 22:03 TalAmuyal

Hi Tal,

Thanks for using JustPy!

Please take a look at this example:

import justpy as jp

def unpack_test():
    wp = jp.WebPage()
    d = jp.Div(a=wp)
    d.add(*[jp.Div(text=f'Start Div {i}', classes='m-2') for i in range(1,11)], jp.Div(text='Middle div'), 
          *[jp.Div(text=f'End Div {i}', classes='m-2') for i in range(1,11)])
    return wp


jp.justpy(unpack_test)

I may be missing something but I think you can get the functionality you are looking for using the * unpacking operator. Let me know what you think.

elimintz avatar Mar 20 '21 14:03 elimintz

Indeed it will work, however I suggest adding support for the following version:

import justpy as jp

def unpack_test():
    wp = jp.WebPage()
    d = jp.Div(a=wp)
    d.add([jp.Div(text=f'Start Div {i}', classes='m-2') for i in range(1,11)], jp.Div(text='Middle div'), 
          [jp.Div(text=f'End Div {i}', classes='m-2') for i in range(1,11)])
    return wp

This could be also nice when a function returns a list of Divs, for example:

import justpy as jp

def unpack_test():
    wp = jp.WebPage()
    d = jp.Div(a=wp)
    d.add([jp.Div(text=f'Start Div {i}', classes='m-2') for i in range(1,11)], jp.Div(text='Middle div'), 
          make_bottom_part())
    return wp

def make_bottom_part():
    return [jp.Div(text=f'End Div {i}', classes='m-2') for i in range(1,11)]

If you don't want this feature, feel free to close this PR.

TalAmuyal avatar Mar 20 '21 19:03 TalAmuyal

Another example:

def build_page(content):
    wp = jp.WebPage()
    d = jp.Div(a=wp)
    d.add(content)
    return wp

Where content could be a jp.HTMLComponent or a list of such.

It looks much more helpful when you have an .add() function that has multiple static parts and somewhere in the middle it has something that could be either an element or a list of elements.

TalAmuyal avatar Mar 22 '21 16:03 TalAmuyal

Wouldn't it be cleaner to have to different calls add_component and add_components?

WolfgangFahl avatar Aug 20 '22 12:08 WolfgangFahl

@TalAmuyal i would have merged this and modify it to show your collaboration. Since you are already on the collaborator list i feel that other issues and cleanups have priority and we might regret the decision to support this in the process of a general cleanup. I like the idea of being able to handle iterables but wouldn't force it with an instance of check but a separate function which we might readd later.

WolfgangFahl avatar Aug 25 '22 12:08 WolfgangFahl

@WolfgangFahl , thanks for taking the time and investing in this project. I'm fine with it either way.

TalAmuyal avatar Aug 26 '22 21:08 TalAmuyal