This release is a pre-release and may not be stable for production use.
Functions Framework for Python
This project is inspired by GCP functions-framework-python .
An open source FaaS (Function as a service) framework for writing portable Python functions.
The Functions Framework lets you write lightweight functions that run in many different environments, including:
- OpenFunction
- Knative-based environments
- Dapr-based environments
- Your local development machine
The framework allows you to go from:
from functions_framework.context.user_context import UserContext
def hello(context: UserContext):
return "Hello world!"
To:
curl http://my-url
# Output: Hello world!
All without needing to worry about writing an HTTP server or complicated request handling logic.
Features
- Spin up a local development server for quick testing
- Invoke a function in response to a request
- Portable between serverless platforms
- Can integrate various data middlewares
Installation
Install the Functions Framework via pip:
pip install ofn-functions-framework
Or, for deployment, add the Functions Framework to your requirements.txt file:
ofn-functions-framework==0.2.0
Quickstarts
Quickstart: HTTP Function (Hello World)
Create an main.py file with the following contents:
from functions_framework.context.user_context import UserContext
def hello(context: UserContext):
# context.get_http_request()
return "Hello world!"
Export Function Context:
export FUNC_CONTEXT='{"name":"function_name","version":"v1","triggers":{"http":{"port":8080}}}'
Run the following command:
ff --source hello_world.py --target hello
Open http://localhost:8080/ in your browser and see Hello world!.
Or send requests to this function using curl from another terminal window:
curl localhost:8080
# Output: Hello world!
Run your function on OpenFunction
Besides Knative function support, one notable feature of OpenFunction is embracing Dapr system, so far Dapr pub/sub and bindings have been support.
Dapr bindings allows you to trigger your applications or services with events coming in from external systems, or interface with external systems. OpenFunction 0.6.0 release adds Dapr output bindings to its synchronous functions which enables HTTP triggers for asynchronous functions. For example, synchronous functions backed by the Knative runtime can now interact with middlewares defined by Dapr output binding or pub/sub, and an asynchronous function will be triggered by the events sent from the synchronous function.
Asynchronous function introduces Dapr pub/sub to provide a platform-agnostic API to send and receive messages. A typical use case is that you can leverage synchronous functions to receive an event in plain JSON or Cloud Events format, and then send the received event to a Dapr output binding or pub/sub component, most likely a message queue (e.g. Kafka, NATS Streaming, GCP PubSub, MQTT). Finally, the asynchronous function could be triggered from the message queue.
More details would be brought up to you in some quickstart samples, stay tuned.
Configure the Functions Framework
You can configure the Functions Framework using command-line flags or environment variables. If you specify both, the environment variable will be ignored.
| Command-line flag | Environment variable | Description |
|---|---|---|
--host |
HOST |
The host on which the Functions Framework listens for requests. Default: 0.0.0.0 |
--port |
PORT |
The port on which the Functions Framework listens for requests. Default: 8080 |
--target |
FUNCTION_TARGET |
The name of the exported function to be invoked in response to requests. Default: function |
--source |
FUNCTION_SOURCE |
The path to the file containing your function. Default: main.py (in the current working directory) |
--debug |
DEBUG |
A flag that allows to run functions-framework to run in debug mode, including live reloading. Default: False |
--dry-run |
DRY_RUN |
A flag that allows for testing the function build from the configuration without creating a server. Default: False |
Advanced Examples
More advanced guides can be found in the examples/ directory.
Metadata
Release files for ofn-functions-framework 0.2.0rc1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| ofn-functions-framework-0.2.0rc1.tar.gz | 20.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ofn_functions_framework-0.2.0rc1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 50.6 kB
Release files / ofn-functions-framework-0.2.0rc1.tar.gz
| Download URL | ofn-functions-framework-0.2.0rc1.tar.gz |
|---|---|
| Size | 20.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
59014353a54139eed5b4df381e40c168e303469b323b9a4997d33f925bc13d9f
|
|
BLAKE2b-256 checksum How to use checksums |
b5e47e201660fc55256aab18ff6daf5e6bd46bc83f6eb91f0c1ce12f56fb9f91
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.17
|
Release files / ofn_functions_framework-0.2.0rc1-py3-none-any.whl
| Download URL | ofn_functions_framework-0.2.0rc1-py3-none-any.whl |
|---|---|
| Size | 30.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1d0bb62bf68d35c8bae562a22048357b77a8c4b60332fcff5b950ae7b4a9675b
|
|
BLAKE2b-256 checksum How to use checksums |
1a64a682fb18fa95392809330c04befaefac1d28327c0b0d55027e839a59ca69
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.17
|
Security for ofn-functions-framework 0.2.0rc1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.