From 4e171cfb699633de47396d2317c140303013fcae Mon Sep 17 00:00:00 2001 From: Juho Inkinen <34240031+juhoinkinen@users.noreply.github.com> Date: Wed, 2 Oct 2024 11:07:24 +0300 Subject: [PATCH] Make fields variable width in project list of ModelCard (#809) --- annif/hfh_util.py | 34 ++++++++++++++++++++++------------ tests/test_hfh_util.py | 19 +++++++++---------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/annif/hfh_util.py b/annif/hfh_util.py index 67015373..b737548f 100644 --- a/annif/hfh_util.py +++ b/annif/hfh_util.py @@ -17,6 +17,7 @@ from flask import current_app import annif +from annif import cli_util from annif.config import AnnifConfigCFG from annif.exception import OperationFailedException from annif.project import Access, AnnifProject @@ -349,18 +350,27 @@ def _update_projects_section(text, configs): def _create_projects_section(configs): - content = f"{AUTOUPDATING_START}\n## Projects\n" - - template = "{0:<19} {1:<23} {2:<15} {3:<8}\n" - header = template.format("Project ID", "Project Name", "Vocabulary ID", "Language") - content += "```\n" + header + "-" * len(header.strip()) + "\n" - - for proj_id in configs.project_ids: - project = configs[proj_id] - content += template.format( + column_headings = ( + "Project ID", + "Project Name", + "Vocabulary ID", + "Language", + ) + table = [ + ( proj_id, - project["name"], - project["vocab"], - project["language"], + configs[proj_id]["name"], + configs[proj_id]["vocab"], + configs[proj_id]["language"], ) + for proj_id in configs.project_ids + ] + template = cli_util.make_list_template(column_headings, *table) + "\n" + + header = template.format(*column_headings) + + content = f"{AUTOUPDATING_START}\n## Projects\n" + content += "```\n" + header + "-" * len(header.strip()) + "\n" + for row in table: + content += template.format(*row) return content + "```\n" + AUTOUPDATING_END diff --git a/tests/test_hfh_util.py b/tests/test_hfh_util.py index 0bc22021..7b59ad4e 100644 --- a/tests/test_hfh_util.py +++ b/tests/test_hfh_util.py @@ -204,9 +204,8 @@ def test_upsert_modelcard_update_existing(read_text, glob, load, push_to_hub, pr load.assert_called_once_with(repo_id) card = load.return_value - retained_project_list_content = ( - "dummy-en Dummy English dummy en" - ) + retained_project_list_content = "dummy-en Dummy English dummy en " + assert retained_project_list_content in card.text assert sorted(card.data.language) == ["en", "fi"] card.push_to_hub.assert_called_once_with( @@ -227,8 +226,8 @@ def test_update_modelcard_projects_section_append_new(): ## Projects ``` -Project ID Project Name Vocabulary ID Language --------------------------------------------------------------------- +Project ID Project Name Vocabulary ID Language +------------------------------------------------- ``` """ @@ -250,8 +249,8 @@ def test_update_modelcard_projects_section_update_existing(): ## Projects ``` -Project ID Project Name Vocabulary ID Language --------------------------------------------------------------------- +Project ID Project Name Vocabulary ID Language +------------------------------------------------- ``` \n""" @@ -266,9 +265,9 @@ def test_update_modelcard_projects_section_update_existing(): ## Projects ``` -Project ID Project Name Vocabulary ID Language --------------------------------------------------------------------- -dummy-fi Dummy Finnish dummy fi \n``` +Project ID Project Name Vocabulary ID Language +-------------------------------------------------- +dummy-fi Dummy Finnish dummy fi \n``` """