export.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. # Check that the app is initialized.
  42. prerequisites.check_initialized(frontend=frontend)
  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. # Update some parameters for export
  47. prerequisites.update_next_config(export=True)
  48. # Ensure module can be imported and app.compile() is called.
  49. prerequisites.get_app()
  50. # Set up .web directory and install frontend dependencies.
  51. build.setup_frontend(Path.cwd())
  52. # Export the app.
  53. build.export(
  54. backend=backend,
  55. frontend=frontend,
  56. zip=zipping,
  57. zip_dest_dir=zip_dest_dir,
  58. deploy_url=config.deploy_url,
  59. upload_db_file=upload_db_file,
  60. )
  61. # Post a telemetry event.
  62. telemetry.send("export")