8000 GitHub - banditburai/ft-datastar
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Notifications You must be signed in to change notification settings

banditburai/ft-datastar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Datastar FastHTML

Datastar integration for FastHTML.

Installation

pip install git+https://github.com/banditburai/ft-datastar.git

Usage example in FastHTML

from fasthtml.common import *
from ft_datastar import *
from json import dumps as json_dumps

datastar_script = Script(src="https://cdn.jsdelivr.net/gh/starfederation/datastar@v1.0.0-beta.8/bundles/datastar.js", type="module")

app, rt = fast_app(
    pico=False,
    surreal=False,
    htmx=False,
    live=False,    
    hdrs=(datastar_script,),
)

@rt("/")
def index():
    return Div(
        H2("Datastar + FastHTML Example", cls="text-xl font-bold mb-4"),
        Div(
            "Using signals we default the starting count to 5",
            ds_signals(count="5")
        ),
        Button(
            "Increment",
            ds_on(click="$count++"),
            cls="btn btn-primary mr-2"
        ),
        Button(
            "Reset",
            ds_on(click="$count = 0"),
            cls="btn btn-warning"
        ),
        Div(
            "Count: ",
            Span(ds_text("$count"), cls="font-mono"),
            cls="mt-2"
        ),
        cls="p-4 space-y-2"
    )

if __name__ == "__main__":
    serve()    

Features

ft-datastar provides a set of helper functions that make it easy to use Datastar with FastHTML. These helpers map directly to Datastar attributes and functionality.

Helper Functions

Function Description DataStar Equivalent
ds_attrs(**exprs) Bind attributes dynamically data-attr-* attributes
ds_bind(name) Two-way binding for form elements data-bind attribute
ds_classes(exprs, **kwargs) Conditionally apply CSS classes data-class attribute
ds_computed(**computed) Define computed properties data-computed attribute
ds_indicator(signal_name) Show loading indicator data-indicator attribute
ds_on(**events) Attach event handlers data-on-* attributes
ds_show(*args, **conditions) Conditionally show elements data-show attribute
ds_signals(exprs, **kwargs) Define reactive signals data-signals attribute
ds_text(expr) Bind text content data-text attribute

Server-Sent Events (SSE)

ft-datastar includes support for Server-Sent Events (SSE) to enable real-time updates:

Function Description
sse(handler) Decorator for SSE endpoint handlers
update_signals(**signals) Update signal values
update_fragments(fragment, selector, merge_mode) Update DOM fragments
FastHTMLDatastarSSEResponse Response class for SSE streams

Components

Component Description
IconifyIcon Wrapper for the Iconify web component

Examples

Check out the examples directory for sample applications and usage patterns.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0