gradio

Форк
0
/
render_readme.py 
41 строка · 2.0 Кб
1
#!/usr/bin/env python
2

3
import re
4
from pathlib import Path
5

6
README_TEMPLATE_FILEPATH = "readme_template.md"
7
GETTING_STARTED_TEMPLATE_FILEPATH = "guides/01_getting-started/01_quickstart.md"
8

9
readme_template = Path(README_TEMPLATE_FILEPATH).read_text()
10
getting_started_template = Path(GETTING_STARTED_TEMPLATE_FILEPATH).read_text()
11
getting_started_template = getting_started_template.replace("# Quickstart", "")
12
getting_started_template = getting_started_template.replace("Tip:", "> [!TIP]\n >")
13

14
# Extract all the code and demo tags from the getting started template
15
code_tags = re.findall(r"\$code_([^\s]+)", getting_started_template)
16
demo_tags = re.findall(r"\$demo_([^\s]+)", getting_started_template)
17
codes = {}
18
demos = {}
19

20
for src in code_tags:
21
    context = Path(f"demo/{src}/run.py").read_text()
22
    # Replace the condition to run the demo directly with actual launch code
23
    context = re.sub(r"if __name__(.*[\n$]*)*", "demo.launch()", context)
24
    codes[src] = f"```python\n{context}\n```\n"  # Convert to Markdown code block
25

26
for src in demo_tags:
27
    demos[src] = f"![`{src}` demo](demo/{src}/screenshot.gif)"
28

29
# Replace the headers in the getting started template with a smaller header (e.g. H3 instead of H2) to
30
# make the README more readable and less cluttered.
31
getting_started_template = re.sub(r"^(#+)", r"#\1", getting_started_template, flags=re.MULTILINE)
32
readme_template = readme_template.replace("$getting_started", getting_started_template)
33

34
# Now put the codes and the screenshots in the README template
35
readme_template = re.sub(r"\$code_([^\s]+)", lambda x: codes[x.group(1)], readme_template)
36
readme_template = re.sub(r"\$demo_([^\s]+)", lambda x: demos[x.group(1)], readme_template)
37

38
# Save the README template to the actual README.md file (with a note about the editing)
39
EDITING_NOTE = ("<!-- DO NOT EDIT THIS FILE DIRECTLY. INSTEAD EDIT THE `readme_template.md` OR "
40
                "`guides/1)getting_started/1)quickstart.md` TEMPLATES AND THEN RUN `render_readme.py` SCRIPT. -->")
41
Path("README.md").write_text(f"{EDITING_NOTE}\n\n{readme_template}")
42

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.