From f5967ab85285e9ef10eae7d0a59266e741adc203 Mon Sep 17 00:00:00 2001 From: Mike Nomitch Date: Mon, 17 Mar 2025 10:48:30 -0700 Subject: [PATCH] Adds vendored packages --- 06-vendoring/README.md | 63 +++++++++++++++++++++++++++++++++++ 06-vendoring/requirements.txt | 1 + 06-vendoring/src/worker.py | 35 +++++++++++++++++++ 06-vendoring/vendor.txt | 2 ++ 06-vendoring/wrangler.toml | 12 +++++++ README.md | 6 ++-- 6 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 06-vendoring/README.md create mode 100644 06-vendoring/requirements.txt create mode 100644 06-vendoring/src/worker.py create mode 100644 06-vendoring/vendor.txt create mode 100644 06-vendoring/wrangler.toml diff --git a/06-vendoring/README.md b/06-vendoring/README.md new file mode 100644 index 0000000..ca4482a --- /dev/null +++ b/06-vendoring/README.md @@ -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 +``` + +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`. diff --git a/06-vendoring/requirements.txt b/06-vendoring/requirements.txt new file mode 100644 index 0000000..170703d --- /dev/null +++ b/06-vendoring/requirements.txt @@ -0,0 +1 @@ +fastapi \ No newline at end of file diff --git a/06-vendoring/src/worker.py b/06-vendoring/src/worker.py new file mode 100644 index 0000000..f140258 --- /dev/null +++ b/06-vendoring/src/worker.py @@ -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/ 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 + } diff --git a/06-vendoring/vendor.txt b/06-vendoring/vendor.txt new file mode 100644 index 0000000..2cea586 --- /dev/null +++ b/06-vendoring/vendor.txt @@ -0,0 +1,2 @@ +markupsafe +jinja2 \ No newline at end of file diff --git a/06-vendoring/wrangler.toml b/06-vendoring/wrangler.toml new file mode 100644 index 0000000..9dece33 --- /dev/null +++ b/06-vendoring/wrangler.toml @@ -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 diff --git a/README.md b/README.md index ad7b7c3..20d30bf 100644 --- a/README.md +++ b/README.md @@ -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.