Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Setup for flask with tailwind project
Bogusdeck
Bogusdeck

Posted on

     

Setup for flask with tailwind project

how to setup tailwind css with flask app

status: ✏️ Draft
categories: Coding, Javascript, flask, python, webdev
published: 12/03/2024
Author: tanish vashisth
route: tutorial

Simplicity is the cornerstone of efficient web development. In this blog post, we'll explore the seamless integration of Flask, a lightweight Python web framework, with Tailwind CSS, a utility-first CSS framework. By combining these two powerful tools, developers can effortlessly create sleek and responsive web applications with minimal setup. Join us as we unveil the essentials of setting up a Flask and Tailwind project, empowering you to embark on your web development journey with ease and finesse.

Setting up a Flask project with Tailwind CSS involves several steps. Here's a basic guide to get you started:

  1. Install Flask :

    First, you need to have Flask installed. You can install it via pip:

    pipinstallFlask
  2. Create a Flask project directory :

    Create a directory for your Flask project.

  3. Initialize a virtual environment (optional but recommended):

    Navigate to your project directory and create a virtual environment to isolate your project dependencies.

    python3-m venv venv
  4. Activate the virtual environment :

    • On Windows:
    .venv\Scripts\activate
- On macOS/Linux:
Enter fullscreen modeExit fullscreen mode
```source venv/bin/activate```
Enter fullscreen modeExit fullscreen mode
  1. Start an npm project :

    We need this to automatically compile our tailwindcss file to make sure only the tailwind classes we are using are included in the output.css file

    npm init-y
  2. Initialize Tailwind CSS :

    Initialize Tailwind CSS in your project directory. This will create atailwind.config.js file and atailwind.css file.

    npx i tailwindcss

    now we need to initialise tailwindcss

    npx tailwindcss init
  3. Add content in tailwind.config.js :

we need to add the path of our templates files inside tailwind.config.js

/** @type {import('tailwindcss').Config} **/module.exports={content:["./templates/*.html"],theme:{extend:{},},plugins:[],}
Enter fullscreen modeExit fullscreen mode
  1. Create input.css file:

Now create a CSS file which will contain the base code of tailwindcss.

create the file inside“/static/css/input.css”

Enter the following code inside input.css

@tailwindbase;@tailwindcomponents;@tailwindutilities;
Enter fullscreen modeExit fullscreen mode
  1. Start the build process:

We will now run a command that will take the input.css file as input and generate the output CSS which will only contain the tailwind classes required by our html files

now run this command on a new terminal :

npxtailwindcss-i./static/css/input.css-o./static/css/output.css--watch
Enter fullscreen modeExit fullscreen mode
  1. Add the output.css:

This command instructs Tailwind to create an "output.css" file containing only the utility classes utilized within the HTML files located in your "templates" folder. You can then include the generated file "./static/css/output.css" in your HTML files using the specified command.

<linkhref="{{url_for('static',filename='css/output.css')}}"rel="stylesheet">
Enter fullscreen modeExit fullscreen mode
  1. Test your project :

Add the below code inside the index.html file

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><linkhref="{{url_for('static',filename='css/output.css')}}"rel="stylesheet"><title>Document</title></head><body><divclass="bg-red-600">        background is red</div></body></html>
Enter fullscreen modeExit fullscreen mode
  1. Createapp.py file in your root directory :

    from flask import Flask, render_templateapp = Flask(__name__)@app.route("/")def hello_world():    return render_template("index.html")app.run(debug=True)
  2. Run the flask app:

    python main.py

Now, you should have a Flask project set up with Tailwind CSS integrated. Access your Flask application in a web browser by navigating tohttp://localhost:5000/.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Way Down We Go
  • Joined

More fromBogusdeck

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp