Warning
Currently in alpha phase so expect bugs but do report them please. π
- Zero dependencies πͺΆ
- Auto watch when in dev mode. π
- Doesn't require NodeJS and NPM. π«§πͺ₯
- Seemless integration into the FastAPI codebase. π₯
- GZIP automatically configured to compress TailwindCSS out of the box. β‘
Note
These instructions assume you already have a somewhat intermediate understanding of FastAPI and that you've used TailwindCSS before (if you haven't be sure to read the documentation pages I link below).
If you're using Windows you can still replicate these commands via a file explorer.
- Install the pypi package.
pip install fastapi-tailwind
- Edit your FastAPI APP.
Before:
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
app = FastAPI()
@app.get("/")
def index():
return FileResponse("./index.html")
app.mount("/static", StaticFiles(directory = "static"), name = "static")
After:
# main.py
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi_tailwind import tailwind
from contextlib import asynccontextmanager
static_files = StaticFiles(directory = "static")
@asynccontextmanager
async def lifespan(app: FastAPI):
# YAY, our tailwind get's compiled here! π
process = tailwind.compile(
static_files.directory + "/output.css",
tailwind_stylesheet_path = "./input.css"
)
yield # The code after this is called on shutdown.
process.terminate() # We must terminate the compiler on shutdown to
# prevent multiple compilers running in development mode or when watch is enabled.
app = FastAPI(
# See the fastapi documentation for an explanation on lifespans: https://fastapi.tiangolo.com/advanced/events/
lifespan = lifespan
)
@app.get("/")
def index():
return FileResponse("./index.html")
# We need somewhere to drop the compiled stylesheet so our html file can link it.
app.mount("/static", static_files, name = "static")
- Make sure the
static
folder exists and create ainput.css
file (in v4 this is now used for configuration).
mkdir ./static
touch input.css
- Open
input.css
and write@import "tailwindcss;
in the file or just run this command:
echo '@import "tailwindcss";' > input.css
There is currently a bug in the vscode tailwindcss extension where you will not get any intellisense in v4 unless we add back the old v3
tailwind.config.js
file to point to the files with tailwind code in them (e.g. html, markdown, javascript files).A simple, quick and minimal way to fix this for the time being, is to create a file located at
.vscode/settings.json
where ourinput.css
file is (should be in root if you followed my previous instructions) and configure it like so:{ "tailwindCSS.experimental.configFile": "./input.css" }That should fix that issue.
- Now write your tailwind css in your
index.html
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>β¨ Tailwind in π₯ FastAPI</title>
<link rel="stylesheet" href="/static/output.css">
</head>
<body class="bg-slate-950">
<h1 class="mt-10 text-center text-6xl font-bold text-red-400">π Hello β¨ Tailwind!</h1>
</body>
</html>
- Then run FastAPI and visit your site. It should be gucci. β¨
fastapi dev main.py