fasthtml icon indicating copy to clipboard operation
fasthtml copied to clipboard

[FEATURE] Allow any Mapping for attribute dictionaries

Open gazpachoking opened this issue 6 months ago • 0 comments

If you'd like to discuss your feature idea first with the community (highly recommended!) please visit our Discord channel.

Is your feature request related to a problem? Please describe. When passing in a custom Mapping it doesn't get detected as attributes. e.g. Div(MyMapping(), "content")

Describe the solution you'd like Currently the code is using an isinstance(x, dict) check, I'd like to change it to isinstance(x, Mapping) to allow any mapping types. https://github.com/AnswerDotAI/fasthtml/blob/ad42bb8fb70b47516da914aa5ea633e800c04191/fasthtml/components.py#L87

Example code

# Your example code here
Div(MyMapping(), {'regular': 'dict'}, Span("content"))

Similar implementations If available, provide links to similar features in other libraries:

  1. htpy allows any Mapping, not just dicts.

Problem solved Explain what problem this feature would solve and how it would benefit users of the library: I'm creating a helper for datastar which allows constructing datastar attributes. Ideally this helper is easily usable with multiple html building frameworks. Rather than subclassing a dict, it subclasses Mapping to make things simpler. Currently it has to be used with Fasthtml via unpacking Div(**data.on("click", "console.log('clicked');")) but ideally it would work the same as a dictionary as well. Div(data.on("click", "console.log('clicked');") https://github.com/starfederation/datastar/pull/902

Additional context Add any other context or screenshots about the feature request here.

Confirmation Please confirm the following:

  • [x] I have checked the existing issues and pull requests to ensure this feature hasn't been requested before.
  • [x] I have read the project's documentation to ensure this feature doesn't already exist.

gazpachoking avatar May 20 '25 16:05 gazpachoking