From 511ff8a33055c98a7393a30cc18fc8806e8ff7d2 Mon Sep 17 00:00:00 2001 From: Elijah Date: Thu, 14 Nov 2024 16:19:49 +0000 Subject: [PATCH] Add more templates and keep template name in kebab case --- reflex/constants/base.py | 6 ++++++ reflex/utils/format.py | 13 ++++++++++++ reflex/utils/prerequisites.py | 39 +++++++++++++++++++++++++++++------ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/reflex/constants/base.py b/reflex/constants/base.py index b50ce50ac..3266043c5 100644 --- a/reflex/constants/base.py +++ b/reflex/constants/base.py @@ -103,6 +103,12 @@ class Templates(SimpleNamespace): # The option for the user to choose a remote template. CHOOSE_TEMPLATES = "choose-templates" + # The URL to find reflex templates. + REFLEX_TEMPLATES_URL = "https://reflex.dev/templates" + + # Demo url for the default template. + DEFAULT_TEMPLATE_URL = "https://blank-template.reflex.run" + # The reflex.build frontend host REFLEX_BUILD_FRONTEND = "https://flexgen.reflex.run" diff --git a/reflex/utils/format.py b/reflex/utils/format.py index 1b3d1740f..42ba5a4d5 100644 --- a/reflex/utils/format.py +++ b/reflex/utils/format.py @@ -766,3 +766,16 @@ def format_data_editor_cell(cell: Any): "kind": Var(_js_expr="GridCellKind.Text"), "data": cell, } + + +def format_template_name(name: str) -> str: + """Format the template name of remote templates obtained during reflex init. + + Args: + name: The name of the template. + + Returns: + The formatted template name. + """ + formatted_name = to_kebab_case(name) + return formatted_name.removesuffix("-app").removesuffix("-template") diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index de1506775..6f55bec50 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -39,7 +39,7 @@ from reflex.utils.exceptions import ( GeneratedCodeHasNoFunctionDefs, raise_system_package_missing_error, ) -from reflex.utils.format import format_library_name +from reflex.utils.format import format_library_name, format_template_name from reflex.utils.registry import _get_npm_registry CURRENTLY_INSTALLING_NODE = False @@ -1242,6 +1242,30 @@ def prompt_for_template_options(templates: list[Template]) -> str: return templates[int(template)].name +def get_other_templates() -> list[dict[str, str]]: + """Get other templates not included in the templates repo. + + Returns: + List of other templates. + """ + return [ + { + "name": "llamaindex", + "description": "A minimal chat app using LLamaIndex.", + "demo_url": "https://frontend-gold-orca.dev.reflexcorp.run/", + "code_url": "https://github.com/reflex-dev/reflex-llamaindex-template/archive/refs/heads/main.zip", + "hidden": False, + }, + { + "name": "chat", + "description": "A chat app with a dark theme.", + "demo_url": "https://chat.reflex.run", + "code_url": "https://github.com/reflex-dev/reflex-chat/archive/refs/heads/main.zip", + "hidden": False, + }, + ] + + def fetch_app_templates(version: str) -> dict[str, Template]: """Fetch a dict of templates from the templates repo using github API. @@ -1288,9 +1312,12 @@ def fetch_app_templates(version: str) -> dict[str, Template]: ), None, ) + # include other templates not in the templates repo. + templates_data.extend(get_other_templates()) filtered_templates = {} for tp in templates_data: + tp["name"] = format_template_name(tp["name"]) if tp["hidden"] or tp["code_url"] is None: continue known_fields = set(f.name for f in dataclasses.fields(Template)) @@ -1391,7 +1418,7 @@ def prompt_for_remote_template_selection(templates: dict[str, Template]) -> str: """ while True: console.print( - "Visit https://reflex.dev/templates for the complete list of templates." + f"Visit {constants.Templates.REFLEX_TEMPLATES_URL} for the complete list of templates." ) selected_template = console.ask( "Enter a valid template name", show_choices=False @@ -1484,7 +1511,7 @@ def fetch_and_prompt_with_remote_templates( try: # Get the available templates available_templates = fetch_app_templates(constants.Reflex.VERSION) - + console.info(available_templates) if not show_prompt and template in available_templates: return template, available_templates @@ -1575,19 +1602,19 @@ def get_init_cli_prompt_options() -> list[Template]: Template( name=constants.Templates.DEFAULT, description="A blank Reflex app.", - demo_url="https://blank-template.reflex.run", + demo_url=constants.Templates.DEFAULT_TEMPLATE_URL, code_url="", ), Template( name=constants.Templates.AI, description="Generate a template using AI (Flexgen)", - demo_url="https://flexgen.reflex.run", + demo_url=constants.Templates.REFLEX_BUILD_FRONTEND, code_url="", ), Template( name=constants.Templates.CHOOSE_TEMPLATES, description="Choose an existing template.", - demo_url="https://reflex.dev/templates", + demo_url=constants.Templates.REFLEX_TEMPLATES_URL, code_url="", ), ]