Skip to content

devlanes is in beta and not ready for production use.Report an issue

Python and FastAPI

Two FastAPI apps run with uv. web renders Jinja2 pages under uvicorn and calls quotes over HTTPS with httpx, which trusts the devlanes CA through SSL_CERT_FILE.

  • devlanes, with devlanes setup run once
  • uv and Python 3.12
Terminal window
cd languages-python-fastapi
devlanes start

devlanes start --no-ui prints the start as text:

example-python-fastapi@0 (main): 2/2 ready
ready web http://127.0.0.1:20591 https://example-python-fastapi.localhost
ready quotes http://127.0.0.1:20590 https://quotes.example-python-fastapi.localhost

Open https://example-python-fastapi.localhost. The page shows lane 0 of example-python-fastapi, the request.url uvicorn saw, the answer of the lane’s quotes service, and whether your browser’s own call to quotes passed CORS.

  1. On a lane’s first start the sync task (when: lane.create) runs uv sync --frozen, which builds .venv from uv.lock. Later starts skip it.
  2. web runs uv run uvicorn acme.web.main:app with --port ${{ self.port }} and serves https://example-python-fastapi.localhost. quotes runs uv run quotes, the entry point in [project.scripts], which reads PORT and HOST, and serves https://quotes.example-python-fastapi.localhost.
  3. Each service counts as ready once /healthz answers. The 3 minute timeout leaves room for uv run to sync again after a uv.lock change.

devlanes detects the python platform from pyproject.toml and gives both services SSL_CERT_FILE and REQUESTS_CA_BUNDLE (system roots plus the devlanes CA) and PYTHONUNBUFFERED=1. web gets QUOTES_URL from ${{ services.quotes.public_url }}, so its httpx call goes through the proxy over HTTPS. The proxy sends X-Forwarded-Proto: https and --proxy-headers makes uvicorn use it, so request.url and the page’s url_for links start with https://.

  • Start a second lane: git worktree add ../examples-checkout -b feat/checkout, then cd ../examples-checkout/languages-python-fastapi && devlanes start. https://example-python-fastapi1.localhost says lane 1, and its quotes answer comes from lane 1 too.

  • Without a worktree, devlanes lane new --name example-python-fastapi-review --start gives this folder a second lane, and devlanes lane clean example-python-fastapi-review --yes removes it.

  • devlanes env show web --explain lists SSL_CERT_FILE, REQUESTS_CA_BUNDLE and PYTHONUNBUFFERED in the contributor layer. Your own shell does not have them, so the same httpx call from there fails with httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED]:

    Terminal window
    uv run python -c "import httpx; print(httpx.get('https://quotes.example-python-fastapi.localhost/healthz').text)"
  • Change the footer in src/acme/web/templates/base.html and reload the page: Jinja reads the template again without a restart.

  • Change src/acme/web/main.py and --reload restarts the server; devlanes logs web --tail 10 shows StatReload detected changes in 'src\acme\web\main.py'. Reloading... (on Windows).

  • quotes sends CORS headers only to this lane’s origins. This prints access-control-allow-origin: https://example-python-fastapi.localhost, and prints nothing with lane 1’s origin, https://example-python-fastapi1.localhost (on Windows add --ssl-revoke-best-effort):

    Terminal window
    curl -sS -o /dev/null -D - -H "Origin: https://example-python-fastapi.localhost" https://quotes.example-python-fastapi.localhost/api/quotes | grep -i access-control
  • devlanes.yaml: the sync task, web as a run string, quotes as a run_script, each with hosts and an http ready check.
  • pyproject.toml: the pinned dependencies and the quotes entry point.
  • src/acme/settings.py: every variable the apps read, parsed once.
  • src/acme/web/: the page, the httpx call in quotes_client.py and the Jinja templates.
  • src/acme/quotes/: the API, with CORS from DEVLANES_ORIGINS and the main() that uv run quotes calls.