Skip to content

Commit 4cecd49

Browse files
authored
Merge pull request #20 from cloudflare/mikenomitch/adds-vendoring
Adds vendored packages
2 parents f0a2878 + f5967ab commit 4cecd49

File tree

6 files changed

+117
-2
lines changed

6 files changed

+117
-2
lines changed

06-vendoring/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Vendoring Packages: FastAPI + Jinja2 Example
2+
3+
*Note: You must have Python Packages enabled on your account for built-in packages to work. Request Access to our Closed Beta using [This Form](https://forms.gle/FcjjhV3YtPyjRPaL8)*
4+
5+
This is an example of a Python Worker that uses a built-in package (FastAPI) with a vendored package (Jinja2).
6+
7+
## Adding Packages
8+
9+
Built-in packages can be selected from [this list](https://developers.cloudflare.com/workers/languages/python/packages/#supported-packages) and added to your `requirements.txt` file. These can be used with no other explicit install step.
10+
11+
Vendored packages are added to your source files and need to be installed in a special manner. The Python Workers team plans to make this process automatic in the future, but for now, manual steps need to be taken.
12+
13+
### Vendoring Packages
14+
15+
First, install Python3.12 and pip for Python 3.12.
16+
17+
*Currently, other versions of Python will not work - use 3.12!*
18+
19+
Then create a virtual environment and activate it from your shell:
20+
```console
21+
python3.12 -m venv .venv
22+
source .venv/bin/activate
23+
```
24+
25+
Within our virtual environment, install the pyodide CLI:
26+
```console
27+
.venv/bin/pip install pyodide-build
28+
.venv/bin/pyodide venv .venv-pyodide
29+
```
30+
31+
Next, add packages to your vendor.txt file. Here we'll add jinja2
32+
```
33+
jinja2
34+
```
35+
36+
Lastly, add these packages to your source files at `src/vendor`. For any additional packages, re-run this command.
37+
```console
38+
.venv-pyodide/bin/pip install -t src/vendor -r vendor.txt
39+
```
40+
41+
### Using Vendored packages
42+
43+
In your wrangler.toml, make the vendor directory available:
44+
45+
```toml
46+
[[rules]]
47+
globs = ["vendor/**"]
48+
type = "Data"
49+
fallthrough = true
50+
```
51+
52+
Now, you can import and use the packages:
53+
54+
```python
55+
import jinja2
56+
# ... etc ...
57+
```
58+
59+
### Developing and Deploying
60+
61+
To develop your Worker, run `wrangler dev`.
62+
63+
To deploy your Worker, run `wrangler deploy`.

06-vendoring/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fastapi

06-vendoring/src/worker.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import jinja2
2+
from fastapi import FastAPI, Request
3+
4+
environment = jinja2.Environment()
5+
template = environment.from_string("Hello, {{ name }}!")
6+
7+
8+
async def on_fetch(request, env):
9+
import asgi
10+
11+
return await asgi.fetch(app, request, env)
12+
13+
14+
app = FastAPI()
15+
16+
17+
@app.get("/")
18+
async def root():
19+
message = "This is an example of FastAPI with Jinja2 - go to /hi/<name> to see a template rendered"
20+
return {"message": message}
21+
22+
23+
@app.get("/hi/{name}")
24+
async def say_hi(name: str):
25+
message = template.render(name=name)
26+
return {"message": message}
27+
28+
29+
@app.get("/env")
30+
async def env(req: Request):
31+
env = req.scope["env"]
32+
return {
33+
"message": "Here is an example of getting an environment variable: "
34+
+ env.MESSAGE
35+
}

06-vendoring/vendor.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
markupsafe
2+
jinja2

06-vendoring/wrangler.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name = "fastapi-worker"
2+
main = "src/worker.py"
3+
compatibility_flags = ["python_workers"]
4+
compatibility_date = "2025-03-16"
5+
6+
[vars]
7+
MESSAGE = "My env var"
8+
9+
[[rules]]
10+
globs = ["vendor/**"]
11+
type = "Data"
12+
fallthrough = true

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ Need to deploy your Worker to Cloudflare? Python Workers are in open beta and ha
2020
- [**`03-fastapi/`**](03-fastapi) — demonstrates how to use the [FastAPI](https://fastapi.tiangolo.com/) package with Python Workers
2121
- [**`04-langchain/`**](04-langchain) — demonstrates how to use the [LangChain](https://pypi.org/project/langchain/) package with Python Workers
2222
- [**`05-query-d1/`**](05-query-d1) - shows how to query D1 with Python Workers
23+
- [**`06-vendoring/`**](06-vendring) - shows how to vendor packages that are not included in the built-in package list
2324

2425
## Open Beta and Limits
2526

26-
- Python Workers are in open beta. Currently, you can only deploy Python Workers that use the standard library. [Packages](https://developers.cloudflare.com/workers/languages/python/packages/#supported-packages) **cannot be deployed** and will only work in local development for the time being.
27-
- You must add the `python_workers` compatibility flag to your Worker, while Python Workers are in open beta.
27+
- Python Workers are in open beta. Currently, you can only deploy Python Workers that use the standard library. [Packages](https://developers.cloudflare.com/workers/languages/python/packages/#supported-packages) **cannot be deployed** and will only work in local development by default.
28+
- If you wish to join our closed beta which enables deploying packages, fill out [this form](https://forms.gle/827KAtKJTZdgAkNm7).
29+
- You must add the `python_workers` compatibility flag to your Worker while Python Workers are in open beta.
2830

2931
We’d love your feedback. Join the `#python-workers channel` in the [Cloudflare Developers Discord](https://discord.cloudflare.com/) and let us know what you’d like to see next.
3032

0 commit comments

Comments
 (0)