Coder Social home page Coder Social logo

oliverandrich / django-tailwind-cli Goto Github PK

View Code? Open in Web Editor NEW
86.0 5.0 10.0 1.16 MB

Django and Tailwind integration based on the prebuilt Tailwind CSS CLI.

License: MIT License

Python 93.04% HTML 2.47% Just 4.48%
django tailwind tailwindcss django-application python tailwind-css

django-tailwind-cli's Introduction

django-tailwind-cli

GitHub Workflow Status PyPI Code style: black GitHub Django Versions Python Versions Downloads Downloads / Month

This library provides an integration of Tailwind CSS for Django that is using on the precompiled versions of the Tailwind CSS CLI.

The goal of this library is to provided the simplest possible Tailwind integration for your Django project. It took its inspiration from the Tailwind integration for Phoenix which completely skips the neccesity of a node installation.

Installation

  1. Install the library.

    python -m pip install django-tailwind-cli
  2. Add django_tailwind_cli to INSTALLED_APPS in settings.py.

    INSTALLED_APPS = [
        # other Django apps
        "django_tailwind_cli",
    ]
  3. Configure the STATICFILES_DIRS parameter in your settings.py if not already configured.

    STATICFILES_DIRS = [BASE_DIR / "assets"]
  4. Add template code.

    {% load tailwind_cli %}
    ...
    <head>
      ...
      {% tailwind_css %}
      ...
    </head>
  5. Start the debug server.

    python manage.py tailwind runserver

Enjoy!

Checkout the detailed installation guide if you want to activate browser reload or the runserver_plus management command known from django-extensions.

Features

  • Simplest possible integration.
  • Management commands:
    • To start the Tailwind CLI in watch mode during development.
    • To build the production grade CSS file for your project.
    • To start a debug server along with the Tailwind CLI in watch mode in a single session.
  • Necessary configuration to adapt the library to your project, when the defaults don't fit you.
  • A template tag to include the Tailwind CSS file in your project.
  • A base template for your project.
  • A sane tailwind.config.js that activates all the official plugins and includes a simple HTMX plugin.

Requirements

Python 3.9 or newer with Django >= 3.2.

Documentation

The documentation can be found at https://django-tailwind-cli.andrich.me/

Contributing

In order to contribute, this package has no required tool dependencies besides an installed version of Python and pip. But you can use uv to speed up your workflow a bit. The following commands assume that you have already setup a virtual environment and activated it.

# Setup development environment
just bootstrap

# Upgrade/install all dependencies defined in pyproject.toml
just upgrade

# Run pre-commit rules on all files
just lint

# Run test suite
just test

Without just, but using pip

# Install dependencies
pip3 install -e ".[django-extensions,dev,docs]"

# Run nox testrunner
nox

Without just, but using uv

# Install dependencies
uv pip install -r pyproject.toml --all-extras -e .

# Run nox testrunner
nox --force-venv-backend uv

License

This software is licensed under MIT license.

django-tailwind-cli's People

Contributors

andrlik avatar bckohan avatar bminde avatar dependabot[bot] avatar jefftriplett avatar joshuadavidthomas avatar killianarts avatar lgp171188 avatar oliverandrich avatar wshayes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

django-tailwind-cli's Issues

tailwind download

It would be helpful if _download_cli_if_not_exists was exposed as an optional tailwind download sub-command.

This would make it possible in a Dockerfile to download the binary (without architecture hoops) when the image is built so that it's not later downloaded when the tailwind build command is run.

I currently run python -m manage tailwind --skip-checks build in my Dockerfile, which is fine to get the binary file installed, but it tends to create a bunch of extra layers that seem to add up rather quickly.

subprocess.CalledProcessError

I am trying to use the plugin but while run python manage.py tailwind watch i got the following error
subprocess.CalledProcessError: Command '['/home/<user_name>/.local/bin/tailwindcss-linux-x64-3.4.0', '--output', '/var/www/html/django_new/assets/css/tailwind.css', '--watch']' died with <Signals.SIGSEGV: 11>. any help will be appreciated

I am working on a Awesome-Python-HTMX, seeking your feedback

A few of us at PyCon this year got together and brainstormed a new Web Stack that we are calling PyHAT (Python, htmx, ASGI, TailwindCSS). The first thing we set out to do is create awesome-python-htmx; a collection of active tools/libraries/projects in that space (including Tailwind). I have included your project a section listing tooling.

If you could also participate in PyHAT-stack/awesome-python-htmx#1 that would be greatly appreciated! I would in particular be interested to hear about sets your tailwind Python tooling apart from the alternatives.

django_extensions runserver_plus option?

Great tool. It makes developing with Django and Tailwind much easier.

Would it be possible to add an option for runserver_plus from django extensions (to be able to run SSL for localdev)?

I'm happy to take a stab at creating a pull request for this, but I wanted to run it past you first in case you didn't want to do this.

`tailwind runserver` isn't passing through `--skip-checks`

I'm currently running python manage.py tailwind --skip-checks runserver 0.0.0.0:8000 and I noticed it was taking between 7 to 10 seconds to scan+reload Python code changes.

I checked my logs and noticed Performing system checks... which is a sign that --skip-checks isn't being respected/passed to runserver.

Frustratingly, this is a common performance issue for medium to large projects in containers.

if --skip-checks could either get passed through or added as an options to runserver then it should just work via: python manage.py tailwind runserver --skip-checks 0.0.0.0:8000.

Is there any interest in adding this? I might be able to take it on Friday if so. (assuming someone doesn't beat me to it :) )

Could `TAILWIND_CLI_PATH` point to a binary?

Discussed in #65

Originally posted by jefftriplett November 1, 2023
Wonderful tool btw.

This is a one-part question and one-part help for anyone else who might be trying to wrap their head around making this work in Docker so that it doesn't try to re-download the tailwindcss binary every time python manage.py tailwind <cmd> is run.

I'm using django-tailwind-cli in containers, and I found myself struggling/jumping through a few hoops to get the tailwindcss binary to work effectively.

I mistakenly had TAILWIND_CLI_PATH set to /usr/bin/tailwindcss which created /usr/bin/tailwindcss/tailwindcss-linux-x64-3.3.5.

What I'm using now is:

ARG TAILWINDCSS_VERSION=3.3.5
...

# download the Tailwind CSS CLI binary
ADD https://github.com/tailwindlabs/tailwindcss/releases/download/v${TAILWINDCSS_VERSION}/tailwindcss-linux-x64 \
    /usr/bin/tailwindcss-linux-x64-${TAILWINDCSS_VERSION}

RUN chmod 755 /usr/bin/tailwindcss-linux-x64-${TAILWINDCSS_VERSION}

...

RUN DATABASE_URL=sqlite://:memory: python manage.py tailwind --skip-checks build

Would it make sense to allow users to set where the Tailwind CSS CLI binary is stored?

Add documentation about running watch command in `docker-compose.yml` as a sidecar

Firstly, thanks so much for this package! ๐Ÿ’š It has really helped simplify our dev stack by (mostly) removing the need to have Node installed just to use Tailwind.

I do have a small suggestion for documentation improvements.

I have been playing with running python manage.py tailwind watch as a sidecar container in our dev docker-compose.yml file to take care of auto-rebuilding the CSS without having to run the command myself.

At first this wasn't working, either in a dedicated tailwind container using the above command or in the main django container using the python manage.py tailwind runserver command. The dedicated container would just quit immediately after starting -- with no log output to help debug what was going on -- or in the main container the runserver command would start the development server and no CSS was rebuilt with no logs as well.

I even skipped using this package and ran the Tailwind cli directly using the same arguments this package uses to the same effect of the container quitting immediately.

I was tearing my hair out trying out trying to chase down what was going on -- hard to do with no logs ๐Ÿ™ƒ -- and I stumbled across some issues related to the issue in regards to the rails package:

tailwindlabs/tailwindcss#9831

Adding tty: true to the compose file that the tailwindcss:watch process is running in will prevent the immediate exit.

https://github.com/rails/tailwindcss-rails#update-assets-automatically

If you are running rails tailwindcss:watch as a process in a Docker container, set tty: true in docker-compose.yml for the appropriate container to keep the watch process running.

Looks to be an issue with the Tailwind cli binary in watch mode expecting to be run in a TTY. Sure enough, adding tty: true to the service definition in my docker-compose.yml fixed the issue and the sidecar container is able to start and stay running while rebuilding the CSS in the background.

I don't mind writing up something and submitting a PR for this myself, but I wasn't sure where you would want something like this. I assume somewhere in the "Usage" section, but I'll leave that up to you. ๐Ÿ˜

Remove the httpx dependency

Generally, httpx is a good idea, but adding it as a dependency to simply download a single file is a bit too much. Switch to urllib.request instead.

adding plugins from node_modules

I wonder if there is an easy way to add additional npm packages as plugins to this app.
For example I would like to integrate daisyui with django-tailwind-cli and don't know how to start...

Any hint would be very helpful.

Update Windows Tailwind executable download path

Hello. When I run python .\manage.py tailwind runserver I get the message:

Tailwind CSS CLI not found.
Downloading Tailwind CSS CLI from 'https://github.com/tailwindlabs/tailwindcss/releases/download/v3.3.5/tailwindcss-windows-amd64'

Which errors with 404 not found. The download link in Tailwind Labs repository is:

https://github.com/tailwindlabs/tailwindcss/releases/download/v3.3.5/tailwindcss-windows-x64.exe

With the differences being:

  • x64 instead of amd64
  • Includes .exe suffix

If I update these two functions it downloads the correct Tailwind CLI executable:

def get_system_and_machine(self) -> Tuple[str, str]:
    """Get the system and machine name."""
    system = platform.system().lower()
    if system == "darwin":  # pragma: no cover
        system = "macos"

    machine = platform.machine().lower()
    if machine == "x86_64" or 'amd64':  # pragma: no cover
        machine = "x64"
    elif machine == "aarch64":  # pragma: no cover
        machine = "arm64"

    return (system, machine)

def get_download_url(self) -> str:
    """Get the download url for the Tailwind CSS CLI."""
    system, machine = self.get_system_and_machine()
    extension = '.exe' if system == 'windows' else ''
    return (
        "https://github.com/tailwindlabs/tailwindcss/releases/download/"
        f"v{self.tailwind_version}/tailwindcss-{system}-{machine}{extension}"
    )

With the changes being:

  • Add or 'amd64' to if machine == "x86_64" or 'amd64':
  • Add extension = '.exe' if system == 'windows' else ''
  • Update the f-string to include the {extension}

I'm using Python 3.11.3 64-bit on Windows 11 with Django 4.2.4

add --settings for runserver or runserver_plus

I'm experimenting with your work and I find it amazing, thanks.

I normally use separate settings files for development, production and what I normally type in the terminal is something like this:

python manage.py runserver_plus --settings=<project_name>.settings.development

If I try to use your script to run both server_plus and tailwind like this:

python manage.py tailwind runserver_plus --settings=<project_name>.settings.development

It won't work because (as I understand) you need to add a parser for passing --settings=<project_name>.settings.development to runserver_plus. I know I can use two separate scripts but it will be great if you can add it to the single script. Please let me know if this is possible and you plan to add it in future release.

Thanks a lot for your work.. I appreciate it a lot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.