50
loading...
This website collects cookies to deliver better user experience
pip install Flask-Assets
npm install tailwindcss postcss postcss-cli autoprefixer @fullhuman/postcss-purgecss
tailwind.config.js
by running this inside terminal.npx tailwind init
postcss.config.js
and insert this code below:// postcss.config.js
const path = require('path');
module.exports = (ctx) => ({
plugins: [
require('tailwindcss')(path.resolve(__dirname, 'tailwind.config.js')),
require('autoprefixer'),
process.env.FLASK_PROD === 'production' && require('@fullhuman/postcss-purgecss')({
content: [
path.resolve(__dirname, 'templates/**/*.html')
],
defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || []
})
],
});
static/src/main.css
:/* static/src/main.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
static/dist/main.css
. You don’t need to create the file static/dist/main.css
since this file will be automatically generated when you run the application.# app.py
from flask import Flask
from flask_assets import Bundle, Environment
app = Flask(__name__)
# Bundling src/main.css files into dist/main.css'
css = Bundle('src/main.css', output='dist/main.css', filters='postcss')
assets = Environment(app)
assets.register('main_css', css)
css.build()
static/dist/main.css
is generated properly. If not and if you get an error like this:Program file not found: postcss
npm install --global postcss postcss-cli
<!-- templates/base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% assets 'main_css' %}
<link rel="stylesheet" href="{{ ASSET_URL }}">
{% endassets %}
<title>Flask + TailwindCSS + Flask-Assets</title>
</head>
<body class="bg-blue-100">
{% block content %}
{% endblock content %}
</body>
</html>
{% assets 'main_css' %}
<link rel="stylesheet" href="{{ ASSET_URL }}">
{% endassets %}
<body class="bg-green-100">
<div>About Page</div>
</body>