gradio
/
render_readme.py
41 строка · 2.0 Кб
1#!/usr/bin/env python
2
3import re4from pathlib import Path5
6README_TEMPLATE_FILEPATH = "readme_template.md"7GETTING_STARTED_TEMPLATE_FILEPATH = "guides/01_getting-started/01_quickstart.md"8
9readme_template = Path(README_TEMPLATE_FILEPATH).read_text()10getting_started_template = Path(GETTING_STARTED_TEMPLATE_FILEPATH).read_text()11getting_started_template = getting_started_template.replace("# Quickstart", "")12getting_started_template = getting_started_template.replace("Tip:", "> [!TIP]\n >")13
14# Extract all the code and demo tags from the getting started template
15code_tags = re.findall(r"\$code_([^\s]+)", getting_started_template)16demo_tags = re.findall(r"\$demo_([^\s]+)", getting_started_template)17codes = {}18demos = {}19
20for src in code_tags:21context = Path(f"demo/{src}/run.py").read_text()22# Replace the condition to run the demo directly with actual launch code23context = re.sub(r"if __name__(.*[\n$]*)*", "demo.launch()", context)24codes[src] = f"```python\n{context}\n```\n" # Convert to Markdown code block25
26for src in demo_tags:27demos[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.
31getting_started_template = re.sub(r"^(#+)", r"#\1", getting_started_template, flags=re.MULTILINE)32readme_template = readme_template.replace("$getting_started", getting_started_template)33
34# Now put the codes and the screenshots in the README template
35readme_template = re.sub(r"\$code_([^\s]+)", lambda x: codes[x.group(1)], readme_template)36readme_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)
39EDITING_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. -->")41Path("README.md").write_text(f"{EDITING_NOTE}\n\n{readme_template}")42