Skip to content

Add @with_children decorator #113

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
May 30, 2025
Merged

Add @with_children decorator #113

merged 3 commits into from
May 30, 2025

Conversation

jodal
Copy link
Contributor

@jodal jodal commented May 3, 2025

This adds the @with_children decorator as discussed in #98.

I've kept the implementation in a file of its own, but imported it into the htpy namespace for easy usage.

The docs have not been updated, as I wanted to get this out there for discussion and tweaks first.

TODO

  • Tests
  • Implementation
  • Documentation
  • Changelog

@jodal jodal force-pushed the with-children branch 3 times, most recently from b1045ad to 552c90b Compare May 3, 2025 09:08
@jodal jodal changed the title Add @with_children decorator Add @with_children decorator May 3, 2025
@jodal
Copy link
Contributor Author

jodal commented May 3, 2025

I went ahead and added a suggestion for a new section in the docs, so we can get the disussion on that part rolling as well.

@jodal jodal force-pushed the with-children branch from e7d8469 to a9d151c Compare May 4, 2025 09:02
@jodal
Copy link
Contributor Author

jodal commented May 14, 2025

Have you had the chance to have a look at this?

Copy link
Owner

@pelme pelme left a comment

Choose a reason for hiding this comment

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

Hi @jodal , thanks for creating this PR! I ;eft some small comments. I think it looks good overall. I think it is a good idea that with_children has its own file.

Would be happy if another @with_component user had a look too :) /cc @bessey @davepeck

@jodal jodal force-pushed the with-children branch 3 times, most recently from 2debf78 to 3b9548f Compare May 14, 2025 20:59
@jodal jodal requested a review from pelme May 14, 2025 21:01
@jodal
Copy link
Contributor Author

jodal commented May 25, 2025

Rebased on top of 25.5.0.

Copy link
Owner

@pelme pelme left a comment

Choose a reason for hiding this comment

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

This looks good to me but would be happy if another @with_children user reviewed it too :)

@jodal
Copy link
Contributor Author

jodal commented May 25, 2025

Thanks for having a second look!

I know that @davepeck has this on his list 😇

@jodal jodal closed this May 26, 2025
@jodal jodal deleted the with-children branch May 26, 2025 21:28
@jodal jodal reopened this May 26, 2025
@jodal jodal force-pushed the with-children branch 2 times, most recently from 49563d9 to 29cafc1 Compare May 26, 2025 21:34
@jodal
Copy link
Contributor Author

jodal commented May 26, 2025

Rebased after merge of #119.

@pelme
Copy link
Owner

pelme commented May 27, 2025

cc @Majsvaffla

@davepeck
Copy link

davepeck commented May 27, 2025

@jodal This is great! I ripped out my home-spun @with_children in a project I'm working on and replaced it with yours; all was well, including type checking.

One thing that might be worth documenting is how to layer context consumers and @with_children:

from dataclasses import dataclass
import htpy as h

@dataclass(frozen=True)
class User:
    name: str
    email: str


USER_CONTEXT: h.Context[User | None] = h.Context("user", default=None)

@h.with_children
@USER_CONTEXT.consumer
def UserCard(user: User | None, children: h.Node, *, extra: str) -> h.Renderable:
    ...

I don't tend to use contexts personally, but there you have it.

FWIW, I think @with_children is a natural enough pattern (with a tricky enough implementation, particularly where type hints are concerned) that I'd love to see it find its way into htpy proper.

PS: Sorry it took so long to get back to this. PyCon needed extra recovery time. :-)

@jodal jodal force-pushed the with-children branch 3 times, most recently from 4836f1e to f30f77d Compare May 29, 2025 17:34
@jodal jodal force-pushed the with-children branch 2 times, most recently from 4b23060 to b279936 Compare May 29, 2025 17:47
@jodal
Copy link
Contributor Author

jodal commented May 29, 2025

I've fixed the typo @Majsvaffla found and added a paragraph and an example about how to combine context consumers and @with_children, as suggested by @davepeck.

@pelme pelme merged commit 6777390 into pelme:main May 30, 2025
14 checks passed
@pelme
Copy link
Owner

pelme commented May 30, 2025

awesome, thanks! 🙌

jodal added a commit to jodal/htpy that referenced this pull request May 31, 2025
In pelme#119, all markupsafe imports were updated to simply `import
markupsafe`. This change was not applied to pelme#113 before it was merged.
pelme pushed a commit that referenced this pull request May 31, 2025
In #119, all markupsafe imports were updated to simply `import
markupsafe`. This change was not applied to #113 before it was merged.
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

Successfully merging this pull request may close these issues.

4 participants