Show status bar on reflex init (#1467)

* Show status bar on reflex init

* Fix tests

* Fix version

* Fix test
This commit is contained in:
Nikhil Rao 2023-07-30 20:45:34 -07:00 committed by GitHub
parent 068bcd906e
commit f01eff5b29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 27 deletions

View File

@ -86,7 +86,7 @@ def init(
prerequisites.initialize_gitignore() prerequisites.initialize_gitignore()
# Finish initializing the app. # Finish initializing the app.
console.success(f"Finished Initializing: {app_name}") console.success(f"Initialized {app_name}")
@cli.command() @cli.command()

View File

@ -11,8 +11,7 @@ from typing import Optional, Union
from reflex import constants from reflex import constants
from reflex.config import get_config from reflex.config import get_config
from reflex.utils import console, path_ops, prerequisites from reflex.utils import console, path_ops, prerequisites, processes
from reflex.utils.processes import new_process, show_progress
def update_json_file(file_path: str, update_dict: dict[str, Union[int, str]]): def update_json_file(file_path: str, update_dict: dict[str, Union[int, str]]):
@ -122,11 +121,11 @@ def export(
] ]
# Start the subprocess with the progress bar. # Start the subprocess with the progress bar.
process = new_process( process = processes.new_process(
[prerequisites.get_package_manager(), "run", command], [prerequisites.get_package_manager(), "run", command],
cwd=constants.WEB_DIR, cwd=constants.WEB_DIR,
) )
show_progress("Creating Production Build", process, checkpoints) processes.show_progress("Creating Production Build", process, checkpoints)
# Zip up the app. # Zip up the app.
if zip: if zip:
@ -192,7 +191,7 @@ def setup_frontend(
# Disable the Next telemetry. # Disable the Next telemetry.
if disable_telemetry: if disable_telemetry:
new_process( processes.new_process(
[ [
prerequisites.get_package_manager(), prerequisites.get_package_manager(),
"run", "run",

View File

@ -8,7 +8,6 @@ from pathlib import Path
from reflex import constants from reflex import constants
from reflex.config import get_config from reflex.config import get_config
from reflex.utils import console, prerequisites, processes from reflex.utils import console, prerequisites, processes
from reflex.utils.processes import new_process
from reflex.utils.watch import AssetFolderWatch from reflex.utils.watch import AssetFolderWatch
@ -30,7 +29,7 @@ def run_process_and_launch_url(
Args: Args:
run_command: The command to run. run_command: The command to run.
""" """
process = new_process( process = processes.new_process(
run_command, run_command,
cwd=constants.WEB_DIR, cwd=constants.WEB_DIR,
) )
@ -95,7 +94,7 @@ def run_backend(
port: The app port port: The app port
loglevel: The log level. loglevel: The log level.
""" """
new_process( processes.new_process(
[ [
"uvicorn", "uvicorn",
f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}", f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}",
@ -155,4 +154,4 @@ def run_backend_prod(
"--workers", "--workers",
str(num_workers), str(num_workers),
] ]
new_process(command, run=True, show_logs=True) processes.new_process(command, run=True, show_logs=True)

View File

@ -23,8 +23,7 @@ from redis import Redis
from reflex import constants, model from reflex import constants, model
from reflex.config import get_config from reflex.config import get_config
from reflex.utils import console, path_ops from reflex.utils import console, path_ops, processes
from reflex.utils.processes import new_process, show_logs, show_status
IS_WINDOWS = platform.system() == "Windows" IS_WINDOWS = platform.system() == "Windows"
@ -37,7 +36,7 @@ def check_node_version() -> bool:
""" """
try: try:
# Run the node -v command and capture the output. # Run the node -v command and capture the output.
result = new_process([constants.NODE_PATH, "-v"], run=True) result = processes.new_process([constants.NODE_PATH, "-v"], run=True)
except FileNotFoundError: except FileNotFoundError:
return False return False
@ -59,7 +58,7 @@ def get_bun_version() -> Optional[version.Version]:
""" """
try: try:
# Run the bun -v command and capture the output # Run the bun -v command and capture the output
result = new_process([constants.BUN_PATH, "-v"], run=True) result = processes.new_process([constants.BUN_PATH, "-v"], run=True)
return version.parse(result.stdout) # type: ignore return version.parse(result.stdout) # type: ignore
except FileNotFoundError: except FileNotFoundError:
return None return None
@ -277,12 +276,13 @@ def initialize_node():
install_node() install_node()
def download_and_run(url: str, *args, **env): def download_and_run(url: str, *args, show_status: bool = False, **env):
"""Download and run a script. """Download and run a script.
Args: Args:
url: The url of the script. url: The url of the script.
args: The arguments to pass to the script. args: The arguments to pass to the script.
show_status: Whether to show the status of the script.
env: The environment variables to use. env: The environment variables to use.
""" """
# Download the script # Download the script
@ -298,8 +298,9 @@ def download_and_run(url: str, *args, **env):
# Run the script. # Run the script.
env = {**os.environ, **env} env = {**os.environ, **env}
process = new_process(["bash", f.name, *args], env=env) process = processes.new_process(["bash", f.name, *args], env=env)
show_logs(f"Installing {url}", process) show = processes.show_status if show_status else processes.show_logs
show(f"Installing {url}", process)
def install_node(): def install_node():
@ -319,11 +320,11 @@ def install_node():
# Create the nvm directory and install. # Create the nvm directory and install.
path_ops.mkdir(constants.NVM_DIR) path_ops.mkdir(constants.NVM_DIR)
env = {**os.environ, "NVM_DIR": constants.NVM_DIR} env = {**os.environ, "NVM_DIR": constants.NVM_DIR}
download_and_run(constants.NVM_INSTALL_URL, **env) download_and_run(constants.NVM_INSTALL_URL, show_status=True, **env)
# Install node. # Install node.
# We use bash -c as we need to source nvm.sh to use nvm. # We use bash -c as we need to source nvm.sh to use nvm.
process = new_process( process = processes.new_process(
[ [
"bash", "bash",
"-c", "-c",
@ -331,7 +332,7 @@ def install_node():
], ],
env=env, env=env,
) )
show_logs("Installing node", process) processes.show_status("", process)
def install_bun(): def install_bun():
@ -366,20 +367,20 @@ def install_bun():
def install_frontend_packages(): def install_frontend_packages():
"""Installs the base and custom frontend packages.""" """Installs the base and custom frontend packages."""
# Install the base packages. # Install the base packages.
process = new_process( process = processes.new_process(
[get_install_package_manager(), "install", "--loglevel", "silly"], [get_install_package_manager(), "install", "--loglevel", "silly"],
cwd=constants.WEB_DIR, cwd=constants.WEB_DIR,
) )
show_status("Installing base frontend packages", process) processes.show_status("Installing base frontend packages", process)
# Install the app packages. # Install the app packages.
packages = get_config().frontend_packages packages = get_config().frontend_packages
if len(packages) > 0: if len(packages) > 0:
process = new_process( process = processes.new_process(
[get_install_package_manager(), "add", *packages], [get_install_package_manager(), "add", *packages],
cwd=constants.WEB_DIR, cwd=constants.WEB_DIR,
) )
show_status("Installing custom frontend packages", process) processes.show_status("Installing custom frontend packages", process)
def check_initialized(frontend: bool = True): def check_initialized(frontend: bool = True):

View File

@ -205,7 +205,7 @@ def show_status(message: str, process: subprocess.Popen):
""" """
with console.status(message) as status: with console.status(message) as status:
for line in stream_logs(message, process): for line in stream_logs(message, process):
status.update(f"{message}: {line}") status.update(f"{message} {line}")
def show_progress(message: str, process: subprocess.Popen, checkpoints: List[str]): def show_progress(message: str, process: subprocess.Popen, checkpoints: List[str]):

View File

@ -536,8 +536,8 @@ def test_node_install_unix(tmp_path, mocker):
mocker.patch("httpx.get", return_value=Resp()) mocker.patch("httpx.get", return_value=Resp())
download = mocker.patch("reflex.utils.prerequisites.download_and_run") download = mocker.patch("reflex.utils.prerequisites.download_and_run")
mocker.patch("reflex.utils.prerequisites.new_process") mocker.patch("reflex.utils.processes.new_process")
mocker.patch("reflex.utils.prerequisites.show_logs") mocker.patch("reflex.utils.processes.stream_logs")
prerequisites.install_node() prerequisites.install_node()