Cloud Run Hello World Explained
Flask
Flask is a micro web framework written in Python.
A Backend framework plays a crucial role in building robust and efficient web applications.
It is a lightweight WSGI web application.
Framework designed to get started quickly and scale applications.
A web framework is designed to support the development of web applications.
import os
The import os library allows for the python program to interact with the Operating System.
from flask import Flask
This pulls in the Flask class for use.
app = Flask(__name__)
Creates an "instance" of the Flask.
The __name__ argument helps determine the root path of the application.
__name__ is a built-in variable that evaluates name of the current module.
@app.route("/")
the route() decorator to bind a function to a URL.
Routing is mapping the URL directly to the code.
When a user clicks the URL it triggers the function.
def hello_world():
return f"Hello Google World!"
Function that runs and returns the string "Hello Google World!" in your browser.
if __name__ == "__main__":
'__main__' is the name of the scope in which top-level code executes. Ensures the server will start. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.
port = int(os.environ.get("PORT", 8080))
Sets the communication port to use and checks the operating system for port availability.
app.run(debug=True, host="0.0.0.0", port=port)
Launch the local server. Enables debug mode. Tells server to listen to all available network interfaces.
App - The core web application object.
@app.route - Maps a URL path to a Python function.
App.run - Starts the actual web server.
Os.environ - Fetches configuration from the system environment.
App.run - Starts the actual web server.
requirements.txt file is used for specifying what python packages are required to run the project you are looking at.
Flask==3.0.0
gunicorn==21.2.0
Gunicorn (Green Unicorn) translates requests from the internet so your Python web application can understand.
$ gcloud run deploy python-hello-world --source . --allow-unauthenticated --region us-central1
Instructs Cloud run to build a container using the python code and host it on the web.
gcloud run deploy - create a service on Cloud Run.
Python-hello-world - Name of your service, part of URL.
--source - Tells process to look at the current directory (.)
--allow-unauthenticated - Allows all users to access service.
--region us-central1 - This makes your service public.
Command will:
Build the Image: Package your Python code into a Docker container.
Stores the Image: saves that package in the Artifact Registry.
Deploys: starts an instance of that container.
Provides a URL: Gives you a generated HTTPS link.
No comments:
Post a Comment