AnswerDotAI / fasthtml

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

@dataclass does not work when __ft__ function returns multiple elements #236

Closed Ivan-hdz closed 1 month ago

Ivan-hdz commented 2 months ago

Hi! I am having the following scenario using the dataclass decorator with a class When returning multiple elements in the __ft__ function I am getting a JSON representation of the class instead of HTML elements Code:

# /src/pages/home/home_page.py
from fasthtml.common import *;

@dataclass
class HomePage:

    def __ft__(self):
        return H1('HomePage'), Div('hello!');
# main.py
from fasthtml.common import *

from src.pages.home.home_page import *;

def e404_handler(req,exc):
    return Titled('Page not found');

app, route = fast_app(exception_handlers={404: e404_handler});

@route('/')
def get():
    return HomePage();

I also tried:

@dataclass
class HomePage:

    def __ft__(self):
        return Titled('HomePage', Div('hello'))

Result: image

I had to rewrite the __tf__ function as follows in order to get it work:

# /src/pages/home/home_page.py

from fasthtml.common import *;

@dataclass
class HomePage:

    def __ft__(self):
        return Div(H1('HomePage'), Div('hello!'));
Rahulbeniwal26119 commented 2 months ago

@Ivan-hdz Checking it.

Rahulbeniwal26119 commented 2 months ago

@Ivan-hdz @jph00 Pr has been raised in fastcore. Please leave comments there. Both above code snippets are rendering fine after this change.