Benjamin Bossan commited on
Commit
2481b28
1 Parent(s): 2174ccd

Add table of contents

Browse files
Files changed (2) hide show
  1. edit.py +21 -4
  2. utils.py +20 -2
edit.py CHANGED
@@ -223,10 +223,23 @@ def create_form_from_section(
223
 
224
 
225
  def display_sections(model_card: card.Card) -> None:
226
- for key, section_name, content, is_fig in iterate_key_section_content(
227
- model_card._data
228
- ):
229
- create_form_from_section(model_card, key, section_name, content, is_fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
 
232
  def display_model_card(model_card: card.Card) -> None:
@@ -236,6 +249,10 @@ def display_model_card(model_card: card.Card) -> None:
236
  # strip metadata
237
  with st.expander("show metadata"):
238
  st.text(metadata)
 
 
 
 
239
  st.markdown(rendered, unsafe_allow_html=True)
240
 
241
 
 
223
 
224
 
225
  def display_sections(model_card: card.Card) -> None:
226
+ for section_info in iterate_key_section_content(model_card._data):
227
+ create_form_from_section(
228
+ model_card,
229
+ key=section_info.return_key,
230
+ section_name=section_info.title,
231
+ content=section_info.content,
232
+ is_fig=section_info.is_fig,
233
+ )
234
+
235
+
236
+ def display_toc(model_card: card.Card) -> None:
237
+ elements = []
238
+ for section_info in iterate_key_section_content(model_card._data):
239
+ title, level = section_info.title, section_info.level
240
+ section_name = split_subsection_names(title)[-1]
241
+ elements.append(" " * level + "- " + section_name)
242
+ st.markdown("\n".join(elements))
243
 
244
 
245
  def display_model_card(model_card: card.Card) -> None:
 
249
  # strip metadata
250
  with st.expander("show metadata"):
251
  st.text(metadata)
252
+
253
+ with st.expander("Table of Contents"):
254
+ display_toc(model_card)
255
+
256
  st.markdown(rendered, unsafe_allow_html=True)
257
 
258
 
utils.py CHANGED
@@ -4,6 +4,7 @@ from __future__ import annotations
4
 
5
  import base64
6
  import re
 
7
  from pathlib import Path
8
  from typing import Iterator
9
 
@@ -61,11 +62,21 @@ def process_card_for_rendering(rendered: str) -> tuple[str, str]:
61
  return metadata, rendered_with_img
62
 
63
 
 
 
 
 
 
 
 
 
 
64
  def iterate_key_section_content(
65
  data: dict[str, Section],
66
  parent_section: str = "",
67
  parent_keys: list[str] | None = None,
68
- ) -> Iterator[tuple[str, str, str, bool]]:
 
69
  parent_keys = parent_keys or []
70
 
71
  for key, val in data.items():
@@ -79,11 +90,18 @@ def iterate_key_section_content(
79
 
80
  return_key = key if not parent_keys else "/".join(parent_keys + [key])
81
  is_fig = getattr(val, "is_fig", False)
82
- yield return_key, title, val.content, is_fig
 
 
 
 
 
 
83
 
84
  if val.subsections:
85
  yield from iterate_key_section_content(
86
  val.subsections,
87
  parent_section=title,
88
  parent_keys=parent_keys + [key],
 
89
  )
 
4
 
5
  import base64
6
  import re
7
+ from dataclasses import dataclass
8
  from pathlib import Path
9
  from typing import Iterator
10
 
 
62
  return metadata, rendered_with_img
63
 
64
 
65
+ @dataclass(frozen=True)
66
+ class SectionInfo:
67
+ return_key: str
68
+ title: str
69
+ content: str
70
+ is_fig: bool
71
+ level: int
72
+
73
+
74
  def iterate_key_section_content(
75
  data: dict[str, Section],
76
  parent_section: str = "",
77
  parent_keys: list[str] | None = None,
78
+ level=0,
79
+ ) -> SectionInfo:
80
  parent_keys = parent_keys or []
81
 
82
  for key, val in data.items():
 
90
 
91
  return_key = key if not parent_keys else "/".join(parent_keys + [key])
92
  is_fig = getattr(val, "is_fig", False)
93
+ yield SectionInfo(
94
+ return_key=return_key,
95
+ title=title,
96
+ content=val.content,
97
+ is_fig=is_fig,
98
+ level=level,
99
+ )
100
 
101
  if val.subsections:
102
  yield from iterate_key_section_content(
103
  val.subsections,
104
  parent_section=title,
105
  parent_keys=parent_keys + [key],
106
+ level=level + 1,
107
  )