Comments (6)
Install firebase-tools
:
$ npm install -g firebase-tools
Install the firestore emulator:
$ firebase setup:emulators:firestore
Write a basic "background" function:
def hello(data, context):
print(data, context)
Install the Functions Framework:
$ pip install functions-framework
In one terminal, start the Functions Framework (by default this will be on localhost:8080
):
$ functions-framework --target hello --signature-type event
In a second terminal, start the emulator (on a different port, localhost:8081
) and tell it which host/port to use for the functions emulator:
$ java -jar ~/.cache/firebase/emulators/cloud-firestore-emulator-v1.11.11.jar --functions_emulator localhost:8080 --port 8081
Register a trigger with the firestore emulator:
$ curl --location --request PUT 'http://localhost:8081/emulator/v1/projects/my-project/triggers/Test' \
--header 'Content-Type: application/json' \
--data-raw '{
"eventTrigger": {
"resource": "projects/my-project/databases/(default)/documents/my-collection/{id}",
"eventType": "providers/cloud.firestore/eventTypes/document.write",
"service": "firestore.googleapis.com"
}
}'
Install the Firestore client library (using Python here but it can be any client library):
$ pip install google-cloud-firestore
Write a script to trigger a write:
from google.cloud import firestore
client = firestore.Client(project="my-project")
client.collection("my-collection").add(document_data={"foo": "bar"})
In a third terminal, trigger a write:
$ export FIRESTORE_EMULATOR_HOST=localhost:8081 && python test.py
Observe the function being triggered by the emulator in the first terminal:
$ functions-framework --target hello --signature-type event
[2021-01-12 13:31:33 -0600] [22783] [INFO] Starting gunicorn 20.0.4
[2021-01-12 13:31:33 -0600] [22783] [INFO] Listening at: http://0.0.0.0:8080 (22783)
[2021-01-12 13:31:33 -0600] [22783] [INFO] Using worker: threads
[2021-01-12 13:31:33 -0600] [22800] [INFO] Booting worker with pid: 22800
{'value': {'name': 'projects/my-project/databases/(default)/documents/my-collection/LDtrTcJpPiy0V5TIg4p9', 'fields': {'foo': {'stringValue': 'bar'}}, 'createTime': '2021-01-12T20:35:29.425788Z', 'updateTime': '2021-01-12T20:35:29.425788Z'}, 'updateMask': {}} {event_id: 3299c6a0-3efa-4893-ad87-3dc59c78b084, timestamp: 2021-01-12T20:35:29.425Z, event_type: providers/cloud.firestore/eventTypes/document.write, resource: {'name': 'projects/my-project/databases/(default)/documents/my-collection/LDtrTcJpPiy0V5TIg4p9', 'service': 'firestore.googleapis.com'}}
from functions-framework-python.
Happy to help!
I can't currently test storage triggers, correct?
If you mean Cloud Storage for Firebase, correct. From https://firebase.google.com/docs/emulator-suite#feature-matrix:
the Cloud Functions emulator does not support background functions triggered by Auth or Cloud Storage for Firebase.
There's a feature request for this here: firebase/firebase-tools#1738
Can I start the emulator with
firebase emulators:start
and still register the python function? My hope is to test both my nodejs and python functions at the same time.
I'm not very familiar with the firebase emulator, but it seems like it's not possible to start the emulator via firebase emulators:start
and specify --functions_emulator
. In addition I think the emulated functions would all need to run on the same host/port.
This might be a good question for the firebase-tools
tag on Stack Overflow instead -- it doesn't really have anything to do with the Functions Framework at this point.
from functions-framework-python.
This is such a detailed reply. Thank you very much @di ! :)
from functions-framework-python.
I have 2 more questions.
- I can't currently test
storage
triggers, correct? - Can I start the emulator with
firebase emulators:start
and still register the python function? My hope is to test both my nodejs and python functions at the same time.
from functions-framework-python.
Hi @di
Thanks for guidelines, quite useful. However, I am facing an issue and I hope you can help me out
I followed your step-by-step instructions, but it is not working. When I checked the firebase-debug.log I find these logs:
[debug] [2021-03-08T23:18:40.086Z] Accepted request POST /functions/projects/{project Id}/triggers/Test --> Test {"metadata":{"emulator":{"name":"functions"},"message":"Accepted request POST /functions/projects/{project id}/triggers/Test --> Test"}}
[debug] [2021-03-08T23:18:40.086Z] Could not find key=Test in {}
[debug] [2021-03-08T23:18:40.088Z] RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined {"metadata":{"emulator":{"name":"functions"},"message":{"code":"ERR_HTTP_INVALID_STATUS_CODE"}}}
[debug] [2021-03-08T23:18:40.088Z] [work-queue] {"queueLength":0,"workRunningCount":0}
I think I am missing something about registering the trigger "Test" somewhere, the one is referring to in the PUT endpoint:
http://localhost:8081/emulator/v1/projects/my-project/triggers/Test
Is there any configuration missing?
Thanks in advance!
from functions-framework-python.
@lfpazmino Sorry, I can't really help with the Firebase emulator here. This might be a good issue for the https://github.com/firebase/firebase-tools/ repo or a question for the firebase-tools
tag on Stack Overflow instead.
from functions-framework-python.
Related Issues (20)
- Ability to pass runtime environment variables for local testing
- functions-framework does not use the sourced venv HOT 2
- Not able to run locally HOT 1
- Debug flag broken HOT 2
- Unit tests are failing
- Code behaves differently with debug flag HOT 2
- The framework default Gunicorn configs do not make sense for Gen2 environments HOT 5
- remove requirement for requirements.txt when specifying dependencies for deploying to functions: support poetry HOT 3
- Exception has occurred: SystemExit 3 HOT 6
- Symbolic linking within the Source Directory to include shared local directories.
- Firebase Functions Support HOT 1
- Possibility for multiple target/signature-type pairs?
- Gunicorn 21 is out HOT 3
- Flask Celery worker raise RuntimeError(unbound_message) from None RuntimeError: Working outside of request context. HOT 2
- Add Flask 3 support HOT 2
- README.md - Flask "Request" object hyperlink is broken HOT 2
- Native support for Pydantic models for function_framework.typed
- Document functions_framework.typed
- Support for postponed annnotations
- Improve developer experience for uncaught exceptions on GCP HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from functions-framework-python.