Skip to content

Commit 038b76e

Browse files
authored
fix: Update tutorials script try 2 (#6335)
1 parent ef0c0e8 commit 038b76e

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

scripts/update_ml_tutorials.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,30 @@
2525

2626
import os
2727
import re
28+
import yaml
29+
import logging
2830
from pathlib import Path
2931
from typing import List
3032

31-
import yaml
3233

33-
ML_REPO_PATH = os.getenv('ML_REPO_PATH', 'label-studio-ml-backend/')
34+
ML_REPO_PATH = os.getenv('ML_REPO_PATH', '/ml/')
3435

3536

3637
def get_readme_files() -> List:
3738
p = Path(ML_REPO_PATH) / 'label_studio_ml' / 'examples'
38-
return list(Path(p).rglob('README.md'))
39+
return sorted(list(Path(p).rglob('README.md')))
3940

4041

4142
def parse_readme_file(file_path: str) -> dict:
43+
print(file_path)
4244
with open(file_path, 'r') as f:
4345
content = f.read()
44-
46+
4547
match = re.search(r"---(.*?)---", content, re.DOTALL)
46-
header = match.group(1).strip()
47-
body = re.sub(r'^---\n(.*?)\n---\n', '', content, flags=re.DOTALL).strip()
48-
49-
return {'header': header[0].strip() if header else '', 'body': body}
48+
header = match.group(1).strip() if match else ''
49+
body = content[content.find('-->') + 3:].strip()
50+
51+
return {'header': header, 'body': body}
5052

5153

5254
def create_tutorial_files():
@@ -66,9 +68,10 @@ def create_tutorial_files():
6668
f.write(parsed_content['header'])
6769
f.write('\n---\n\n')
6870
f.write(parsed_content['body'])
69-
files_and_headers.append(
70-
{'model_name': model_name, 'header': yaml.load(parsed_content['header'], Loader=yaml.FullLoader)}
71-
)
71+
files_and_headers.append({
72+
'model_name': model_name,
73+
'header': yaml.load(parsed_content['header'], Loader=yaml.FullLoader)
74+
})
7275

7376
update_ml_tutorials_index(files_and_headers)
7477

@@ -80,17 +83,22 @@ def update_ml_tutorials_index(files_and_headers: List):
8083
with open(str(p), 'r') as f:
8184
content = f.read()
8285

83-
match = re.search(r"---(.*?)---", content, re.DOTALL)
84-
yaml_content = match.group(1).strip()
86+
yaml_content = re.findall(r'---\n(.*?)\n---', content, re.DOTALL)
8587
# read in python dict
8688
data = yaml.load(yaml_content[0].strip(), Loader=yaml.FullLoader)
8789
data['cards'] = []
8890
print(data)
8991
for f in files_and_headers:
92+
h = f['header']
93+
if not isinstance(h, dict):
94+
logging.error(f'No dict header found in {f} file. Skipping ...')
95+
continue
9096
print('Processing', f['model_name'])
91-
h = f['header'] or {}
92-
card = {'title': h.get('title') or f['model_name'], 'url': f'/tutorials/{f["model_name"]}.html'}
93-
card.update(f['header'] or {})
97+
card = {
98+
'title': h.get('title') or f['model_name'],
99+
'url': f'/tutorials/{f["model_name"]}.html'
100+
}
101+
card.update(h)
94102
data['cards'].append(card)
95103

96104
p = Path(__file__).resolve().parent.parent / 'docs' / 'source' / 'guide' / 'ml_tutorials.html'

0 commit comments

Comments
 (0)