From 6e1bce341288758972229772e664622604b4dfd5 Mon Sep 17 00:00:00 2001 From: Nikhil Rao Date: Fri, 3 Nov 2023 13:37:01 -0700 Subject: [PATCH] Prompt for template on reflex init (#2122) --- .../init-test/in_docker_test_script.sh | 2 +- reflex/reflex.py | 10 +++++-- reflex/utils/prerequisites.py | 29 +++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/integration/init-test/in_docker_test_script.sh b/integration/init-test/in_docker_test_script.sh index 6ac3af815..a0bfc4afd 100755 --- a/integration/init-test/in_docker_test_script.sh +++ b/integration/init-test/in_docker_test_script.sh @@ -12,4 +12,4 @@ cd /reflex-repo poetry install echo "Running reflex init in test project dir" export TELEMETRY_ENABLED=false -poetry run /bin/bash -c "cd ~/hello && reflex init && rm -rf ~/.reflex .web && reflex export --backend-only" \ No newline at end of file +poetry run /bin/bash -c "cd ~/hello && reflex init --template blank && rm -rf ~/.reflex .web && reflex export --backend-only" \ No newline at end of file diff --git a/reflex/reflex.py b/reflex/reflex.py index 058ce6c14..fdf760947 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -1,5 +1,7 @@ """Reflex CLI to create, run, and deploy apps.""" +from __future__ import annotations + import asyncio import atexit import json @@ -76,8 +78,8 @@ def main( def _init( name: str, - template: constants.Templates.Kind, - loglevel: constants.LogLevel, + template: constants.Templates.Kind | None = constants.Templates.Kind.BLANK, + loglevel: constants.LogLevel = config.loglevel, ): """Initialize a new Reflex app in the given directory.""" # Set the log level. @@ -98,6 +100,8 @@ def _init( # Set up the app directory, only if the config doesn't exist. if not os.path.exists(constants.Config.FILE): + if template is None: + template = prerequisites.prompt_for_template() prerequisites.create_config(app_name) prerequisites.initialize_app_directory(app_name, template) telemetry.send("init") @@ -120,7 +124,7 @@ def init( None, metavar="APP_NAME", help="The name of the app to initialize." ), template: constants.Templates.Kind = typer.Option( - constants.Templates.Kind.BLANK.value, + None, help="The template to initialize the app with.", ), loglevel: constants.LogLevel = typer.Option( diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index ed9f9c0ce..ec7d81ee1 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -705,6 +705,35 @@ def check_schema_up_to_date(): ) +def prompt_for_template() -> constants.Templates.Kind: + """Prompt the user to specify a template. + + Returns: + The template the user selected. + """ + # Show the user the URLs of each temlate to preview. + console.print("\nGet started with a template:") + console.print("blank (https://blank-template.reflex.run) - A minimal template.") + console.print( + "sidebar (https://sidebar-template.reflex.run) - A template with a sidebar to navigate pages." + ) + console.print("") + + # Prompt the user to select a template. + template = console.ask( + "Which template would you like to use?", + choices=[ + template.value + for template in constants.Templates.Kind + if template.value != "demo" + ], + default=constants.Templates.Kind.BLANK.value, + ) + + # Return the template. + return constants.Templates.Kind(template) + + def migrate_to_reflex(): """Migration from Pynecone to Reflex.""" # Check if the old config file exists.