AnswerDotAI / fasthtml-tut

Code to go with beginner FastHTML tutorial
Apache License 2.0
19 stars 4 forks source link

errors when trying running all simple examples #1

Open jnchacon opened 2 months ago

jnchacon commented 2 months ago

when running simple01.py

... app = fast_app() rt = app.route ...

get this error:

rt = app.route
     ^^^^^^^^^

AttributeError: 'tuple' object has no attribute 'route'

when running simple02.py: ... app,todos,Todo = fast_app('data/todos.db', id=int, title=str, done=bool, pk='id') rt = app.route ....

get his error:

app,todos,Todo = fast_app('data/todos.db', id=int, title=str, done=bool, pk='id')
^^^^^^^^^^^^^^

ValueError: too many values to unpack (expected 3)

I think that this examples are for a previous version when the returned objects are different.

Also, the FT functions are not recognized.

dgwyer commented 2 months ago

I think the examples in this repo are a little out of date as it was one of the first FastHTML demos Jeremy presented.

For example here is an updated working version of simple01.py:

from fasthtml.common import *

app,rt = fast_app()

@rt('/')
def get():
    contents = Div(
        A('Link', hx_get='/page'),
        Card('hi'),
    )
    return PageX('Home', contents)

@rt('/page')
def get():
    contents = Div(
        A('Home', hx_get='/'),
    )
    return PageX('Page', contents)

serve()

@jph00 The examples in this repo aren't too complicated. I can submit a PR if you want them updated? Or prerhaps just add a note to the readme to mention these examples are outdated, and add a link to the official example repos.

jph00 commented 2 months ago

Good suggestion - I've updated the readme. I'd be happy to accept a PR too though, if you're interested, since that way this repo would match the tutorial video.

dgwyer commented 1 month ago

Good suggestion - I've updated the readme. I'd be happy to accept a PR too though, if you're interested, since that way this repo would match the tutorial video.

I don't mind updating the examples. I just wanted to check though the usage of Titled was correct when replacing the now removed PageX component. I found that when directly replacing PageX with Titled, it needed extra attributes to work as expected to update the browser URL.

from fasthtml.common import *

app,rt = fast_app(live=true)

@rt('/')
def get():
    contents = Div(
        A('Link', hx_get='/page', hx_target='body', hx_push_url='true'),
        Card('hi'),
    )
    return Titled('Home', contents)

@rt('/page')
def get():
    contents = Div(
        A('Home', hx_get='/', hx_target='body', hx_push_url='true'),
    )
    return Titled('Page', contents)

serve()

This works as expected. Is this the correct approach? Using hx_boost='true' along with the href attribute also works. Do you have a preference?

jph00 commented 1 month ago

Thanks for checking. Yes Titled is good to get the right formatting. And I think using href is a good change - afaict we don't actually need hx_boost for this example to work correctly?

Message ID: @.***>

dgwyer commented 1 month ago

That's right. The example will work fine with just href and no hx_boost, but it will replace the whole page HTML by default. Using hx_boost (as you know) makes this a little smoother as only the innerHTML of the body is replaced.

I guess we can keep it minimal for this simple example:

from fasthtml.common import *

app,rt = fast_app(live=true)

@rt('/')
def get():
    contents = Div(
        A('Link', href='/page'),
        Card('hi'),
    )
    return Titled('Home', contents)

@rt('/page')
def get():
    contents = Div(
        A('Home', href='/'),
    )
    return Titled('Page', contents)

serve()
dgwyer commented 1 month ago

FYI, I'll be taking a look at the updates this week, just to let you know I'm still interested in doing this. :)