minor format

This commit is contained in:
Elijah 2024-11-13 15:40:37 +00:00
parent 1b42eea1ed
commit aa4570175e

View File

@ -218,10 +218,10 @@ def get_install_package_manager(on_failure_return_none: bool = False) -> str | N
The path to the package manager. The path to the package manager.
""" """
if ( if (
constants.IS_WINDOWS constants.IS_WINDOWS
and not is_windows_bun_supported() and not is_windows_bun_supported()
or windows_check_onedrive_in_path() or windows_check_onedrive_in_path()
or windows_npm_escape_hatch() or windows_npm_escape_hatch()
): ):
return get_package_manager(on_failure_return_none) return get_package_manager(on_failure_return_none)
return str(get_config().bun_path) return str(get_config().bun_path)
@ -443,8 +443,8 @@ def create_config(app_name: str):
def initialize_gitignore( def initialize_gitignore(
gitignore_file: Path = constants.GitIgnore.FILE, gitignore_file: Path = constants.GitIgnore.FILE,
files_to_ignore: set[str] | list[str] = constants.GitIgnore.DEFAULTS, files_to_ignore: set[str] | list[str] = constants.GitIgnore.DEFAULTS,
): ):
"""Initialize the template .gitignore file. """Initialize the template .gitignore file.
@ -510,10 +510,10 @@ def initialize_requirements_txt():
def initialize_app_directory( def initialize_app_directory(
app_name: str, app_name: str,
template_name: str = constants.Templates.DEFAULT, template_name: str = constants.Templates.DEFAULT,
template_code_dir_name: str | None = None, template_code_dir_name: str | None = None,
template_dir: Path | None = None, template_dir: Path | None = None,
): ):
"""Initialize the app directory on reflex init. """Initialize the app directory on reflex init.
@ -687,7 +687,7 @@ def update_next_config(export=False, transpile_packages: Optional[List[str]] = N
def _update_next_config( def _update_next_config(
config: Config, export: bool = False, transpile_packages: Optional[List[str]] = None config: Config, export: bool = False, transpile_packages: Optional[List[str]] = None
): ):
next_config = { next_config = {
"basePath": config.frontend_path or "", "basePath": config.frontend_path or "",
@ -844,7 +844,7 @@ def install_bun():
# Skip if bun is already installed. # Skip if bun is already installed.
if Path(get_config().bun_path).exists() and get_bun_version() == version.parse( if Path(get_config().bun_path).exists() and get_bun_version() == version.parse(
constants.Bun.VERSION constants.Bun.VERSION
): ):
console.debug("Skipping bun installation as it is already installed.") console.debug("Skipping bun installation as it is already installed.")
return return
@ -943,16 +943,16 @@ def install_frontend_packages(packages: set[str], config: Config):
fallback_command = ( fallback_command = (
get_package_manager(on_failure_return_none=True) get_package_manager(on_failure_return_none=True)
if ( if (
not constants.IS_WINDOWS not constants.IS_WINDOWS
or constants.IS_WINDOWS or constants.IS_WINDOWS
and is_windows_bun_supported() and is_windows_bun_supported()
and not windows_check_onedrive_in_path() and not windows_check_onedrive_in_path()
) )
else None else None
) )
install_package_manager = ( install_package_manager = (
get_install_package_manager(on_failure_return_none=True) or fallback_command get_install_package_manager(on_failure_return_none=True) or fallback_command
) )
if install_package_manager is None: if install_package_manager is None:
@ -1178,8 +1178,8 @@ def check_db_initialized() -> bool:
True if alembic is initialized (or if database is not used). True if alembic is initialized (or if database is not used).
""" """
if ( if (
get_config().db_url is not None get_config().db_url is not None
and not environment.ALEMBIC_CONFIG.get().exists() and not environment.ALEMBIC_CONFIG.get().exists()
): ):
console.error( console.error(
"Database is not initialized. Run [bold]reflex db init[/bold] first." "Database is not initialized. Run [bold]reflex db init[/bold] first."
@ -1195,8 +1195,8 @@ def check_schema_up_to_date():
with model.Model.get_db_engine().connect() as connection: with model.Model.get_db_engine().connect() as connection:
try: try:
if model.Model.alembic_autogenerate( if model.Model.alembic_autogenerate(
connection=connection, connection=connection,
write_migration_scripts=False, write_migration_scripts=False,
): ):
console.error( console.error(
"Detected database schema changes. Run [bold]reflex db makemigrations[/bold] " "Detected database schema changes. Run [bold]reflex db makemigrations[/bold] "
@ -1380,7 +1380,9 @@ def create_config_init_app_from_remote_template(app_name: str, template_url: str
def prompt_templates(templates: dict[str, Template]) -> str: def prompt_templates(templates: dict[str, Template]) -> str:
while True: while True:
console.print("visit https://reflex.dev/templates for the complete list of templates.") console.print(
"visit https://reflex.dev/templates for the complete list of templates."
)
answer = console.ask("Enter a valid template name", show_choices=False) answer = console.ask("Enter a valid template name", show_choices=False)
if not answer in templates: if not answer in templates:
console.error("Invalid template name. Please try again.") console.error("Invalid template name. Please try again.")
@ -1402,7 +1404,9 @@ def use_ai_generation(template: str | None = None) -> str:
raise typer.Exit(2) raise typer.Exit(2)
def initialize_app(app_name: str, template: str | None = None, ai: bool = False) -> str | None: def initialize_app(
app_name: str, template: str | None = None, ai: bool = False
) -> str | None:
"""Initialize the app either from a remote template or a blank app. If the config file exists, it is considered as reinit. """Initialize the app either from a remote template or a blank app. If the config file exists, it is considered as reinit.
Args: Args:
@ -1431,7 +1435,9 @@ def initialize_app(app_name: str, template: str | None = None, ai: bool = False)
templates: dict[str, Template] = {} templates: dict[str, Template] = {}
# Don't fetch app templates if the user directly asked for DEFAULT. # Don't fetch app templates if the user directly asked for DEFAULT.
if template is not None and (template != constants.Templates.DEFAULT or template != constants.Templates.AI): if template is not None and (
template != constants.Templates.DEFAULT or template != constants.Templates.AI
):
try: try:
# Get the available templates # Get the available templates
templates = fetch_app_templates(constants.Reflex.VERSION) templates = fetch_app_templates(constants.Reflex.VERSION)
@ -1444,7 +1450,6 @@ def initialize_app(app_name: str, template: str | None = None, ai: bool = False)
template = template or constants.Templates.DEFAULT template = template or constants.Templates.DEFAULT
if template is None: if template is None:
template = prompt_for_template(get_init_cli_options()) template = prompt_for_template(get_init_cli_options())
if template == constants.Templates.AI: if template == constants.Templates.AI:
generation_hash = use_ai_generation() generation_hash = use_ai_generation()
@ -1453,9 +1458,15 @@ def initialize_app(app_name: str, template: str | None = None, ai: bool = False)
# Get the available templates # Get the available templates
templates = fetch_app_templates(constants.Reflex.VERSION) templates = fetch_app_templates(constants.Reflex.VERSION)
# default to the blank template if no templates are available # default to the blank template if no templates are available
template = prompt_templates(templates) if len(templates) > 0 else constants.Templates.DEFAULT template = (
prompt_templates(templates)
if len(templates) > 0
else constants.Templates.DEFAULT
)
except Exception as e: except Exception as e:
console.warn("Failed to fetch templates. Falling back to default template.") console.warn(
"Failed to fetch templates. Falling back to default template."
)
console.debug(f"Error while fetching templates: {e}") console.debug(f"Error while fetching templates: {e}")
template = constants.Templates.DEFAULT template = constants.Templates.DEFAULT
@ -1469,7 +1480,6 @@ def initialize_app(app_name: str, template: str | None = None, ai: bool = False)
create_config(app_name) create_config(app_name)
initialize_app_directory(app_name) initialize_app_directory(app_name)
else: else:
# If user selects a template, it needs to exist. # If user selects a template, it needs to exist.
if template in templates: if template in templates:
template_url = templates[template].code_url template_url = templates[template].code_url
@ -1500,7 +1510,9 @@ def initialize_app(app_name: str, template: str | None = None, ai: bool = False)
return template return template
def fetch_and_prompt_for_templates(template: str | None, templates: dict[str, Template]) -> str: def fetch_and_prompt_for_templates(
template: str | None, templates: dict[str, Template]
) -> str:
"""Fetches available templates and prompts the user if template is not specified.""" """Fetches available templates and prompts the user if template is not specified."""
try: try:
templates = fetch_app_templates(constants.Reflex.VERSION) templates = fetch_app_templates(constants.Reflex.VERSION)
@ -1514,11 +1526,24 @@ def fetch_and_prompt_for_templates(template: str | None, templates: dict[str, Te
def get_init_cli_options() -> list[Template]: def get_init_cli_options() -> list[Template]:
return [ return [
Template(name=constants.Templates.DEFAULT, description="A blank Reflex app.", demo_url="", code_url=""), Template(
Template(name=constants.Templates.AI, description="Generate a template using AI(Flexgen)", demo_url="https://flexgen.reflex.run", name=constants.Templates.DEFAULT,
code_url=""), description="A blank Reflex app.",
Template(name=constants.Templates.CHOOSE_TEMPLATES, description="Choose an existing template.", demo_url="https://reflex.dev/templates", demo_url="",
code_url=""), code_url="",
),
Template(
name=constants.Templates.AI,
description="Generate a template using AI(Flexgen)",
demo_url="https://flexgen.reflex.run",
code_url="",
),
Template(
name=constants.Templates.CHOOSE_TEMPLATES,
description="Choose an existing template.",
demo_url="https://reflex.dev/templates",
code_url="",
),
] ]
@ -1663,11 +1688,11 @@ def is_windows_bun_supported() -> bool:
""" """
cpu_info = get_cpu_info() cpu_info = get_cpu_info()
return ( return (
constants.IS_WINDOWS constants.IS_WINDOWS
and cpu_info is not None and cpu_info is not None
and cpu_info.address_width == 64 and cpu_info.address_width == 64
and cpu_info.model_name is not None and cpu_info.model_name is not None
and "ARM" not in cpu_info.model_name and "ARM" not in cpu_info.model_name
) )