export.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Export utilities."""
  2. from pathlib import Path
  3. from typing import Optional
  4. from reflex import constants
  5. from reflex.config import environment, get_config
  6. from reflex.utils import build, console, exec, prerequisites, telemetry
  7. config = get_config()
  8. def export(
  9. zipping: bool = True,
  10. frontend: bool = True,
  11. backend: bool = True,
  12. zip_dest_dir: str = str(Path.cwd()),
  13. upload_db_file: bool = False,
  14. api_url: Optional[str] = None,
  15. deploy_url: Optional[str] = None,
  16. env: constants.Env = constants.Env.PROD,
  17. loglevel: constants.LogLevel = console._LOG_LEVEL,
  18. ):
  19. """Export the app to a zip file.
  20. Args:
  21. zipping: Whether to zip the exported app. Defaults to True.
  22. frontend: Whether to export the frontend. Defaults to True.
  23. backend: Whether to export the backend. Defaults to True.
  24. zip_dest_dir: The directory to export the zip file to. Defaults to os.getcwd().
  25. upload_db_file: Whether to upload the database file. Defaults to False.
  26. api_url: The API URL to use. Defaults to None.
  27. deploy_url: The deploy URL to use. Defaults to None.
  28. env: The environment to use. Defaults to constants.Env.PROD.
  29. loglevel: The log level to use. Defaults to console._LOG_LEVEL.
  30. """
  31. # Set the log level.
  32. console.set_log_level(loglevel)
  33. # Set env mode in the environment
  34. environment.REFLEX_ENV_MODE.set(env)
  35. # Override the config url values if provided.
  36. if api_url is not None:
  37. config.api_url = str(api_url)
  38. console.debug(f"overriding API URL: {config.api_url}")
  39. if deploy_url is not None:
  40. config.deploy_url = str(deploy_url)
  41. console.debug(f"overriding deploy URL: {config.deploy_url}")
  42. # Show system info
  43. exec.output_system_info()
  44. # Compile the app in production mode and export it.
  45. console.rule("[bold]Compiling production app and preparing for export.")
  46. if frontend:
  47. # Ensure module can be imported and app.compile() is called.
  48. prerequisites.get_compiled_app(export=True)
  49. # Set up .web directory and install frontend dependencies.
  50. build.setup_frontend(Path.cwd())
  51. # Build the static app.
  52. if frontend:
  53. build.build(deploy_url=config.deploy_url, for_export=True)
  54. # Zip up the app.
  55. if zipping:
  56. build.zip_app(
  57. frontend=frontend,
  58. backend=backend,
  59. zip_dest_dir=zip_dest_dir,
  60. upload_db_file=upload_db_file,
  61. )
  62. # Post a telemetry event.
  63. telemetry.send("export")