Skip to main content
Saram Consulting LLC

Flask with Tailwind CSS

Starting a new project with Flask and Tailwind CSS can be an exciting journey into web development. Flask offers a simple yet powerful framework for building web applications, while Tailwind CSS provides a utility-first approach to styling, making your workflow both efficient and fun. This blog post will guide you through the process of bootstrapping a Flask project with Tailwind CSS, ensuring you have a solid foundation for building beautiful, responsive web applications.

Step 1: Set Up Your Flask Environment #

Before diving into Tailwind CSS, you need to set up your Flask environment. If you haven't already, install Python on your machine. Once Python is installed, you can create a virtual environment for your project. This virtual environment allows you to manage dependencies separately for each project, avoiding conflicts.

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your project.
  3. Run the following commands:
python3 -m venv myprojectenv
source myprojectenv/bin/activate  # On Windows, use `myprojectenv\Scripts\activate`

With your virtual environment activated, install Flask using pip:

pip install Flask

Step 2: Initialize Your Flask Application #

Create a new file named app.py in your project directory. Open it in your text editor and add the following code to define a simple Flask application:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, Flask with Tailwind CSS!'

if __name__ == '__main__':
    app.run(debug=True)

This code snippet creates a basic Flask app with a single route that returns a greeting message.

Step 3: Integrate Tailwind CSS #

Now, let's add Tailwind CSS to your project. We'll use npm (Node Package Manager) to install Tailwind CSS, so make sure Node.js is installed on your system.

  1. Initialize a new npm project in your Flask application directory:
npm init -y
  1. Install Tailwind CSS, PostCSS, and autoprefixer:
npm install tailwindcss postcss autoprefixer
  1. Create your Tailwind configuration files:
npx tailwindcss init -p

This command generates a tailwind.config.js file for configuring Tailwind and a postcss.config.js file for PostCSS.

  1. Create a static/css directory in your Flask project, then create a file named styles.css inside it. Add the following Tailwind directives to styles.css:
@tailwind base;
@tailwind components;
@tailwind utilities;
  1. Modify your package.json to add a script for building your CSS with Tailwind. Add the following line to the "scripts" section:
"build-css": "tailwindcss -i ./static/css/styles.css -o ./static/css/output.css --watch"
  1. Run the build script:
npm run build-css

This command compiles your styles.css, including all the Tailwind utilities, to output.css, which you will link to in your Flask templates.

Step 4: Use Tailwind CSS in Your Flask Application #

With Tailwind CSS set up, you can start using it in your Flask application. Create a template directory called templates if it doesn't already exist, and add an index.html file inside it with the following content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask with Tailwind CSS</title>
    <link rel="stylesheet" href="/static/css/output.css">
</head>
<body class="bg-gray-200">
    <div class="text-center pt-10">
        <h1 class="text-4xl text-blue-500">Hello, Flask with Tailwind CSS!</h1>
    </div>
</body>
</html>

Modify your app.py to render this template:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

Step 5: Enjoy Your Flask Application with Tailwind CSS #

Congratulations! You've successfully bootstrapped a Flask project with Tailwind CSS. You now have a basic but powerful setup to create a responsive and stylish web application. Tailwind's utility-first approach allows you to quickly design your app directly in your HTML files, speeding up the development process significantly.

Remember, the journey doesn't stop here. Explore Flask and Tailwind CSS documentation to discover more features and best practices to