export.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. # Export the app.
  49. build.export(
  50. backend=backend,
  51. frontend=frontend,
  52. zip=zipping,
  53. zip_dest_dir=zip_dest_dir,
  54. deploy_url=config.deploy_url,
  55. upload_db_file=upload_db_file,
  56. )
  57. # Post a telemetry event.
  58. telemetry.send("export")