Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions 06-vendoring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Vendoring Packages: FastAPI + Jinja2 Example

*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)*

This is an example of a Python Worker that uses a built-in package (FastAPI) with a vendored package (Jinja2).

## Adding Packages

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.

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.

### Vendoring Packages

First, install Python3.12 and pip for Python 3.12.

*Currently, other versions of Python will not work - use 3.12!*

Then create a virtual environment and activate it from your shell:
```console
python3.12 -m venv .venv
source .venv/bin/activate
Copy link
Contributor

@hoodmane hoodmane Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these instructions are intended to be mac/linux specific? Maybe we can later add an OS picker that shows windows instructions... Though none of this is tested on windows at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should change these instructions to use uv? it makes installing a specific python version really easy (https://docs.astral.sh/uv/guides/install-python/#installing-a-specific-version) so should make it much easier for people to get this going.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No objection.

```

Within our virtual environment, install the pyodide CLI:
```console
.venv/bin/pip install pyodide-build
.venv/bin/pyodide venv .venv-pyodide
```

Next, add packages to your vendor.txt file. Here we'll add jinja2
```
jinja2
```

Lastly, add these packages to your source files at `src/vendor`. For any additional packages, re-run this command.
```console
.venv-pyodide/bin/pip install -t src/vendor -r vendor.txt
```

### Using Vendored packages

In your wrangler.toml, make the vendor directory available:

```toml
[[rules]]
globs = ["vendor/**"]
type = "Data"
fallthrough = true
```

Now, you can import and use the packages:

```python
import jinja2
# ... etc ...
```

### Developing and Deploying

To develop your Worker, run `wrangler dev`.

To deploy your Worker, run `wrangler deploy`.
1 change: 1 addition & 0 deletions 06-vendoring/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fastapi
35 changes: 35 additions & 0 deletions 06-vendoring/src/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import jinja2
from fastapi import FastAPI, Request

environment = jinja2.Environment()
template = environment.from_string("Hello, {{ name }}!")


async def on_fetch(request, env):
import asgi

return await asgi.fetch(app, request, env)


app = FastAPI()


@app.get("/")
async def root():
message = "This is an example of FastAPI with Jinja2 - go to /hi/<name> to see a template rendered"
return {"message": message}


@app.get("/hi/{name}")
async def say_hi(name: str):
message = template.render(name=name)
return {"message": message}


@app.get("/env")
async def env(req: Request):
env = req.scope["env"]
return {
"message": "Here is an example of getting an environment variable: "
+ env.MESSAGE
}
2 changes: 2 additions & 0 deletions 06-vendoring/vendor.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
markupsafe
jinja2
12 changes: 12 additions & 0 deletions 06-vendoring/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name = "fastapi-worker"
main = "src/worker.py"
compatibility_flags = ["python_workers"]
compatibility_date = "2025-03-16"

[vars]
MESSAGE = "My env var"

[[rules]]
globs = ["vendor/**"]
type = "Data"
fallthrough = true
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ Need to deploy your Worker to Cloudflare? Python Workers are in open beta and ha
- [**`03-fastapi/`**](03-fastapi) — demonstrates how to use the [FastAPI](https://fastapi.tiangolo.com/) package with Python Workers
- [**`04-langchain/`**](04-langchain) — demonstrates how to use the [LangChain](https://pypi.org/project/langchain/) package with Python Workers
- [**`05-query-d1/`**](05-query-d1) - shows how to query D1 with Python Workers
- [**`06-vendoring/`**](06-vendring) - shows how to vendor packages that are not included in the built-in package list

## Open Beta and Limits

- 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.
- You must add the `python_workers` compatibility flag to your Worker, while Python Workers are in open beta.
- 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.
- If you wish to join our closed beta which enables deploying packages, fill out [this form](https://forms.gle/827KAtKJTZdgAkNm7).
- You must add the `python_workers` compatibility flag to your Worker while Python Workers are in open beta.

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.

Expand Down