diff --git a/reflex/reflex.py b/reflex/reflex.py index b8a4ed03a..224bace47 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -92,16 +92,30 @@ def _init( # Set up the web project. prerequisites.initialize_frontend_dependencies() - # Check if AI is requested and redirect the user to reflex.build. + # Integrate with reflex.build. + generation_hash = None if ai: - prerequisites.initialize_app(app_name, template=constants.Templates.DEFAULT) - generation_hash = redir.reflex_build_redirect() + if template is None: + # If AI is requested and no template specified, redirect the user to reflex.build. + generation_hash = redir.reflex_build_redirect() + elif prerequisites.is_generation_hash(template): + # Otherwise treat the template as a generation hash. + generation_hash = template + else: + console.error( + "Cannot use `--template` option with `--ai` option. Please remove `--template` option." + ) + raise typer.Exit(2) + template = constants.Templates.DEFAULT + + # Initialize the app. + prerequisites.initialize_app(app_name, template) + + # If a reflex.build generation hash is available, download the code and apply it to the main module. + if generation_hash: prerequisites.initialize_main_module_index_from_generation( app_name, generation_hash=generation_hash ) - else: - # Initialize the app. - prerequisites.initialize_app(app_name, template) # Migrate Pynecone projects to Reflex. prerequisites.migrate_to_reflex() diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index c8455f259..40a2338d3 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -1598,3 +1598,15 @@ def is_windows_bun_supported() -> bool: and cpu_info.model_name is not None and "ARM" not in cpu_info.model_name ) + + +def is_generation_hash(template: str) -> bool: + """Check if the template looks like a generation hash. + + Args: + template: The template name. + + Returns: + True if the template is composed of 32 or more hex characters. + """ + return re.match(r"^[0-9a-f]{32,}$", template) is not None diff --git a/reflex/utils/redir.py b/reflex/utils/redir.py index 461f055bb..1dbd989e9 100644 --- a/reflex/utils/redir.py +++ b/reflex/utils/redir.py @@ -11,7 +11,7 @@ from . import console def open_browser_and_wait( - target_url: str, poll_url: str, interval: int = 1 + target_url: str, poll_url: str, interval: int = 2 ) -> httpx.Response: """Open a browser window to target_url and request poll_url until it returns successfully. @@ -27,10 +27,14 @@ def open_browser_and_wait( console.warn( f"Unable to automatically open the browser. Please navigate to {target_url} in your browser." ) - console.info("Complete the workflow in the browser to continue.") - while response := httpx.get(poll_url, follow_redirects=True): - if response.is_success: - break + console.info("[b]Complete the workflow in the browser to continue.[/b]") + while True: + try: + response = httpx.get(poll_url, follow_redirects=True) + if response.is_success: + break + except httpx.RequestError as err: + console.info(f"Will retry after error occurred while polling: {err}.") time.sleep(interval) return response