Source code for galaxy.web.framework.helpers.tags

Code derived from WebHelpers:

Licensed under the MIT license:

from markupsafe import escape_silent

[docs]def format_attrs(**attrs): """Format HTML attributes into a string of ' key="value"' pairs which can be inserted into an HTML tag. The attributes are sorted alphabetically. If any value is None, the entire attribute is suppressed. Usage: >>> format_attrs(p=2, q=3) == u' p="2" q="3"' True >>> format_attrs(p=2, q=None) == u' p="2"' True >>> format_attrs(p=None) == u'' True """ strings = [f' {attr}="{escape_silent(value)}"' for attr, value in sorted(attrs.items()) if value is not None] return "".join(strings)