Skip to main content

Core utilities and reusable patterns for FastHTML applications including page layouts, HTMX request handling, navbar, and confirm modal components.

Project description

cjm-fasthtml-app-core

Install

pip install cjm_fasthtml_app_core

Project Structure

nbs/
├── components/ (3)
│   ├── alerts.ipynb         # Alert components for displaying success, error, warning, and info messages
│   ├── confirm_modal.ipynb  # Generic destructive-confirm modal — Cancel-on-left, Confirm-on-right, with backdrop click-to-dismiss and defensive form-submission guards. Codifies **Convention V12 (Destructive-confirm composition)** as running code.
│   └── navbar.ipynb         # Responsive navigation bar components with mobile support
└── core/ (5)
    ├── errors.ipynb    # Utilities for converting structured errors to FastHTML responses, alerts, and error pages
    ├── html_ids.ipynb  # Base HTML ID constants for FastHTML applications
    ├── htmx.ipynb      # Utilities for handling HTMX requests and responses
    ├── layout.ipynb    # Page layout utilities for wrapping content with common page structure
    └── routing.ipynb   # Routing utilities for FastHTML applications

Total: 8 notebooks across 2 directories

Module Dependencies

graph LR
    components_alerts[components.alerts<br/>Alerts]
    components_confirm_modal[components.confirm_modal<br/>Confirm Modal]
    components_navbar[components.navbar<br/>Navbar]
    core_errors[core.errors<br/>Error Utilities]
    core_html_ids[core.html_ids<br/>HTML IDs]
    core_htmx[core.htmx<br/>HTMX Utilities]
    core_layout[core.layout<br/>Layout]
    core_routing[core.routing<br/>routing]

    components_alerts --> core_html_ids
    components_navbar --> core_html_ids
    core_errors --> components_alerts
    core_errors --> core_html_ids
    core_layout --> core_html_ids

5 cross-module dependencies detected

CLI Reference

No CLI commands found in this project.

Module Overview

Detailed documentation for each module in the project:

Alerts (alerts.ipynb)

Alert components for displaying success, error, warning, and info messages

Import

from cjm_fasthtml_app_core.components.alerts import (
    create_success_alert,
    create_error_alert,
    create_warning_alert,
    create_info_alert
)

Functions

def _create_auto_dismiss_script(
    timeout_ms:int=3000 # Time in milliseconds before auto-dismiss
) -> FT: # Script element for auto-dismissing alerts
    "Create a script that auto-dismisses the alert after a timeout."
def create_success_alert(
    message:str, # The success message to display
    timeout_ms:int=3000 # Time in milliseconds before auto-dismiss
) -> FT: # Div element containing the success alert
    "Create a success alert that auto-dismisses."
def create_error_alert(
    message:str, # The error message to display
    details:Optional[str]=None # Optional additional details text
) -> FT: # Div element containing the error alert
    "Create an error alert with optional details."
def create_warning_alert(
    message: str,  # The warning message to display
    details: Optional[str] = None  # Optional additional details text
) -> Div:  # Div element containing the warning alert
    "Create a warning alert with optional details."
def create_info_alert(
    message:str, # The info message to display
    details:Optional[str]=None # Optional additional details text
) -> FT: # Div element containing the info alert
    "Create an info alert with optional details."

Confirm Modal (confirm_modal.ipynb)

Generic destructive-confirm modal — Cancel-on-left, Confirm-on-right, with backdrop click-to-dismiss and defensive form-submission guards. Codifies Convention V12 (Destructive-confirm composition) as running code.

Import

from cjm_fasthtml_app_core.components.confirm_modal import (
    render_confirm_modal
)

Functions

def render_confirm_modal(
    modal_id:str, # HTML id for the <dialog> element
    body_id:str, # HTML id for the inner Div HTMX targets for message text
    title:str="Confirm Action?", # Modal title (rendered in <h3>)
    confirm_label:str="Confirm", # Right-button label
    confirm_icon:Optional[str]=None, # Optional Lucide icon name (e.g. "trash-2") prefixed to the confirm label
    cancel_label:str="Cancel", # Left-button label
    confirm_attrs:Optional[Dict[str, Any]]=None, # Caller-supplied attrs for the confirm button (hx_post / hx_vals / hx_swap / etc.)
) -> FT: # Dialog element implementing V12
    "Render a destructive-confirm modal (V12). Cancel-LEFT, Confirm-RIGHT, backdrop click-to-dismiss, defensive type=button. Body populated via HTMX swap into body_id."

Error Utilities (errors.ipynb)

Utilities for converting structured errors to FastHTML responses, alerts, and error pages

Import

from cjm_fasthtml_app_core.core.errors import (
    error_to_alert,
    error_to_htmx_response,
    create_error_page,
    error_to_page
)

Functions

def error_to_alert(
    error: Any,  # Error object (BaseError from cjm-error-handling or standard Exception)
    include_debug_info: bool = False  # Whether to include debug information in the alert
) -> FT:  # Alert component (error or warning)
    "Convert an error to an alert component."
def error_to_htmx_response(
    error: Any,  # Error object (BaseError or Exception)
    target_id: str = AppHtmlIds.ALERT_CONTAINER,  # ID of element to update with error
    include_debug_info: bool = False  # Whether to include debug information
) -> FT:  # Alert component with correct ID for HTMX targeting
    "Create an HTMX-compatible error response."
def create_error_page(
    title: str = "Error",  # Page title
    message: str = "An error occurred",  # Main error message
    details: Optional[str] = None,  # Optional details or debug info
    show_home_link: bool = True,  # Whether to show a link back to home
    home_path: str = "/"  # Path for the home link (defaults to root)
) -> FT:  # Div element containing the full error page
    "Create a full-page error display."
def error_to_page(
    error: Any,  # Error object (BaseError or Exception)
    include_debug_info: bool = False,  # Whether to include debug information
    show_home_link: bool = True,  # Whether to show a link back to home
    home_path: str = "/"  # Path for the home link (defaults to root)
) -> FT:  # Full error page component
    "Convert an error to a full-page error display."

HTML IDs (html_ids.ipynb)

Base HTML ID constants for FastHTML applications

Import

from cjm_fasthtml_app_core.core.html_ids import (
    AppHtmlIds
)

Classes

class AppHtmlIds:
    "Base HTML ID constants for FastHTML applications."
    
    def as_selector(
            id_str:str # The HTML ID to convert
        ) -> str: # CSS selector with # prefix
        "Convert an ID to a CSS selector format."

HTMX Utilities (htmx.ipynb)

Utilities for handling HTMX requests and responses

Import

from cjm_fasthtml_app_core.core.htmx import (
    is_htmx_request,
    handle_htmx_request
)

Functions

def is_htmx_request(
    request # FastHTML request object
) -> bool: # True if request is from HTMX
    "Check if a request is an HTMX request."
def handle_htmx_request(
    request, # FastHTML request object
    content_fn:Callable, # Function to generate content
    *args, # Positional arguments for content_fn
    wrap_fn:Optional[Callable]=None, # Optional wrapper function for full page requests
    **kwargs # Keyword arguments for content_fn
): # Content or wrapped content based on request type
    "Handle HTMX vs full page response pattern."

Layout (layout.ipynb)

Page layout utilities for wrapping content with common page structure

Import

from cjm_fasthtml_app_core.core.layout import (
    wrap_with_layout
)

Functions

def wrap_with_layout(
    content:FT, # The main content to display
    navbar:Optional[FT]=None, # Optional navbar component
    footer:Optional[FT]=None, # Optional footer component
    container_id:str=AppHtmlIds.MAIN_CONTENT, # ID for the main content container
    container_tag:str="div" # HTML tag for the container
) -> FT: # Main element with navbar and content
    "Wrap content with the full page layout including optional navbar and footer."

Navbar (navbar.ipynb)

Responsive navigation bar components with mobile support

Import

from cjm_fasthtml_app_core.components.navbar import (
    create_nav_link,
    create_navbar
)

Functions

def create_nav_link(
    label:str, # Link text to display
    route, # FastHTML route object with .to() method
    target_id:str=AppHtmlIds.MAIN_CONTENT # HTMX target container ID
) -> FT: # Anchor element with HTMX attributes
    "Create a navigation link with HTMX attributes for SPA-like behavior."
def create_navbar(
    title:str, # Application title
    nav_items:List[Tuple[str, Any]], # List of (label, route) tuples
    home_route:Optional[Any]=None, # Optional home route for title link
    theme_selector:bool=True, # Whether to include theme selector
    target_id:str=AppHtmlIds.MAIN_CONTENT, # HTMX target container ID
    **navbar_kwargs # Additional kwargs for navbar styling
) -> FT: # Navbar component
    "Create a responsive navigation bar with mobile dropdown menu."

routing (routing.ipynb)

Routing utilities for FastHTML applications

Import

from cjm_fasthtml_app_core.core.routing import (
    register_routes
)

Functions

def register_routes(
    app,  # FastHTML app instance
    *routers  # One or more APIRouter instances to register
) -> None:  # No return value
    "Register multiple APIRouter instances to a FastHTML app at once."

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cjm_fasthtml_app_core-0.0.14.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cjm_fasthtml_app_core-0.0.14-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file cjm_fasthtml_app_core-0.0.14.tar.gz.

File metadata

  • Download URL: cjm_fasthtml_app_core-0.0.14.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for cjm_fasthtml_app_core-0.0.14.tar.gz
Algorithm Hash digest
SHA256 8f3521783449808b6af7446e88af89accaa4486668b17abf8e1f5767d44d9e57
MD5 fab0b136fe7fddb6f0c79513a0ae41d7
BLAKE2b-256 4b43b068d113f89b5b193b6d0cc7a39151283833f335b4110c5d434ad7a38ee7

See more details on using hashes here.

File details

Details for the file cjm_fasthtml_app_core-0.0.14-py3-none-any.whl.

File metadata

File hashes

Hashes for cjm_fasthtml_app_core-0.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 58703da3c44803a7ed5bdef2e804e77d7bf3d61fdb339d5fe42a08fb869bf36f
MD5 551f1e89a8b9ab6211da46840068237f
BLAKE2b-256 12a44b9c5547d5fb37a63a8b01390a2c758ac6a04eab6df709dd92b4ae121a7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page