Last active
December 29, 2024 13:59
-
-
Save jph00/0590da374a11b8def808c1821abdd42a to your computer and use it in GitHub Desktop.
Revisions
-
jph00 revised this gist
Sep 7, 2024 . 1 changed file with 15 additions and 12 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,24 +2,28 @@ db = database(':memory:') tbl = None 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(): return Titled("CSV Uploader", 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 = [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): 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') -
jph00 revised this gist
Sep 7, 2024 . 1 changed file with 13 additions and 12 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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")( 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_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 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() -
jph00 created this gist
Sep 7, 2024 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()