export.py 2.3 KB

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