Skip to main content

A simple package that enables live reload and Tailwind CSS for Flask templates.

Project description

Flask live tailwindcss

A simple package for adding a dev server to your flask app that automatically compiles the tailwindcss of the templates on file save and triggers a browser reload to sync the changes on the fly.

Installation

This package is available on PyPI, you can install it using pip or other package manager of your choice.

pip install flask-livetw

Its a good practice to add as a development dependency. You can do this by adding the package to your requirements-dev.txt file or the "dev dependencies" of your package manager.

flask-livetw

Initialization

To start using this package, go to your project folder or the folder where you want to develop. Then run the following command to initialize the package.

livetw init

For default values use the -d or --default flag.

Then where you have your flask app add the following configuration to enable the live reload feature.

import os

...

app.config["LIVETW_DEV"] = os.getenv("LIVETW_ENV") == "development"

When running the flask server separately during development remember to set the LIVETW_DEV to True in your flask app configuration to enable the live reload feature.

app.config["LIVETW_DEV"] = True

For more information on the configuration options see the Configuration section.

Commands

Each command has its own help page, you can use the -h or --help flag to see the available options.

dev

livetw dev

By default the command starts:

  • a flask server in debug mode
  • a live reload websocket server
  • a tailwindcss in watch mode

This command sets the enviroment variable LIVETW_ENV to development. This is useful for conditional code execution.

build

Builds the Tailwind CSS for the templates into a single CSS file.

livetw build

By default, the built Tailwind CSS file will be minified to reduce the file size. You can disable this feature by using the --no-minify flag.

livetw build --no-minify

init

Initializes the package in the current directory. Its the command used in Initialization.

livetw init

Configuration

After initialization, a pyproject.toml file will be created if it does not exist. In this file under the [tool.flask-livetw] section

This is an example of a configuration file with the default values:

[tool.flask-livetw]
flask_root = "src"
static_folder = "static"
template_folder = "templates"
template_glob = "**/*.html"
base_layout = "layout.html"
livetw_folder = ".dev"
flask_app = "app"

As a file tree, the configuration would look like this:

🌳 project_folder/
┣ 📁 src/
┃ ┣ 📁 static/
┃ ┃ ┣ 📁 .dev/
┃ ┃ ┗ ...
┃ ┣ 📁 templates/
┃ ┃ ┣ 📄 layout.html
┃ ┃ ┗ ...
┃ ┗ ...
┃ 📄 app.py
┣ 📄 pyproject.toml
┗ ...

Main options

flask_root

The root folder of the flask application. All paths are relative to this folder.

Default is "src".

flask_app

The argument --app passed to flask to discover the application. For more information see the Flask Application Discovery documentation.

Default is "app".

static_folder

The folder where the static files are stored relative to the flask_root.

Default is "static".

template_folder

The folder where the templates are stored relative to the flask_root.

Default is "templates".

template_glob

The glob pattern to search for templates to build the Tailwind CSS. Must be relative to the template_folder.

Default is "**/*.html".

For more information see the Tailwindcss configuring source paths documentation.

base_layout

The layout that is shared among all templates. This file is used to inject the Tailwind CSS and the live reload script. If it isn't shared among all templates, the Tailwind CSS and the live reload script won't be injected into the templates.

Default is "layout.html".

livetw_folder

The folder where flask-livetw stores the related files.

It contains:

  • global.css: The global CSS file for the application.
  • live_reload.js: The live reload script for the application.
  • tailwind_development.css: The Tailwind CSS file for the application during development.

Default is ".dev".

Other options

global_css

The global CSS file for the application relative to the livetw_folder.

Default is "global.css".

tailwind_prod

The production CSS file for the application relative to the static_folder.

Default is "tailwind_production.css".

live_reload_host

The host for websocket server for the live reload functionality. If set must be manually sync on the live reload script for the client.

Default is "127.0.0.1".

live_reload_port

The port for websocket server for the live reload functionality. If set must be manually sync on the live reload script for the client.

Default is 5678.

flask_host

The host for the Flask application.

Default is "127.0.0.1".

flask_port

The port for the Flask application.

Default is 5000.

flask_exclude_patterns

The patterns to exclude python scripts from the Flask application. This is useful to exclude files from triggering the flask server to reload.

Contributing

Contributions are welcome, feel free to submit a pull request or an issue.

Packages used

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-livetw-0.4.2.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_livetw-0.4.2-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file flask-livetw-0.4.2.tar.gz.

File metadata

  • Download URL: flask-livetw-0.4.2.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for flask-livetw-0.4.2.tar.gz
Algorithm Hash digest
SHA256 069e293d82ecba6afa83975cc537dda12021dbb1463a3943a5b7342474147646
MD5 570370f8987502a174360eff26aa06d3
BLAKE2b-256 27ee72915e72fe87c9025a5268548276332a7541b2857b2d16f284c98ad8d064

See more details on using hashes here.

File details

Details for the file flask_livetw-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: flask_livetw-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for flask_livetw-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1924217175aacf082fa45fec1d559242d3e3a31c493fe990092b9b163eb69cd2
MD5 840523b05795178e1ee092933662d74a
BLAKE2b-256 343d9c62e5d33d8a5f28ad0c489e852728f9f5a6b680c02fee7606dea02d0ef9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page