AnswerDotAI / fasthtml

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

'items' table harcoded into fastapp.py #156

Closed copaceticmumbojumbo closed 1 month ago

copaceticmumbojumbo commented 1 month ago

Is the 'items' table supposed to be hardcoded into fastapp.py?

I ran into the issue when trying to display content from a separate table i created manually into the db. No matter how I called it, it never seemed to get the table I want. It also auto created an 'items' table to which my test data was inserted.

Please find the sample code below. I was bulding atop the example todo code.

from fasthtml.common import *

app,rt, students, Student = fast_app('data/buds.db', live=True, id=int, name=str, pk='id')

@rt('/')
def get():
    students.insert(Student(name='John Doe'))
    items = students()
    return Titled('Students',
                  Div(*items))

serve()

The database buds has one table named 'students' with columns id and name. An 'items' table was created when I ran the code. I tried deleting it and trying again, but it reappeared when rerun. So I thought there must be some hardcodings and searched for 'items' in fastapp.py, and voila, there it was, being mentioned twice under the fast_app() function.

db = database(db)
    if not tbls: tbls={}
    if kwargs:
        if isinstance(first(kwargs.values()), dict): tbls = kwargs
        else:
            kwargs['render'] = render
            tbls['items'] = kwargs
    dbtbls = [get_tbl(db.t, k, v) for k,v in tbls.items()]
    if len(dbtbls)==1: dbtbls=dbtbls[0]
    return app,app.route,*dbtbls

PS: Im playing with the db using Pycharms inbuilt db viewer.

jph00 commented 1 month ago

Yes if you just have the one table, it'll be called Items. It you want more and/or different names, pass in the tbls dict, where the keys are table names.

copaceticmumbojumbo commented 1 month ago

Could you show how to pass in the tbls dict? Im confused.