reflex 0.2.2__py3-none-any.whl → 0.2.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of reflex might be problematic. Click here for more details.

@@ -7,14 +7,15 @@ import json
7
7
  import os
8
8
  import platform
9
9
  import re
10
- import subprocess
11
10
  import sys
12
- from datetime import datetime
11
+ import tempfile
12
+ import threading
13
13
  from fileinput import FileInput
14
14
  from pathlib import Path
15
15
  from types import ModuleType
16
16
  from typing import Optional
17
17
 
18
+ import httpx
18
19
  import typer
19
20
  from alembic.util.exc import CommandError
20
21
  from packaging import version
@@ -22,30 +23,32 @@ from redis import Redis
22
23
 
23
24
  from reflex import constants, model
24
25
  from reflex.config import get_config
25
- from reflex.utils import console, path_ops
26
+ from reflex.utils import console, path_ops, processes
26
27
 
28
+ IS_WINDOWS = platform.system() == "Windows"
27
29
 
28
- def check_node_version(min_version=constants.MIN_NODE_VERSION):
29
- """Check the version of Node.js.
30
30
 
31
- Args:
32
- min_version: The minimum version of Node.js required.
31
+ def check_node_version() -> bool:
32
+ """Check the version of Node.js.
33
33
 
34
34
  Returns:
35
- Whether the version of Node.js is high enough.
35
+ Whether the version of Node.js is valid.
36
36
  """
37
37
  try:
38
- # Run the node -v command and capture the output
39
- result = subprocess.run(
40
- ["node", "-v"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
41
- )
42
- # The output will be in the form "vX.Y.Z", but version.parse() can handle it
43
- current_version = version.parse(result.stdout.decode())
44
- # Compare the version numbers
45
- return current_version >= version.parse(min_version)
46
- except Exception:
38
+ # Run the node -v command and capture the output.
39
+ result = processes.new_process([constants.NODE_PATH, "-v"], run=True)
40
+ except FileNotFoundError:
47
41
  return False
48
42
 
43
+ # The output will be in the form "vX.Y.Z", but version.parse() can handle it
44
+ current_version = version.parse(result.stdout) # type: ignore
45
+ # Compare the version numbers
46
+ return (
47
+ current_version >= version.parse(constants.NODE_VERSION_MIN)
48
+ if IS_WINDOWS
49
+ else current_version == version.parse(constants.NODE_VERSION)
50
+ )
51
+
49
52
 
50
53
  def get_bun_version() -> Optional[version.Version]:
51
54
  """Get the version of bun.
@@ -55,45 +58,56 @@ def get_bun_version() -> Optional[version.Version]:
55
58
  """
56
59
  try:
57
60
  # Run the bun -v command and capture the output
58
- result = subprocess.run(
59
- [os.path.expandvars(get_config().bun_path), "-v"],
60
- stdout=subprocess.PIPE,
61
- stderr=subprocess.PIPE,
62
- )
63
- return version.parse(result.stdout.decode().strip())
64
- except Exception:
61
+ result = processes.new_process([constants.BUN_PATH, "-v"], run=True)
62
+ return version.parse(result.stdout) # type: ignore
63
+ except FileNotFoundError:
65
64
  return None
66
65
 
67
66
 
68
- def get_package_manager() -> str:
69
- """Get the package manager executable.
67
+ def get_windows_package_manager() -> str:
68
+ """Get the package manager for windows.
70
69
 
71
70
  Returns:
72
- The path to the package manager.
71
+ The path to the package manager for windows.
73
72
 
74
73
  Raises:
75
74
  FileNotFoundError: If bun or npm is not installed.
76
- Exit: If the app directory is invalid.
77
-
78
75
  """
79
- config = get_config()
76
+ npm_path = path_ops.which("npm")
77
+ if npm_path is None:
78
+ raise FileNotFoundError("Reflex requires npm to be installed on Windows.")
79
+ return npm_path
80
80
 
81
- # Check that the node version is valid.
82
- if not check_node_version():
83
- console.print(
84
- f"[red]Node.js version {constants.MIN_NODE_VERSION} or higher is required to run Reflex."
85
- )
86
- raise typer.Exit()
81
+
82
+ def get_install_package_manager() -> str:
83
+ """Get the package manager executable for installation.
84
+ currently on unix systems, bun is used for installation only.
85
+
86
+ Returns:
87
+ The path to the package manager.
88
+ """
89
+ get_config()
87
90
 
88
91
  # On Windows, we use npm instead of bun.
89
- if platform.system() == "Windows" or config.disable_bun:
90
- npm_path = path_ops.which("npm")
91
- if npm_path is None:
92
- raise FileNotFoundError("Reflex requires npm to be installed on Windows.")
93
- return npm_path
92
+ if IS_WINDOWS:
93
+ return get_windows_package_manager()
94
94
 
95
95
  # On other platforms, we use bun.
96
- return os.path.expandvars(get_config().bun_path)
96
+ return constants.BUN_PATH
97
+
98
+
99
+ def get_package_manager() -> str:
100
+ """Get the package manager executable for running app.
101
+ currently on unix systems, npm is used for running the app only.
102
+
103
+ Returns:
104
+ The path to the package manager.
105
+ """
106
+ get_config()
107
+
108
+ if IS_WINDOWS:
109
+ return get_windows_package_manager()
110
+ return constants.NPM_PATH
97
111
 
98
112
 
99
113
  def get_app() -> ModuleType:
@@ -118,7 +132,7 @@ def get_redis() -> Optional[Redis]:
118
132
  if config.redis_url is None:
119
133
  return None
120
134
  redis_url, redis_port = config.redis_url.split(":")
121
- print("Using redis at", config.redis_url)
135
+ console.info(f"Using redis at {config.redis_url}")
122
136
  return Redis(host=redis_url, port=int(redis_port), db=0)
123
137
 
124
138
 
@@ -142,8 +156,20 @@ def get_default_app_name() -> str:
142
156
 
143
157
  Returns:
144
158
  The default app name.
159
+
160
+ Raises:
161
+ Exit: if the app directory name is reflex.
145
162
  """
146
- return os.getcwd().split(os.path.sep)[-1].replace("-", "_")
163
+ app_name = os.getcwd().split(os.path.sep)[-1].replace("-", "_")
164
+
165
+ # Make sure the app is not named "reflex".
166
+ if app_name == constants.MODULE_NAME:
167
+ console.error(
168
+ f"The app directory cannot be named [bold]{constants.MODULE_NAME}[/bold]."
169
+ )
170
+ raise typer.Exit()
171
+
172
+ return app_name
147
173
 
148
174
 
149
175
  def create_config(app_name: str):
@@ -157,24 +183,10 @@ def create_config(app_name: str):
157
183
 
158
184
  config_name = f"{re.sub(r'[^a-zA-Z]', '', app_name).capitalize()}Config"
159
185
  with open(constants.CONFIG_FILE, "w") as f:
186
+ console.debug(f"Creating {constants.CONFIG_FILE}")
160
187
  f.write(templates.RXCONFIG.render(app_name=app_name, config_name=config_name))
161
188
 
162
189
 
163
- def create_web_directory(root: Path) -> str:
164
- """Creates a web directory in the given root directory
165
- and returns the path to the directory.
166
-
167
- Args:
168
- root (Path): The root directory of the project.
169
-
170
- Returns:
171
- The path to the web directory.
172
- """
173
- web_dir = str(root / constants.WEB_DIR)
174
- path_ops.cp(constants.WEB_TEMPLATE_DIR, web_dir, overwrite=False)
175
- return web_dir
176
-
177
-
178
190
  def initialize_gitignore():
179
191
  """Initialize the template .gitignore file."""
180
192
  # The files to add to the .gitignore file.
@@ -184,8 +196,10 @@ def initialize_gitignore():
184
196
  if os.path.exists(constants.GITIGNORE_FILE):
185
197
  with open(constants.GITIGNORE_FILE, "r") as f:
186
198
  files |= set([line.strip() for line in f.readlines()])
199
+
187
200
  # Write files to the .gitignore file.
188
201
  with open(constants.GITIGNORE_FILE, "w") as f:
202
+ console.debug(f"Creating {constants.GITIGNORE_FILE}")
189
203
  f.write(f"{(path_ops.join(sorted(files))).lstrip()}")
190
204
 
191
205
 
@@ -232,112 +246,175 @@ def initialize_web_directory():
232
246
  json.dump(reflex_json, f, ensure_ascii=False)
233
247
 
234
248
 
235
- def validate_and_install_bun(initialize=True):
236
- """Check that bun version requirements are met. If they are not,
237
- ask user whether to install required version.
238
-
239
- Args:
240
- initialize: whether this function is called on `reflex init` or `reflex run`.
241
-
242
- Raises:
243
- Exit: If the bun version is not supported.
249
+ def initialize_bun():
250
+ """Check that bun requirements are met, and install if not."""
251
+ if IS_WINDOWS:
252
+ # Bun is not supported on Windows.
253
+ console.debug("Skipping bun installation on Windows.")
254
+ return
244
255
 
245
- """
256
+ # Check the bun version.
246
257
  bun_version = get_bun_version()
247
- if bun_version is not None and (
248
- bun_version < version.parse(constants.MIN_BUN_VERSION)
249
- or bun_version > version.parse(constants.MAX_BUN_VERSION)
250
- ):
251
- console.print(
252
- f"""[red]Bun version {bun_version} is not supported by Reflex. Please change your to bun version to be between {constants.MIN_BUN_VERSION} and {constants.MAX_BUN_VERSION}."""
258
+ if bun_version != version.parse(constants.BUN_VERSION):
259
+ console.debug(
260
+ f"Current bun version ({bun_version}) does not match ({constants.BUN_VERSION})."
253
261
  )
254
- action = console.ask(
255
- "Enter 'yes' to install the latest supported bun version or 'no' to exit.",
256
- choices=["yes", "no"],
257
- default="no",
258
- )
259
-
260
- if action == "yes":
261
- remove_existing_bun_installation()
262
- install_bun()
263
- return
264
- else:
265
- raise typer.Exit()
266
-
267
- if initialize:
262
+ remove_existing_bun_installation()
268
263
  install_bun()
269
264
 
270
265
 
271
266
  def remove_existing_bun_installation():
272
267
  """Remove existing bun installation."""
273
- package_manager = get_package_manager()
274
- if os.path.exists(package_manager):
275
- console.log("Removing bun...")
276
- path_ops.rm(os.path.expandvars(constants.BUN_ROOT_PATH))
268
+ console.debug("Removing existing bun installation.")
269
+ if os.path.exists(constants.BUN_PATH):
270
+ path_ops.rm(constants.BUN_ROOT_PATH)
271
+
272
+
273
+ def initialize_node():
274
+ """Validate nodejs have install or not."""
275
+ if not check_node_version():
276
+ install_node()
277
+
278
+
279
+ def download_and_run(url: str, *args, show_status: bool = False, **env):
280
+ """Download and run a script.
281
+
282
+ Args:
283
+ url: The url of the script.
284
+ args: The arguments to pass to the script.
285
+ show_status: Whether to show the status of the script.
286
+ env: The environment variables to use.
287
+ """
288
+ # Download the script
289
+ console.debug(f"Downloading {url}")
290
+ response = httpx.get(url)
291
+ if response.status_code != httpx.codes.OK:
292
+ response.raise_for_status()
293
+
294
+ # Save the script to a temporary file.
295
+ script = tempfile.NamedTemporaryFile()
296
+ with open(script.name, "w") as f:
297
+ f.write(response.text)
298
+
299
+ # Run the script.
300
+ env = {**os.environ, **env}
301
+ process = processes.new_process(["bash", f.name, *args], env=env)
302
+ show = processes.show_status if show_status else processes.show_logs
303
+ show(f"Installing {url}", process)
304
+
305
+
306
+ def install_node():
307
+ """Install nvm and nodejs for use by Reflex.
308
+ Independent of any existing system installations.
309
+
310
+ Raises:
311
+ Exit: if installation failed
312
+ """
313
+ # NVM is not supported on Windows.
314
+ if IS_WINDOWS:
315
+ console.error(
316
+ f"Node.js version {constants.NODE_VERSION} or higher is required to run Reflex."
317
+ )
318
+ raise typer.Exit()
319
+
320
+ # Create the nvm directory and install.
321
+ path_ops.mkdir(constants.NVM_DIR)
322
+ env = {**os.environ, "NVM_DIR": constants.NVM_DIR}
323
+ download_and_run(constants.NVM_INSTALL_URL, show_status=True, **env)
324
+
325
+ # Install node.
326
+ # We use bash -c as we need to source nvm.sh to use nvm.
327
+ process = processes.new_process(
328
+ [
329
+ "bash",
330
+ "-c",
331
+ f". {constants.NVM_DIR}/nvm.sh && nvm install {constants.NODE_VERSION}",
332
+ ],
333
+ env=env,
334
+ )
335
+ processes.show_status("", process)
277
336
 
278
337
 
279
338
  def install_bun():
280
339
  """Install bun onto the user's system.
281
340
 
282
341
  Raises:
283
- FileNotFoundError: if unzip or curl packages are not found.
342
+ FileNotFoundError: If required packages are not found.
284
343
  """
285
344
  # Bun is not supported on Windows.
286
- if platform.system() == "Windows":
287
- console.log("Skipping bun installation on Windows.")
345
+ if IS_WINDOWS:
346
+ console.debug("Skipping bun installation on Windows.")
288
347
  return
289
348
 
290
- # Only install if bun is not already installed.
291
- if not os.path.exists(get_package_manager()):
292
- console.log("Installing bun...")
293
-
294
- # Check if curl is installed
295
- curl_path = path_ops.which("curl")
296
- if curl_path is None:
297
- raise FileNotFoundError("Reflex requires curl to be installed.")
298
-
299
- # Check if unzip is installed
300
- unzip_path = path_ops.which("unzip")
301
- if unzip_path is None:
302
- raise FileNotFoundError("Reflex requires unzip to be installed.")
303
-
304
- os.system(constants.INSTALL_BUN)
349
+ # Skip if bun is already installed.
350
+ if os.path.exists(constants.BUN_PATH):
351
+ console.debug("Skipping bun installation as it is already installed.")
352
+ return
305
353
 
354
+ # if unzip is installed
355
+ unzip_path = path_ops.which("unzip")
356
+ if unzip_path is None:
357
+ raise FileNotFoundError("Reflex requires unzip to be installed.")
306
358
 
307
- def install_frontend_packages(web_dir: str):
308
- """Installs the base and custom frontend packages
309
- into the given web directory.
359
+ # Run the bun install script.
360
+ download_and_run(
361
+ constants.BUN_INSTALL_URL,
362
+ f"bun-v{constants.BUN_VERSION}",
363
+ BUN_INSTALL=constants.BUN_ROOT_PATH,
364
+ )
310
365
 
311
- Args:
312
- web_dir: The directory where the frontend code is located.
313
- """
314
- # Install the frontend packages.
315
- console.rule("[bold]Installing frontend packages")
316
366
 
367
+ def install_frontend_packages():
368
+ """Installs the base and custom frontend packages."""
317
369
  # Install the base packages.
318
- subprocess.run(
319
- [get_package_manager(), "install"],
320
- cwd=web_dir,
321
- stdout=subprocess.PIPE,
370
+ process = processes.new_process(
371
+ [get_install_package_manager(), "install", "--loglevel", "silly"],
372
+ cwd=constants.WEB_DIR,
322
373
  )
374
+ processes.show_status("Installing base frontend packages", process)
323
375
 
324
376
  # Install the app packages.
325
377
  packages = get_config().frontend_packages
326
378
  if len(packages) > 0:
327
- subprocess.run(
328
- [get_package_manager(), "add", *packages],
329
- cwd=web_dir,
330
- stdout=subprocess.PIPE,
379
+ process = processes.new_process(
380
+ [get_install_package_manager(), "add", *packages],
381
+ cwd=constants.WEB_DIR,
331
382
  )
383
+ processes.show_status("Installing custom frontend packages", process)
332
384
 
333
385
 
334
- def is_initialized() -> bool:
335
- """Check whether the app is initialized.
386
+ def check_initialized(frontend: bool = True):
387
+ """Check that the app is initialized.
336
388
 
337
- Returns:
338
- Whether the app is initialized in the current directory.
389
+ Args:
390
+ frontend: Whether to check if the frontend is initialized.
391
+
392
+ Raises:
393
+ Exit: If the app is not initialized.
339
394
  """
340
- return os.path.exists(constants.CONFIG_FILE) and os.path.exists(constants.WEB_DIR)
395
+ has_config = os.path.exists(constants.CONFIG_FILE)
396
+ has_reflex_dir = IS_WINDOWS or os.path.exists(constants.REFLEX_DIR)
397
+ has_web_dir = not frontend or os.path.exists(constants.WEB_DIR)
398
+
399
+ # Check if the app is initialized.
400
+ if not (has_config and has_reflex_dir and has_web_dir):
401
+ console.error(
402
+ f"The app is not initialized. Run [bold]{constants.MODULE_NAME} init[/bold] first."
403
+ )
404
+ raise typer.Exit()
405
+
406
+ # Check that the template is up to date.
407
+ if frontend and not is_latest_template():
408
+ console.error(
409
+ "The base app template has updated. Run [bold]reflex init[/bold] again."
410
+ )
411
+ raise typer.Exit()
412
+
413
+ # Print a warning for Windows users.
414
+ if IS_WINDOWS:
415
+ console.warn(
416
+ "We strongly advise using Windows Subsystem for Linux (WSL) for optimal performance with reflex."
417
+ )
341
418
 
342
419
 
343
420
  def is_latest_template() -> bool:
@@ -353,20 +430,40 @@ def is_latest_template() -> bool:
353
430
  return app_version == constants.VERSION
354
431
 
355
432
 
433
+ def initialize_frontend_dependencies():
434
+ """Initialize all the frontend dependencies."""
435
+ # Create the reflex directory.
436
+ path_ops.mkdir(constants.REFLEX_DIR)
437
+
438
+ # Install the frontend dependencies.
439
+ threads = [
440
+ threading.Thread(target=initialize_bun),
441
+ threading.Thread(target=initialize_node),
442
+ ]
443
+ for thread in threads:
444
+ thread.start()
445
+
446
+ # Set up the web directory.
447
+ initialize_web_directory()
448
+
449
+ # Wait for the threads to finish.
450
+ for thread in threads:
451
+ thread.join()
452
+
453
+
356
454
  def check_admin_settings():
357
455
  """Check if admin settings are set and valid for logging in cli app."""
358
456
  admin_dash = get_config().admin_dash
359
- current_time = datetime.now()
360
457
  if admin_dash:
361
458
  if not admin_dash.models:
362
- console.print(
363
- f"[yellow][Admin Dashboard][/yellow] :megaphone: Admin dashboard enabled, but no models defined in [bold magenta]rxconfig.py[/bold magenta]. Time: {current_time}"
459
+ console.log(
460
+ f"[yellow][Admin Dashboard][/yellow] :megaphone: Admin dashboard enabled, but no models defined in [bold magenta]rxconfig.py[/bold magenta]."
364
461
  )
365
462
  else:
366
- console.print(
367
- f"[yellow][Admin Dashboard][/yellow] Admin enabled, building admin dashboard. Time: {current_time}"
463
+ console.log(
464
+ f"[yellow][Admin Dashboard][/yellow] Admin enabled, building admin dashboard."
368
465
  )
369
- console.print(
466
+ console.log(
370
467
  "Admin dashboard running at: [bold green]http://localhost:8000/admin[/bold green]"
371
468
  )
372
469
 
@@ -378,8 +475,8 @@ def check_db_initialized() -> bool:
378
475
  True if alembic is initialized (or if database is not used).
379
476
  """
380
477
  if get_config().db_url is not None and not Path(constants.ALEMBIC_CONFIG).exists():
381
- console.print(
382
- "[red]Database is not initialized. Run [bold]reflex db init[/bold] first."
478
+ console.error(
479
+ "Database is not initialized. Run [bold]reflex db init[/bold] first."
383
480
  )
384
481
  return False
385
482
  return True
@@ -395,14 +492,14 @@ def check_schema_up_to_date():
395
492
  connection=connection,
396
493
  write_migration_scripts=False,
397
494
  ):
398
- console.print(
399
- "[red]Detected database schema changes. Run [bold]reflex db makemigrations[/bold] "
495
+ console.error(
496
+ "Detected database schema changes. Run [bold]reflex db makemigrations[/bold] "
400
497
  "to generate migration scripts.",
401
498
  )
402
499
  except CommandError as command_error:
403
500
  if "Target database is not up to date." in str(command_error):
404
- console.print(
405
- f"[red]{command_error} Run [bold]reflex db migrate[/bold] to update database."
501
+ console.error(
502
+ f"{command_error} Run [bold]reflex db migrate[/bold] to update database."
406
503
  )
407
504
 
408
505
 
@@ -421,7 +518,7 @@ def migrate_to_reflex():
421
518
  return
422
519
 
423
520
  # Rename pcconfig to rxconfig.
424
- console.print(
521
+ console.log(
425
522
  f"[bold]Renaming {constants.OLD_CONFIG_FILE} to {constants.CONFIG_FILE}"
426
523
  )
427
524
  os.rename(constants.OLD_CONFIG_FILE, constants.CONFIG_FILE)