pc.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """Pynecone CLI to create, run, and deploy apps."""
  2. import os
  3. from pathlib import Path
  4. import httpx
  5. import typer
  6. from pynecone import constants, utils
  7. # Create the app.
  8. cli = typer.Typer()
  9. @cli.command()
  10. def version():
  11. """Get the Pynecone version."""
  12. utils.console.print(constants.VERSION)
  13. @cli.command()
  14. def init():
  15. """Initialize a new Pynecone app in the current directory."""
  16. app_name = utils.get_default_app_name()
  17. # Make sure they don't name the app "pynecone".
  18. if app_name == constants.MODULE_NAME:
  19. utils.console.print(
  20. f"[red]The app directory cannot be named [bold]{constants.MODULE_NAME}."
  21. )
  22. raise typer.Exit()
  23. with utils.console.status(f"[bold]Initializing {app_name}"):
  24. # Set up the web directory.
  25. utils.install_bun()
  26. utils.initialize_web_directory()
  27. # Set up the app directory, only if the config doesn't exist.
  28. if not os.path.exists(constants.CONFIG_FILE):
  29. utils.create_config(app_name)
  30. utils.initialize_app_directory(app_name)
  31. # Initialize the .gitignore.
  32. utils.initialize_gitignore()
  33. # Finish initializing the app.
  34. utils.console.log(f"[bold green]Finished Initializing: {app_name}")
  35. @cli.command()
  36. def run(
  37. env: constants.Env = typer.Option(
  38. constants.Env.DEV, help="The environment to run the app in."
  39. ),
  40. frontend: bool = typer.Option(True, help="Whether to run the frontend."),
  41. backend: bool = typer.Option(True, help="Whether to run the backend."),
  42. loglevel: constants.LogLevel = typer.Option(
  43. constants.LogLevel.ERROR, help="The log level to use."
  44. ),
  45. ):
  46. """Run the app in the current directory."""
  47. # Check that the app is initialized.
  48. if frontend and not utils.is_initialized():
  49. utils.console.print(
  50. "[red]The app is not initialized. Run [bold]pc init[/bold] first."
  51. )
  52. raise typer.Exit()
  53. # Check that the template is up to date.
  54. if frontend and not utils.is_latest_template():
  55. utils.console.print(
  56. "[red]The base app template has updated. Run [bold]pc init[/bold] again."
  57. )
  58. raise typer.Exit()
  59. # Get the app module.
  60. utils.console.rule("[bold]Starting Pynecone App")
  61. app = utils.get_app()
  62. # Get the frontend and backend commands, based on the environment.
  63. frontend_cmd = backend_cmd = None
  64. if env == constants.Env.DEV:
  65. frontend_cmd, backend_cmd = utils.run_frontend, utils.run_backend
  66. if env == constants.Env.PROD:
  67. frontend_cmd, backend_cmd = utils.run_frontend_prod, utils.run_backend_prod
  68. assert frontend_cmd and backend_cmd, "Invalid env"
  69. # Run the frontend and backend.
  70. if frontend:
  71. frontend_cmd(app.app, Path.cwd())
  72. if backend:
  73. backend_cmd(app.__name__, loglevel=loglevel)
  74. @cli.command()
  75. def deploy(dry_run: bool = typer.Option(False, help="Whether to run a dry run.")):
  76. """Deploy the app to the Pynecone hosting service."""
  77. # Get the app config.
  78. config = utils.get_config()
  79. config.api_url = utils.get_production_backend_url()
  80. # Check if the deploy url is set.
  81. if config.deploy_url is None:
  82. typer.echo("This feature is coming soon!")
  83. return
  84. # Compile the app in production mode.
  85. typer.echo("Compiling production app")
  86. app = utils.get_app().app
  87. utils.export_app(app, zip=True)
  88. # Exit early if this is a dry run.
  89. if dry_run:
  90. return
  91. # Deploy the app.
  92. data = {"userId": config.username, "projectId": config.app_name}
  93. original_response = httpx.get(config.deploy_url, params=data)
  94. response = original_response.json()
  95. frontend = response["frontend_resources_url"]
  96. backend = response["backend_resources_url"]
  97. # Upload the frontend and backend.
  98. with open(constants.FRONTEND_ZIP, "rb") as f:
  99. response = httpx.put(frontend, data=f) # type: ignore
  100. with open(constants.BACKEND_ZIP, "rb") as f:
  101. response = httpx.put(backend, data=f) # type: ignore
  102. main = cli
  103. if __name__ == "__main__":
  104. main()