Skip to main content

Tailwind CSS integration for Starlette apps

Project description

Starlette Tailwind CSS

starlette-tailwindcss is a lightweight utility for Starlette that builds Tailwind CSS on startup with optional watch mode during development.

It integrates directly with your Starlette app and provides:

  • Builds CSS on startup.
  • Automatically rebuilds on changes in watch mode.
  • Optional tailwindcss CLI binary auto-installation.
  • Fully typed, following Starlette patterns.

Installation

uv add starlette-tailwindcss
# or
pip install starlette-tailwindcss

Example

from contextlib import asynccontextmanager
from pathlib import Path

from starlette.applications import Starlette
from starlette.routing import Mount
from starlette.staticfiles import StaticFiles

from starlette_tailwindcss import tailwind

static_dir = Path(__file__).parent / "static"

@asynccontextmanager
async def lifespan(app: Starlette):
    async with tailwind(
        watch=app.debug,
        version="v4.2.2",
        input="src/acme/web/style.css",
        output=static_dir / "css" / "output.{build_id}.css",
    ) as assets:
        app.state.tailwind = assets
        yield

routes = [
    Mount("/static", app=StaticFiles(directory=static_dir), name="static"),
]

app = Starlette(
    debug=True,
    routes=routes,
    lifespan=lifespan,
)

Use the generated CSS file in your templates:

<link
    rel="stylesheet"
    href="{{ url_for('static', path='css/' ~ request.app.state.tailwind.file_name) }}"
/>

How it works

starlette-tailwindcss runs the Tailwind CLI alongside your app.

  • Builds CSS when the app starts.
  • Rebuilds CSS in watch mode during development.
  • Stops the process when the app shuts down.

Usage

Use an existing Tailwind CSS CLI binary:

async with tailwind(
    watch=app.debug,
    bin_path="/usr/local/bin/tailwindcss",
    input="src/acme/web/style.css",
    output=static_dir / "css" / "output.css",
):
    ...

Or let the package download a release automatically:

async with tailwind(
    watch=app.debug,
    version="v4.2.2",
    input="src/acme/web/style.css",
    output=static_dir / "css" / "output.css",
):
    ...

bin_path and version are mutually exclusive.

If output includes {build_id}, the startup build writes a unique file name on each app start. That makes cache pruning straightforward in production:

async with tailwind(
    watch=app.debug,
    version="v4.2.2",
    input="src/acme/web/style.css",
    output=static_dir / "css" / "output.{build_id}.css",
):
    ...

Debug logging

To see Tailwind CSS CLI output:

import logging

logging.basicConfig(level=logging.INFO)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_tailwindcss-0.2.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_tailwindcss-0.2.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file starlette_tailwindcss-0.2.0.tar.gz.

File metadata

  • Download URL: starlette_tailwindcss-0.2.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for starlette_tailwindcss-0.2.0.tar.gz
Algorithm Hash digest
SHA256 357d486cd59826705401a7b573451622951bcd8891803a9b71c9c58d990f5b71
MD5 01d1a99a32819a6c7f0842349fad693f
BLAKE2b-256 77738b7b72f0e6dfd43efa8c737856d3523a01e9aeaf9396e4617e9e709617b5

See more details on using hashes here.

File details

Details for the file starlette_tailwindcss-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: starlette_tailwindcss-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for starlette_tailwindcss-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1dde40f69f3d933a89faa1c948c7b5f3a26eca18bd9f57f731ce37d8d461d622
MD5 4ed642a2bc28feafd83c96e18fbee922
BLAKE2b-256 ad1da889cdb25f1c1162c5db597a00015369bc42fdb09143c56b09077e185abc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page