Skip to content

Instantly share code, notes, and snippets.

@axelknock
axelknock / main.py
Last active January 29, 2025 22:21
Example of Datastar with FastHTML, ported from a FastAPI example
import asyncio
import json
from datetime import datetime
from fasthtml.common import *
from fasthtml.starlette import StreamingResponse
from fasthtml.core import to_xml
from datastar_py.sse import SSE_HEADERS, ServerSentEventGenerator
@axelknock
axelknock / main.py
Created November 22, 2024 18:06
Basic example of middleware to alter dataclass response from FT to json
from fasthtml.common import *
# Create middleware to handle requests ending in .json
class JSONRedirectMiddleware(MiddlewareBase):
def __init__(self, app):
self.app = app
# pass along the request with the application/json header
# if the route excepts it (as defined below, the response will be JSON)