AnswerDotAI / fasthtml

The fastest way to create an HTML app
https://fastht.ml/
Apache License 2.0
5.54k stars 235 forks source link

setup_toasts() broken or mis-documented #443

Closed talkingtoaj closed 1 month ago

talkingtoaj commented 1 month ago
css = Style(':root {--pico-font-size:90%,--pico-font-family: Pacifico, cursive;}')
sse = Script(src="https://unpkg.com/htmx-ext-sse@2.2.1/sse.js")
hdrs = (css, sse)
app, rt = fast_app(hdrs=hdrs, live=True)

setup_toasts(app)

Gives compile time error of

  File ".../.cache/pypoetry/virtualenvs/story-writer-s9Ds3i4O-py3.12/lib/python3.12/site-packages/fasthtml/toaster.py", line 57, in setup_toasts
    app.router.hdrs += (Style(toast_css), Script(toast_js, type="module"))
    ^^^^^^^^^^^^^^^
AttributeError: 'RouterX' object has no attribute 'hdrs'

I've tried diagnosing it unsuccessfully.

Removing headers doesn't help.

78wesley commented 1 month ago

If you update FastHTML to the the latest version currently v0.6.8. Do you still have that issue?

pydanny commented 1 month ago

@talkingtoaj I can't replicate your error. Here's my sample code, which uses your example as a base. If you can provide a reproducable error let me know with a flag to @pydanny and I'll reopen this ticket.

from fasthtml.common import *

css = Style(':root {--pico-font-size:90%,--pico-font-family: Pacifico, cursive;}')
sse = Script(src="https://unpkg.com/htmx-ext-sse@2.2.1/sse.js")
hdrs = (css, sse)
app, rt = fast_app(hdrs=hdrs)

setup_toasts(app)

@rt
def index(sess):
    return Titled('Hello, world',
    P(A(href='/toast-test')('Toast test'))
    )

@rt('/toast-test')
def get(sess):
    add_toast(sess, 'This is a toast')
    return Redirect('/')

serve()