pc.py 4.0 KB

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