Skip to content

Commit 4e171cf

Browse files
authored
Make fields variable width in project list of ModelCard (#809)
1 parent 4dcece7 commit 4e171cf

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

annif/hfh_util.py

+22-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from flask import current_app
1818

1919
import annif
20+
from annif import cli_util
2021
from annif.config import AnnifConfigCFG
2122
from annif.exception import OperationFailedException
2223
from annif.project import Access, AnnifProject
@@ -349,18 +350,27 @@ def _update_projects_section(text, configs):
349350

350351

351352
def _create_projects_section(configs):
352-
content = f"{AUTOUPDATING_START}\n## Projects\n"
353-
354-
template = "{0:<19} {1:<23} {2:<15} {3:<8}\n"
355-
header = template.format("Project ID", "Project Name", "Vocabulary ID", "Language")
356-
content += "```\n" + header + "-" * len(header.strip()) + "\n"
357-
358-
for proj_id in configs.project_ids:
359-
project = configs[proj_id]
360-
content += template.format(
353+
column_headings = (
354+
"Project ID",
355+
"Project Name",
356+
"Vocabulary ID",
357+
"Language",
358+
)
359+
table = [
360+
(
361361
proj_id,
362-
project["name"],
363-
project["vocab"],
364-
project["language"],
362+
configs[proj_id]["name"],
363+
configs[proj_id]["vocab"],
364+
configs[proj_id]["language"],
365365
)
366+
for proj_id in configs.project_ids
367+
]
368+
template = cli_util.make_list_template(column_headings, *table) + "\n"
369+
370+
header = template.format(*column_headings)
371+
372+
content = f"{AUTOUPDATING_START}\n## Projects\n"
373+
content += "```\n" + header + "-" * len(header.strip()) + "\n"
374+
for row in table:
375+
content += template.format(*row)
366376
return content + "```\n" + AUTOUPDATING_END

tests/test_hfh_util.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,8 @@ def test_upsert_modelcard_update_existing(read_text, glob, load, push_to_hub, pr
204204
load.assert_called_once_with(repo_id)
205205

206206
card = load.return_value
207-
retained_project_list_content = (
208-
"dummy-en Dummy English dummy en"
209-
)
207+
retained_project_list_content = "dummy-en Dummy English dummy en "
208+
210209
assert retained_project_list_content in card.text
211210
assert sorted(card.data.language) == ["en", "fi"]
212211
card.push_to_hub.assert_called_once_with(
@@ -227,8 +226,8 @@ def test_update_modelcard_projects_section_append_new():
227226
<!--- start-of-autoupdating-part --->
228227
## Projects
229228
```
230-
Project ID Project Name Vocabulary ID Language
231-
--------------------------------------------------------------------
229+
Project ID Project Name Vocabulary ID Language
230+
-------------------------------------------------
232231
```
233232
<!--- end-of-autoupdating-part --->"""
234233

@@ -250,8 +249,8 @@ def test_update_modelcard_projects_section_update_existing():
250249
<!--- start-of-autoupdating-part --->
251250
## Projects
252251
```
253-
Project ID Project Name Vocabulary ID Language
254-
--------------------------------------------------------------------
252+
Project ID Project Name Vocabulary ID Language
253+
-------------------------------------------------
255254
```
256255
<!--- end-of-autoupdating-part --->\n"""
257256

@@ -266,9 +265,9 @@ def test_update_modelcard_projects_section_update_existing():
266265
<!--- start-of-autoupdating-part --->
267266
## Projects
268267
```
269-
Project ID Project Name Vocabulary ID Language
270-
--------------------------------------------------------------------
271-
dummy-fi Dummy Finnish dummy fi \n```
268+
Project ID Project Name Vocabulary ID Language
269+
--------------------------------------------------
270+
dummy-fi Dummy Finnish dummy fi \n```
272271
<!--- end-of-autoupdating-part --->
273272
"""
274273

0 commit comments

Comments
 (0)