Skip to content

Commit

Permalink
update init prompt to use new templates from reflex-dev/templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Lendemor committed Jul 16, 2024
1 parent ec35e4f commit 265050b
Showing 1 changed file with 53 additions and 29 deletions.
82 changes: 53 additions & 29 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Template(Base):

name: str
description: str
code_url: str
code_url: str | None = None
demo_url: str


Expand Down Expand Up @@ -1310,39 +1310,60 @@ def migrate_to_reflex():
print(line, end="")


def fetch_app_templates() -> dict[str, Template]:
"""Fetch the list of app templates from the Reflex backend server.
RELEASES_URL = f"https://api.github.com/repos/reflex-dev/templates/releases"


def fetch_app_templates(version: str) -> dict[str, Template]:
"""Fetch a dict of templates from the templates repo using github API.
Args:
version: The version of the templates to fetch.
Returns:
The name and download URL as a dictionary.
The dict of templates.
"""
config = get_config()
if not config.cp_backend_url:
console.info(
"Skip fetching App templates. No backend URL is specified in the config."
)
return {}
try:
response = httpx.get(
f"{config.cp_backend_url}{constants.Templates.APP_TEMPLATES_ROUTE}"
)

def get_release_by_tag(tag: str) -> dict | None:
response = httpx.get(RELEASES_URL)
response.raise_for_status()
return {
template["name"]: Template.parse_obj(template)
for template in response.json()
}
except httpx.HTTPError as ex:
console.info(f"Failed to fetch app templates: {ex}")
releases = response.json()
for release in releases:
if release["tag_name"] == f"v{tag}":
return release
return None

release = get_release_by_tag(version)
if release is None:
print(f"No templates known for version {version}")
return {}
except (TypeError, KeyError, json.JSONDecodeError) as tkje:
console.info(f"Unable to process server response for app templates: {tkje}")

assets = release.get("assets", [])
asset = next((a for a in assets if a["name"] == "templates.json"), None)
if asset is None:
print(f"Templates metadata not found for version {version}")
return {}
else:
templates_url = asset["browser_download_url"]

templates_data = httpx.get(templates_url, follow_redirects=True).json()["templates"]

for template in templates_data:
template["code_url"] = next(
(
a["browser_download_url"]
for a in assets
if a["name"] == f"{template['name']}.zip"
),
None,
)
return {
tp["name"]: Template.parse_obj(tp)
for tp in templates_data
if not tp["hidden"] and tp["code_url"] is not None
}


def create_config_init_app_from_remote_template(
app_name: str,
template_url: str,
):
def create_config_init_app_from_remote_template(app_name: str, template_url: str):
"""Create new rxconfig and initialize app using a remote template.
Args:
Expand Down Expand Up @@ -1437,7 +1458,7 @@ def initialize_app(app_name: str, template: str | None = None):
return

# Get the available templates
templates: dict[str, Template] = fetch_app_templates()
templates: dict[str, Template] = fetch_app_templates(constants.Reflex.VERSION)

# Prompt for a template if not provided.
if template is None and len(templates) > 0:
Expand Down Expand Up @@ -1467,9 +1488,12 @@ def initialize_app(app_name: str, template: str | None = None):
else:
console.error(f"Template `{template}` not found.")
raise typer.Exit(1)

if template_url is None:
return

create_config_init_app_from_remote_template(
app_name=app_name,
template_url=template_url,
app_name=app_name, template_url=template_url
)

telemetry.send("init", template=template)
Expand Down

0 comments on commit 265050b

Please sign in to comment.