DEV Community

Omonire Oghene-Ovie Great
Omonire Oghene-Ovie Great

Posted on

FSCSS for Python: semantic CSS without utility classes

A practical, Python-first tutorial for Flask, Django, and FastAPI developers.

I maintain the Python wrapper around FSCSS. The language and compiler are Figsh's work; my part is making FSCSS feel like a normal Python tool: pip install, a Python API, a CLI, and framework helpers.

This is the guide I wanted when I started wrapping the compiler.


What we will build

By the end of this tutorial, you will have a small Flask app that:

  • stores styles in static/src/site.fscss
  • compiles them to static/src/site.css
  • uses variables, shorthands, and reusable mixins
  • recompiles automatically in debug mode
  • passes a CSS drift check in CI

The same patterns apply to Django, FastAPI, and plain Python scripts.


1. Install and verify

FSCSS requires:

  • Python 3.7+ in package metadata, with CI covering 3.8-3.12
  • Node.js 20+ on PATH

Node is still required today because the Python package vendors the official xfscss compiler and runs it for you. The Node requirement is an implementation detail I want to remove later; the public API is Python.

python -m venv .venv
pip install fscss flask
Enter fullscreen mode Exit fullscreen mode

Verify the install:

fscss --version
fscss doctor
Enter fullscreen mode Exit fullscreen mode

fscss doctor is read-only. It checks Python, your OS, Node, and the vendored compiler path without modifying anything.


2. Your first .fscss file

Create style.fscss:

$accent: #8f6bff;

body {
  bg: #0b0a13;
  color: $accent!;
  ff: system-ui, sans-serif;
}
Enter fullscreen mode Exit fullscreen mode

Compile it:

fscss style.fscss style.css
Enter fullscreen mode Exit fullscreen mode

You get plain CSS:

:root{--accent: #8f6bff;}
body {
background: #0b0a13;
color: #8f6bff;
font-family: sans-serif;
}
Enter fullscreen mode Exit fullscreen mode

Three things happened:

  1. $accent became a CSS custom property and was inlined where used.
  2. bg expanded to background.
  3. ff expanded to font-family.

That is the core idea: write compact, semantic source; ship normal CSS.


3. How FSCSS thinks

FSCSS is not a utility-class framework. Your HTML keeps semantic class names like card, panel, and badge.

Variables

$accent: #8f6bff;
$surface: #12121a;
$text: #f7f5ff;

body {
  bg: $surface!;
  color: $text!;
}
Enter fullscreen mode Exit fullscreen mode

Declare variables before the rules that use them. They are order-sensitive.

Shorthands

FSCSS CSS
px: 16px; padding-left and padding-right
py: 14px; padding-top and padding-bottom
mx: auto; margin-left and margin-right
max-w: 360px; max-width
rounded: 12px; border-radius
stack: 12px; flex column with gap
bg: #12121a; background
shadow: 0 8px 24px rgba(0,0,0,.35); box-shadow

Always terminate shorthand declarations with ;.

Use color, not c. The c: alias is not supported by this engine version.

Reusable blocks

@define center(elem){`
  @use(elem) {
    display: flex;
    justify-content: center;
    align-items: center;
  }
`}

@center(.hero)
Enter fullscreen mode Exit fullscreen mode

The backticks matter when the body contains braces. This is one of the FSCSS quoting rules the Python wrapper preserves exactly from upstream.


4. Project layout

Create this structure:

dashboard/
??? app.py
??? requirements.txt
??? static/
?   ??? src/
?       ??? site.fscss
?       ??? site.css
??? templates/
    ??? index.html
Enter fullscreen mode Exit fullscreen mode

site.css is generated. You can commit it so production does not need to compile styles at startup.


5. Write the stylesheet

static/src/site.fscss:

$accent: #8f6bff;
$ink: #0b0a13;
$paper: #f7f5ff;

@define panel(sel:.panel){`
  @use(sel) {
    px: 24px;
    py: 20px;
    rounded: 16px;
    shadow: 0 18px 50px rgba(0,0,0,.35);
    stack: 14px;
  }
`}

@define pill(sel:.pill){`
  @use(sel) {
    px: 14px;
    py: 8px;
    rounded: 999px;
  }
`}

body {
  bg: #0b0a13;
  color: $paper!;
  ff: system-ui, sans-serif;
  padding: 40px;
}

main {
  max-width: 760px;
  margin: 0 auto;
}

h1 {
  color: $accent!;
  font-size: 42px;
}

code {
  color: $accent!;
}

@panel(.card)
@pill(.badge)
Enter fullscreen mode Exit fullscreen mode

Compile it directly once:

fscss static/src/site.fscss static/src/site.css
Enter fullscreen mode Exit fullscreen mode

The generated site.css is ordinary CSS. No runtime JavaScript is involved.


6. Wire it into Flask

requirements.txt:

fscss>=0.2.0
flask>=2.2
Enter fullscreen mode Exit fullscreen mode

app.py:

import os
from pathlib import Path

import fscss
from flask import Flask, render_template

BASE_DIR = Path(__file__).resolve().parent
STATIC_SRC = BASE_DIR / "static" / "src"

DEBUG = os.getenv("FLASK_DEBUG") == "1"

app = Flask(__name__)
app.debug = DEBUG
fscss.init_flask(app, directory=STATIC_SRC)


@app.get("/")
def index():
    return render_template("index.html")


if __name__ == "__main__":
    app.run(debug=DEBUG)
Enter fullscreen mode Exit fullscreen mode

fscss.init_flask(app, directory=STATIC_SRC) does two things:

  1. Compiles every .fscss file in STATIC_SRC immediately.
  2. Starts a daemon watcher when app.debug is true.

templates/index.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>FSCSS dashboard</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='src/site.css') }}">
</head>
<body>
  <main>
    <span class="badge">Flask + FSCSS</span>
    <h1>Semantic CSS, compiled.</h1>
    <div class="card">
      <p>Edit <code>static/src/site.fscss</code> and refresh the page.</p>
      <p>With <code>FLASK_DEBUG=1</code>, FSCSS recompiles changed files automatically.</p>
    </div>
  </main>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Run it:

python app.py
Enter fullscreen mode Exit fullscreen mode

On Windows PowerShell, enable watch mode like this:

$env:FLASK_DEBUG = "1"
python app.py
Enter fullscreen mode Exit fullscreen mode

7. Use the Python API directly

The Flask helper is built on top of the same API you can use anywhere.

Compile one file

import fscss

fscss.compile_file("static/src/site.fscss", "static/src/site.css")
Enter fullscreen mode Exit fullscreen mode

compile_file returns the compiler's combined stdout/stderr. That output can include warnings even when the process exits 0.

Compile a string

import fscss

css = fscss.compile_string("$c: red;\nbody{ color: $c!; }")
print(css)
Enter fullscreen mode Exit fullscreen mode

compile_string returns CSS only. If you need warnings, use file compilation or check the CLI output.

Compile a directory

import fscss

for src, out in fscss.compile_dir("static/src"):
    print(f"{src} -> {out}")
Enter fullscreen mode Exit fullscreen mode

Watch manually

import fscss

fscss.watch_dir("static/src", interval=1.0)
Enter fullscreen mode Exit fullscreen mode

The watcher is a daemon thread intended for development. It polls source file mtimes and recompiles changed .fscss files to sibling .css files.


8. Django

For Django, call the helper from settings or an AppConfig.ready() method:

import fscss

fscss.init_django("path/to/static/src")
Enter fullscreen mode Exit fullscreen mode

If you omit the path, the helper tries <BASE_DIR>/static.

Remember that compilation writes a sibling .css file. If your source is static/src/site.fscss, your template should load static/src/site.css.


9. FastAPI

There is no dedicated FastAPI helper yet, but the Python API is enough:

import fscss
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

fscss.compile_file("static/src/site.fscss", "static/src/site.css")

app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
Enter fullscreen mode Exit fullscreen mode

If you want development watching, start fscss.watch_dir("static/src") during local development only.


10. CI: catch stale CSS

Add this to CI after installing fscss:

- name: Set up Node.js
  uses: actions/setup-node@v4
  with:
    node-version: '20'

- name: Install FSCSS
  run: pip install fscss

- name: Check generated CSS
  run: fscss build static/src --check
Enter fullscreen mode Exit fullscreen mode

--check does not write files. It exits 1 if the committed CSS does not match the current .fscss source.

For a normal build step that fails on warnings, use:

fscss build static/src --strict
Enter fullscreen mode Exit fullscreen mode

I recommend running --check in CI and --strict in your local or build-time compile step.


11. Debugging checklist

When something looks wrong, run:

fscss --version
fscss doctor
fscss env --json
fscss report --json
Enter fullscreen mode Exit fullscreen mode

Common issues:

  • Node is missing or too old. Install Node 20+.
  • A shorthand did not expand. Add the semicolon.
  • A variable did not apply. Move $var above the rule that uses it.
  • A warning was missed. Use --strict or inspect compile_file output.
  • An import failed. Local imports resolve relative to the input file; for strings, pass input_dir or set CLI --cwd.
  • A typo produced invalid CSS. The engine is not a full CSS AST validator.

If the same file fails in upstream FSCSS, report it to Figsh/xfscss. If it works upstream but fails through Python, open an issue at Omonire/fscss-py with a minimal reproducer.


12. When FSCSS is a good fit

Use FSCSS when you want:

  • semantic class names
  • design tokens
  • mixins and compact shorthands
  • plain CSS output
  • zero runtime styling
  • a Python-first developer experience

It may not be a fit if your team already has a mature Tailwind design system, needs full CSS AST validation, or wants to avoid Node entirely today.


Final thoughts

FSCSS belongs to the upstream community and Figsh. The Python wrapper is my attempt to make it comfortable for the Python web ecosystem: installable with pip, scriptable from Python, usable from Flask/Django/FastAPI, and safe to check in CI.

If anything in this tutorial feels awkward, that is useful feedback. Open an issue, include your fscss report --json output and a small .fscss file, and I will use it to improve the next release.


Links

Suggested DEV.to tags: python, css, webdev, flask

Top comments (0)