gradio/render_readme.py
Ömer Faruk Özdemir 8544295df2
add-logos-to-repo (#1421)
* add-logos-to-repo

- only encode is missing

* add-logos-to-repo

- add encode logo, add logos to the readme

* add-logos-to-repo

- change python logo, add HF logo
- positioning tweaks on README

* add-logos-to-repo

- tweaks

* add-logos-to-repo

- tweaks

* Update README.md

* add-logos-to-repo

- tweaks

* add-logos-to-repo

- remove gray deee oh :D

* add-logos-to-repo

- final readme design

* add-logos-to-repo

- undo readme changes

* render_readme fix for windows

* add-logos-to-repo

- last tweaks

* add-logos-to-repo

- last tweaks

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2022-06-09 11:05:10 +03:00

53 lines
2.0 KiB
Python

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()
code_tags = re.findall(r'\{\{ code\["([^\s]*)"\] \}\}', getting_started)
demo_tags = re.findall(r'\{\{ demos\["([^\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)"
)
class GuidesLoader(BaseLoader):
def __init__(self, path):
self.path = path
def get_source(self, environment, template):
path = join(self.path, template)
if not exists(path):
raise TemplateNotFound(template)
mtime = getmtime(path)
with open(path, encoding="utf-8") as f:
source = f.read()
return source, path, lambda: mtime == getmtime(path)
readme_template = Environment(loader=GuidesLoader(".")).get_template(README_TEMPLATE)
output_readme = readme_template.render(code=code, demos=demos)
output_readme = output_readme.replace("(/assets/", "(website/homepage/src/assets/")
with open("README.md", "w", encoding="utf-8") as readme_md:
output_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" + output_readme
readme_md.write(output_readme)