gradio/render_readme.py
Abubakar Abid 7bb561a294
Rewriting parts of the README and getting started guides for 4.0 (#6767)
* doc rewrite

* changes

* changes

* tip

* changes

* notebook

* add changeset

* history

* add

* quickstart done

* readme

* changes

* quickstart

* changes

* reorder

* link

* changes

* changes

* changes

* quickstart done

* readme

* quickstart

* quickstart'

* moving around

* spaces

* readme

* guides

* guides

* links

* readme

* readme

* readme

* readme

* readme

* readme

* readme

* readme

* readme

* readme

* email address

* add changeset

* shorten quickstart

* readme

* Update README.md

* readme

* changes

* Update guides/01_getting-started/01_quickstart.md

Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>

* Update guides/01_getting-started/01_quickstart.md

Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>

* Update guides/01_getting-started/01_quickstart.md

Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>

* changes

* changes

* remove gr.Interface.load

* guides

* changes

* more changes

* changes

* sharing

* concurrency

* changes

* changes

* components

* key features

* event listeners

* features

* notebook

* test

* guides

* changes

* changes

* changes

* transitions

* readme links

* links

* links

* guides

* new gif

* add gif

* update gif

* Update guides/02_building-interfaces/01_more-on-examples.md

* Update guides/03_building-with-blocks/01_blocks-and-event-listeners.md

* Update guides/01_getting-started/01_quickstart.md

Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>

* Update guides/01_getting-started/02_key-features.md

Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>

* Update guides/01_getting-started/02_key-features.md

Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>

* Update guides/02_building-interfaces/00_the-interface-class.md

Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>

* Update guides/01_getting-started/01_quickstart.md

Co-authored-by: Hannah <hannahblair@users.noreply.github.com>

* Update guides/01_getting-started/02_key-features.md

Co-authored-by: Hannah <hannahblair@users.noreply.github.com>

* Update guides/01_getting-started/02_key-features.md

Co-authored-by: Hannah <hannahblair@users.noreply.github.com>

* changes

* replace space

* changes

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
Co-authored-by: Hannah <hannahblair@users.noreply.github.com>
2023-12-20 11:07:48 -08:00

42 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python
import re
from pathlib import Path
README_TEMPLATE_FILEPATH = "readme_template.md"
GETTING_STARTED_TEMPLATE_FILEPATH = "guides/01_getting-started/01_quickstart.md"
readme_template = Path(README_TEMPLATE_FILEPATH).read_text()
getting_started_template = Path(GETTING_STARTED_TEMPLATE_FILEPATH).read_text()
getting_started_template = getting_started_template.replace("# Quickstart", "")
getting_started_template = getting_started_template.replace("Tip:", "> [!TIP]\n >")
# Extract all the code and demo tags from the getting started template
code_tags = re.findall(r"\$code_([^\s]+)", getting_started_template)
demo_tags = re.findall(r"\$demo_([^\s]+)", getting_started_template)
codes = {}
demos = {}
for src in code_tags:
context = Path(f"demo/{src}/run.py").read_text()
# Replace the condition to run the demo directly with actual launch code
context = re.sub(r"if __name__(.*[\n$]*)*", "demo.launch()", context)
codes[src] = f"```python\n{context}\n```\n" # Convert to Markdown code block
for src in demo_tags:
demos[src] = f"![`{src}` demo](demo/{src}/screenshot.gif)"
# Replace the headers in the getting started template with a smaller header (e.g. H3 instead of H2) to
# make the README more readable and less cluttered.
getting_started_template = re.sub(r"^(#+)", r"#\1", getting_started_template, flags=re.MULTILINE)
readme_template = readme_template.replace("$getting_started", getting_started_template)
# Now put the codes and the screenshots in the README template
readme_template = re.sub(r"\$code_([^\s]+)", lambda x: codes[x.group(1)], readme_template)
readme_template = re.sub(r"\$demo_([^\s]+)", lambda x: demos[x.group(1)], readme_template)
# Save the README template to the actual README.md file (with a note about the editing)
EDITING_NOTE = ("<!-- DO NOT EDIT THIS FILE DIRECTLY. INSTEAD EDIT THE `readme_template.md` OR "
"`guides/1)getting_started/1)quickstart.md` TEMPLATES AND THEN RUN `render_readme.py` SCRIPT. -->")
Path("README.md").write_text(f"{EDITING_NOTE}\n\n{readme_template}")