-
Notifications
You must be signed in to change notification settings - Fork 62
Adds vendored packages #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should change these instructions to use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fastapi | ||
mikenomitch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| markupsafe | ||
| jinja2 |
| 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 |
Uh oh!
There was an error while loading. Please reload this page.