HTML IDs

Base HTML ID constants for FastHTML applications

source

AppHtmlIds

 AppHtmlIds ()

Base HTML ID constants for FastHTML applications.

This class provides centralized HTML ID constants that are commonly used across FastHTML applications. All IDs are defined as class attributes for IDE autocomplete and type checking.

For IDE Support:

IDEs like VS Code with Pylance will autocomplete these attributes and warn if you try to access non-existent attributes. To add app-specific IDs, extend this class:

class MyAppHtmlIds(AppHtmlIds):
    CUSTOM_SECTION = "custom-section"
    SIDEBAR = "sidebar"

Note: The typing.Final annotation indicates these are constants that shouldn’t be reassigned at runtime.

# Example usage
print(f"Main content ID: {AppHtmlIds.MAIN_CONTENT}")
print(f"As selector: {AppHtmlIds.as_selector(AppHtmlIds.MAIN_CONTENT)}")
Main content ID: main-content
As selector: #main-content
# Example: Extending AppHtmlIds for app-specific IDs
class MyAppHtmlIds(AppHtmlIds):
    """Extended HTML IDs for my application."""
    SIDEBAR = "sidebar"
    FOOTER = "footer"
    NAVBAR = "navbar"

# IDE will autocomplete these and warn about typos
print(f"Sidebar ID: {MyAppHtmlIds.SIDEBAR}")
print(f"As selector: {MyAppHtmlIds.as_selector(MyAppHtmlIds.SIDEBAR)}")
print(f"Main content still available: {MyAppHtmlIds.MAIN_CONTENT}")

# This would cause an IDE warning (if you uncomment it):
# print(MyAppHtmlIds.NONEXISTENT)  # ← IDE shows: "NONEXISTENT" is not a known attribute
Sidebar ID: sidebar
As selector: #sidebar
Main content still available: main-content