Functions Framework for Python

3.10.1 · active · verified Thu Apr 09

Functions Framework for Python is an open-source FaaS (Function as a Service) framework designed for writing portable Python functions. It allows developers to test their Google Cloud Functions locally, run them in other serverless environments, or deploy them directly to Google Cloud. The current version is 3.10.1, and it maintains an active release cadence with frequent patch and minor updates, typically every 1-3 months.

Warnings

Install

Imports

Quickstart

Define an HTTP function `hello_http` in a file (e.g., `main.py`). The function receives a `flask.Request` object. Run it locally using the `functions-framework` CLI, specifying the function name with `--target`. By default, it runs on port 8080.

# main.py

def hello_http(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): The request object.
    Returns:
        The response text, or any set of values that can be turned into a Response object 
        using `make_response`.
    """
    request_json = request.get_json(silent=True)
    request_args = request.args

    if request_json and 'name' in request_json:
        name = request_json['name']
    elif request_args and 'name' in request_args:
        name = request_args['name']
    else:
        name = 'World'
    return f'Hello {name}!'

# To run locally:
# 1. Save this as main.py
# 2. Run from your terminal: functions-framework --target hello_http
# 3. Access at http://localhost:8080/

view raw JSON →