Skip to content

Commit

Permalink
Report error when there is no match
Browse files Browse the repository at this point in the history
  • Loading branch information
frafra committed Mar 6, 2024
1 parent 38b4304 commit 06045a2
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pywebio import start_server
from pywebio.output import (
put_button,
put_error,
put_html,
put_image,
put_progressbar,
Expand Down Expand Up @@ -71,7 +72,9 @@ def webapp():
set_env(title=TITLE, output_max_width="100%")
put_html(INTRO)
put_textarea("species_textarea")
put_button("Sjekk artsnavn", onclick=lambda: generate_table(pin.species_textarea))
put_button(
"Sjekk artsnavn", onclick=lambda: generate_scoped_table(pin.species_textarea)
)


def get_species(text):
Expand All @@ -82,6 +85,10 @@ def get_species(text):
yield line


class NoMatch(Exception):
pass


def get_species_data(species):
species_data = []
data = {
Expand All @@ -95,7 +102,10 @@ def get_species_data(species):
response = requests.post(ENDPOINT, data=data, headers=headers).json()
specie_ids = []
for index, specie in enumerate(species):
result = response[str(index)]["result"][0]
results = response[str(index)]["result"]
if len(results) == 0:
raise NoMatch(f'Ingen treff funnet for "{specie}"')
result = results[0]
specie_ids.append((result["id"], result["score"]))
data = {
"extend": json.dumps(
Expand Down Expand Up @@ -126,7 +136,7 @@ def table_to_csv(table):


def generate_table(text):
with use_scope("result", clear=True):
try:
put_progressbar("bar", auto_close=True)
species = list(get_species(text))
table = []
Expand Down Expand Up @@ -178,6 +188,15 @@ def generate_table(text):
]
+ COLUMNS,
)
except NoMatch as e:
put_error(e)
finally:
set_progressbar("bar", 1)


def generate_scoped_table(*args):
with use_scope("result", clear=True):
generate_table(*args)


def main():
Expand Down

0 comments on commit 06045a2

Please sign in to comment.