export.py 2.3 KB

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