Skip to content

Instantly share code, notes, and snippets.

@jph00
Last active December 29, 2024 13:59
Show Gist options
  • Save jph00/0590da374a11b8def808c1821abdd42a to your computer and use it in GitHub Desktop.
Save jph00/0590da374a11b8def808c1821abdd42a to your computer and use it in GitHub Desktop.

Revisions

  1. jph00 revised this gist Sep 7, 2024. 1 changed file with 15 additions and 12 deletions.
    27 changes: 15 additions & 12 deletions csv-serve-main.py
    Original file line number Diff line number Diff line change
    @@ -2,24 +2,28 @@

    db = database(':memory:')
    tbl = None
    app, rt = fast_app(live=True)
    hdrs = (Style('''
    button,input { margin: 0 1rem; }
    [role="group"] { border: 1px solid #ccc; }
    '''), )
    app, rt = fast_app(live=True, hdrs=hdrs)

    @rt("/")
    async def get():
    """Main page with upload form and results display"""
    return Titled("CSV Uploader",
    Form(hx_post="/upload", hx_target="#results")(
    Group(
    Input(type="file", name="csv_file", accept=".csv"),
    Button("Upload")),
    ), A('Download', href='/download'),
    Group(
    Input(type="file", name="csv_file", accept=".csv"),
    Button("Upload", hx_post="/upload", hx_target="#results",
    hx_encoding="multipart/form-data", hx_include='previous input'),
    A('Download', href='/download', type="button")
    ),
    Div(id="results"))

    def render_row(row):
    vals = [Input(value=v, name=k) for k,v in row.items()]
    vals.append(Button('delete', hx_get=remove.rt(id=row['id'])))
    vals.append(Button('update', hx_post='/update', hx_include="closest tr")),
    return Tr(*map(Td,vals), hx_target='closest tr', hx_swap='outerHTML')
    vals = [Td(Input(value=v, name=k)) for k,v in row.items()]
    vals.append(Td(Group(Button('delete', hx_get=remove.rt(id=row['id'])),
    Button('update', hx_post='/update', hx_include="closest tr"))))
    return Tr(*vals, hx_target='closest tr', hx_swap='outerHTML')

    @rt
    async def download():
    @@ -36,7 +40,6 @@ def remove(id:int): tbl.delete(id)

    @rt("/upload")
    async def post(csv_file: UploadFile):
    """Handle CSV upload and display"""
    global tbl
    if not csv_file.filename.endswith('.csv'): return "Please upload a CSV file"
    tbl = db.import_file('test', await csv_file.read(), pk='id')
  2. jph00 revised this gist Sep 7, 2024. 1 changed file with 13 additions and 12 deletions.
    25 changes: 13 additions & 12 deletions csv-serve-main.py
    Original file line number Diff line number Diff line change
    @@ -9,23 +9,24 @@ async def get():
    """Main page with upload form and results display"""
    return Titled("CSV Uploader",
    Form(hx_post="/upload", hx_target="#results")(
    Input(type="file", name="csv_file", accept=".csv"),
    Grid(
    Button("Upload"), #, hx_post="/upload", hx_target="#results"),
    Button('Download', href='/download')
    ),
    ),
    Group(
    Input(type="file", name="csv_file", accept=".csv"),
    Button("Upload")),
    ), A('Download', href='/download'),
    Div(id="results"))

    def render_row(row):
    vals = [Input(value=v, name=k) for k,v in row.items()]
    vals.append(Button('delete', hx_target='closest tr', hx_swap='outerHTML', hx_get=remove.rt(id=row['id'])))
    vals.append(Button('update', hx_target='closest tr', hx_swap='outerHTML', hx_post='/update', hx_include="closest tr")),
    return Tr(*map(Td,vals))
    vals.append(Button('delete', hx_get=remove.rt(id=row['id'])))
    vals.append(Button('update', hx_post='/update', hx_include="closest tr")),
    return Tr(*map(Td,vals), hx_target='closest tr', hx_swap='outerHTML')

    @rt
    #def download(): return Response('hi,there\n', media_type='text/csv')
    #def download(): return FileResponse(tbl2csv(tbl()))
    async def download():
    csv_data = [",".join(map(str, tbl.columns_dict))]
    csv_data += [",".join(map(str, row.values())) for row in tbl()]
    headers = {'Content-Disposition': 'attachment; filename="data.csv"'}
    return Response("\n".join(csv_data), media_type="text/csv", headers=headers)

    @rt('/update')
    def post(d:dict): return render_row(tbl.update(d))
    @@ -43,4 +44,4 @@ async def post(csv_file: UploadFile):
    vals = [render_row(row) for row in tbl()]
    return Table(Thead(header), Tbody(*vals))

    serve()
    serve()
  3. jph00 created this gist Sep 7, 2024.
    46 changes: 46 additions & 0 deletions csv-serve-main.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    from fasthtml.common import *

    db = database(':memory:')
    tbl = None
    app, rt = fast_app(live=True)

    @rt("/")
    async def get():
    """Main page with upload form and results display"""
    return Titled("CSV Uploader",
    Form(hx_post="/upload", hx_target="#results")(
    Input(type="file", name="csv_file", accept=".csv"),
    Grid(
    Button("Upload"), #, hx_post="/upload", hx_target="#results"),
    Button('Download', href='/download')
    ),
    ),
    Div(id="results"))

    def render_row(row):
    vals = [Input(value=v, name=k) for k,v in row.items()]
    vals.append(Button('delete', hx_target='closest tr', hx_swap='outerHTML', hx_get=remove.rt(id=row['id'])))
    vals.append(Button('update', hx_target='closest tr', hx_swap='outerHTML', hx_post='/update', hx_include="closest tr")),
    return Tr(*map(Td,vals))

    @rt
    #def download(): return Response('hi,there\n', media_type='text/csv')
    #def download(): return FileResponse(tbl2csv(tbl()))

    @rt('/update')
    def post(d:dict): return render_row(tbl.update(d))

    @rt
    def remove(id:int): tbl.delete(id)

    @rt("/upload")
    async def post(csv_file: UploadFile):
    """Handle CSV upload and display"""
    global tbl
    if not csv_file.filename.endswith('.csv'): return "Please upload a CSV file"
    tbl = db.import_file('test', await csv_file.read(), pk='id')
    header = Tr(*map(Th, tbl.columns_dict))
    vals = [render_row(row) for row in tbl()]
    return Table(Thead(header), Tbody(*vals))

    serve()