You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+76-20Lines changed: 76 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Fastly Compute Python SDK
2
2
3
-
Experimental Python SDK for [Fastly Compute](https://www.fastly.com/products/edge-compute) services
3
+
Experimental Python SDK for [Fastly Compute](https://www.fastly.com/products/edge-compute) services.
4
4
5
5
## Highlights
6
6
@@ -10,31 +10,87 @@ Experimental Python SDK for [Fastly Compute](https://www.fastly.com/products/edg
10
10
11
11
## Quick Start
12
12
13
-
Here's how to write your own Python WSGI app and run it on Fastly's edge network:
13
+
### Install Dependencies
14
14
15
-
1. Install the package that provides the Fastly Python build tool and gives you access to the Fastly API:
15
+
To work with `fastly-compute` in Python, you must install two system dependencies:
16
16
17
-
`pip install fastly-compute`
18
-
2. Make a project shaped like [our Flask example](https://github.com/fastly/compute-sdk-python/blob/main/examples/flask-app). You may find it easiest to clone the [repository](https://github.com/fastly/compute-sdk-python/), copy the `examples/flask-app` folder, and modify it. If you change the name of the top-level `.py` file, be sure to also update the entrypoint (`entry = "your_top_level_module_name"`) in `pyproject.toml`.
19
-
3. If your project imports from `Flask` or `bottle` like ours do, pip-install whichever of them you need:
20
-
```
21
-
pip install Flask
22
-
```
23
-
```
24
-
pip install Bottle
25
-
```
26
-
4.`cd your-project`
27
-
5. Install the [Fastly CLI](https://www.fastly.com/documentation/reference/tools/cli/) if you don't already have it.
28
-
6.`fastly compute init`
29
-
7. Say yes when warned "The current directory isn't empty." Answer "Other" when it asks for Language.
30
-
8. Add this to the bottom of `fastly.toml`:
17
+
1. The [Fastly CLI](https://www.fastly.com/documentation/reference/tools/cli/).
18
+
2. The [uv](https://docs.astral.sh/uv/getting-started/installation/) Python package manager.
19
+
20
+
Additional dependencies, including the Python SDK for compute and build tooling, will be installed and managed by `uv` in an isolated environment.
21
+
22
+
### Set Up Your Project
23
+
24
+
For this basic project, we'll use the Flask microframework. We will create our project and add the SDK and build tooling by adding `fastly-compute` and `flask`:
25
+
26
+
```console
27
+
$ uv init my-compute-service
28
+
...
29
+
$ cd my-compute-service
30
+
$ uv add fastly-compute flask
31
+
...
31
32
```
33
+
34
+
### Write Your Service
35
+
36
+
`uv init` automatically creates a `main.py` file in your project directory. Replace its contents with the following Flask application code:
37
+
38
+
```python
39
+
import platform
40
+
from flask import Flask
41
+
from fastly_compute.wsgi import WsgiHttpIncoming
42
+
43
+
app = Flask(__name__)
44
+
45
+
46
+
@app.route("/")
47
+
defindex():
48
+
version = platform.python_version()
49
+
returnf"Hello from Python {version} on Fastly Compute!"
50
+
51
+
HttpIncoming = WsgiHttpIncoming(app)
52
+
```
53
+
54
+
### Configure Compute Service Entry Point
55
+
56
+
The `fastly-compute-py` build tool, provided as part of the `fastly-compute` package, needs to be told about the module containing the compute service we just created. We can do this by modifying the `pyproject.toml` as follows:
57
+
58
+
```toml
59
+
# Add to end of pyproject.toml
60
+
[tool.fastly-compute]
61
+
entry = "main"
62
+
```
63
+
64
+
Then, let's do a quick test to make sure we are able to build a WebAssembly (Wasm) component:
65
+
66
+
```console
67
+
$ uv run fastly-compute-py build
68
+
Building Python application for Fastly Compute...
69
+
Entry point: main
70
+
Output: bin/main.wasm
71
+
Resolving Python dependencies...
72
+
Componentizing Python application...
73
+
Composing final WebAssembly module...
74
+
Injecting Fastly metadata...
75
+
✓ Build complete: bin/main.wasm
76
+
```
77
+
78
+
### Test and Deploy Your Service Using the Fastly CLI
79
+
80
+
Now that we have the skeleton of our service, let's test it using the [Fastly CLI](https://www.fastly.com/documentation/reference/tools/cli/).
81
+
82
+
1. Run `fastly compute init`
83
+
2. Say yes when warned "The current directory isn't empty." Answer "Other" when it asks for Language.
84
+
3. Add this to the bottom of `fastly.toml`:
85
+
86
+
```toml
32
87
[scripts]
33
-
build = "fastly-compute-py build"
88
+
build = "uv run fastly-compute-py build"
34
89
```
35
90
36
-
9.`fastly compute build`
37
-
10.`fastly compute deploy`
91
+
With that in place, we can now run `fastly compute serve` to test our service locally. When you're ready, you can use the Fastly CLI to deploy the service to the production fleet and perform other actions.
92
+
93
+
See the [`examples/`](https://github.com/fastly/compute-sdk-python/examples/) directory for more examples.
0 commit comments