export.py 2.5 KB

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