From 518c97f78ad81d4611209821a132ef30616412eb Mon Sep 17 00:00:00 2001 From: Francesco Frassinelli Date: Wed, 6 Mar 2024 12:29:28 +0100 Subject: [PATCH] Minor UI improvement --- src/webapp.py | 60 ++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src/webapp.py b/src/webapp.py index 9ddfd51..82a3789 100755 --- a/src/webapp.py +++ b/src/webapp.py @@ -11,18 +11,17 @@ import requests from pywebio import start_server from pywebio.output import ( - put_buttons, - put_file, + put_button, put_html, put_image, - put_info, put_progressbar, + put_success, put_table, set_progressbar, use_scope, ) from pywebio.pin import pin, put_textarea -from pywebio.session import run_js, set_env +from pywebio.session import download, run_js, set_env ENDPOINT = os.getenv( "RECONCILE_ENDPOINT", "http://datasette:8001/common/species/-/reconcile" @@ -69,25 +68,12 @@ """ -def buttons_callback(value): - if value == "check": - generate_table(pin.species_textarea) - elif value == "copy": - run_js(js_copy_table) - - def webapp(): set_env(title=TITLE, output_max_width="100%") put_html(INTRO) put_image(IMAGE) put_textarea("species_textarea") - put_buttons( - [ - {"label": "Sjekk artsnavn", "value": "check"}, - {"label": "Kopier resultat", "value": "copy", "color": "success"}, - ], - onclick=buttons_callback, - ) + put_button("Sjekk artsnavn", onclick=lambda: generate_table(pin.species_textarea)) def get_species(text): @@ -129,6 +115,14 @@ def get_species_data(species): return species_data +def table_to_csv(table): + buffer = io.StringIO() + csv_writer = csv.writer(buffer) + csv_writer.writerow(COLUMNS) + csv_writer.writerows(table) + return buffer.getvalue().encode("utf8") + + def generate_table(text): with use_scope("result", clear=True): put_progressbar("bar", auto_close=True) @@ -141,17 +135,29 @@ def generate_table(text): table.extend(species_data) progress = len(table) / len(species) set_progressbar("bar", progress) - if len(table) <= PREVIEW_SIZE: - put_table(table, header=COLUMNS) + message = [ + "%d ligne(r) behandlet." % len(table), + put_button( + "Last ned CSV", lambda: download("arter.csv", table_to_csv(table)) + ), + ] + table_preview = len(table) <= PREVIEW_SIZE + if table_preview: + message.insert( + 1, + put_button( + "Kopier resultat", + onclick=lambda: run_js(js_copy_table), + ), + ) else: - buffer = io.StringIO() - csv_writer = csv.writer(buffer) - csv_writer.writerow(COLUMNS) - csv_writer.writerows(table) - put_info( - "Mer enn %d arter: tabellen er for stor til å vises." % PREVIEW_SIZE, - put_file("arter.csv", buffer.getvalue().encode("utf8"), "Last ned CSV"), + message.insert( + 1, + "Mer enn %d ligner: tabellen er for stor til å vises." % PREVIEW_SIZE, ) + put_success(*message) + if table_preview: + put_table(table, header=COLUMNS) def main():