forked from gradio-app/gradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_readme.py
57 lines (47 loc) · 1.87 KB
/
render_readme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import re
from os.path import exists, getmtime, join
from jinja2 import BaseLoader, Environment, TemplateNotFound
README_TEMPLATE = "readme_template.md"
GETTING_STARTED_TEMPLATE = "getting_started.md"
with open(join("guides", GETTING_STARTED_TEMPLATE), encoding="utf-8") as getting_started_file:
getting_started = getting_started_file.read()
getting_started = "\n".join(
[
line
for i, line in enumerate(getting_started.split("\n"))
if not line.startswith("Pinned: ")
]
)
code_tags = re.findall(r'\$code_([^\s]+)', getting_started)
demo_tags = re.findall(r'\$demo_([^\s]+)', getting_started)
code, demos = {}, {}
for code_src in code_tags:
with open(join("demo", code_src, "run.py")) as code_file:
python_code = code_file.read()
python_code = python_code.replace(
'if __name__ == "__main__":\n demo.launch()', "demo.launch()"
)
python_code = python_code.replace("\n\n\n", "\n\n")
code[code_src] = "```python\n" + python_code + "\n```"
for demo_src in demo_tags:
demos[demo_src] = (
"![" + demo_src + " interface](demo/" + demo_src + "/screenshot.gif)"
)
with open(README_TEMPLATE) as readme_template_md:
readme = readme_template_md.read()
readme = readme.replace("$getting_started", getting_started)
readme = re.sub(
r"\$code_([a-z _\-0-9]+)",
lambda x: code[x.group(1)],
readme
)
readme = re.sub(
r"\$demo_([a-z _\-0-9]+)",
lambda x: demos[x.group(1)],
readme
)
readme = readme.replace("(/assets/", "(guides/assets/")
with open("README.md", "w", encoding="utf-8") as readme_md:
readme = """<!-- DO NOT EDIT THIS FILE DIRECTLY. INSTEAD PLEASE EDIT: "readme_template.md" or
"guides/getting_started.md", AND THEN RUN: "python render_readme.py" --> """ + "\n" + readme
readme_md.write(readme)