Skip to content

Commit ef7b307

Browse files
authored
Merge uv-based quick-start rework. (#108)
2 parents e013011 + 546e833 commit ef7b307

1 file changed

Lines changed: 76 additions & 20 deletions

File tree

README.md

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Fastly Compute Python SDK
22

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.
44

55
## Highlights
66

@@ -10,31 +10,87 @@ Experimental Python SDK for [Fastly Compute](https://www.fastly.com/products/edg
1010

1111
## Quick Start
1212

13-
Here's how to write your own Python WSGI app and run it on Fastly's edge network:
13+
### Install Dependencies
1414

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:
1616

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+
...
3132
```
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+
def index():
48+
version = platform.python_version()
49+
return f"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
3287
[scripts]
33-
build = "fastly-compute-py build"
88+
build = "uv run fastly-compute-py build"
3489
```
3590

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.
3894

3995
## Run Some Examples on Your Own Machine
4096

0 commit comments

Comments
 (0)