|
1 | | -# Vendoring Packages: FastAPI + Jinja2 Example |
| 1 | +# Python Workers: FastMCP Example |
2 | 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)* |
| 3 | +This is an example of a Python Worker that uses the FastMCP package. |
4 | 4 |
|
5 | | -This is an example of a Python Worker that uses a built-in package (FastAPI) with a vendored package (Jinja2). |
| 5 | +[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/ai/tree/main/demos/python-workers-mcp) |
6 | 6 |
|
7 | | -## Adding Packages |
| 7 | +>[!NOTE] |
| 8 | +>Due to the [size](https://developers.cloudflare.com/workers/platform/limits/#worker-size) of the Worker, this example can only be deployed if you're using the Workers Paid plan. Free plan users will encounter deployment errors because this Worker exceeds the 3MB size limit. |
8 | 9 |
|
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 | +## Adding Packages |
10 | 11 |
|
11 | 12 | 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 |
|
13 | 14 | ### Vendoring Packages |
14 | 15 |
|
15 | | -[//]: # (NOTE: when updating the instructions below, be sure to also update the vendoring.yml CI workflow) |
16 | | - |
17 | 16 | First, install Python3.12 and pip for Python 3.12. |
18 | 17 |
|
19 | 18 | *Currently, other versions of Python will not work - use 3.12!* |
20 | 19 |
|
21 | | -Then create a virtual environment and activate it from your shell: |
| 20 | +Then set up your local pyodide virtual environment: |
22 | 21 | ```console |
23 | | -python3.12 -m venv .venv |
24 | | -source .venv/bin/activate |
| 22 | +npm run build |
25 | 23 | ``` |
26 | 24 |
|
27 | | -Within our virtual environment, install the pyodide CLI: |
| 25 | +### Developing and Deploying |
| 26 | + |
| 27 | +To develop your Worker run: |
28 | 28 | ```console |
29 | | -.venv/bin/pip install pyodide-build |
30 | | -.venv/bin/pyodide venv .venv-pyodide |
| 29 | +npm run dev |
31 | 30 | ``` |
32 | 31 |
|
33 | | -Next, add packages to your vendor.txt file. Here we'll add jinja2 |
34 | | -``` |
35 | | -jinja2 |
| 32 | +To deploy your Worker run: |
| 33 | +```console |
| 34 | +npm run deploy |
36 | 35 | ``` |
37 | 36 |
|
38 | | -Lastly, add these packages to your source files at `src/vendor`. For any additional packages, re-run this command. |
| 37 | +### Testing |
| 38 | + |
| 39 | +To test run: |
39 | 40 | ```console |
40 | | -.venv-pyodide/bin/pip install -t src/vendor -r vendor.txt |
| 41 | +npm run test |
41 | 42 | ``` |
42 | 43 |
|
43 | | -### Using Vendored packages |
| 44 | +### Linting and Formatting |
44 | 45 |
|
45 | | -In your wrangler.toml, make the vendor directory available: |
| 46 | +This project uses Ruff for linting and formatting: |
46 | 47 |
|
47 | | -```toml |
48 | | -[[rules]] |
49 | | -globs = ["vendor/**"] |
50 | | -type = "Data" |
51 | | -fallthrough = true |
| 48 | +```console |
| 49 | +npm run lint |
52 | 50 | ``` |
53 | 51 |
|
54 | | -Now, you can import and use the packages: |
| 52 | +### IDE Integration |
55 | 53 |
|
56 | | -```python |
57 | | -import jinja2 |
58 | | -# ... etc ... |
59 | | -``` |
| 54 | +To have good autocompletions in your IDE simply select .venv-pyodide/bin/python as your IDE's interpreter. |
60 | 55 |
|
61 | | -### Developing and Deploying |
| 56 | +You should also install your dependencies for type hints. |
62 | 57 |
|
63 | | -To develop your Worker, run `npx wrangler@latest dev`. |
64 | | - |
65 | | -To deploy your Worker, run `npx wrangler@latest deploy`. |
| 58 | +```console |
| 59 | +.venv-pyodide/bin/pip install -r requirements-dev.txt |
| 60 | +``` |
0 commit comments