Skip to content

Instantly share code, notes, and snippets.

@Kaligraphy247
Last active August 5, 2024 22:34
Show Gist options
  • Save Kaligraphy247/8b4e938d05e35e96aa7bb2aa3d69b60e to your computer and use it in GitHub Desktop.
Save Kaligraphy247/8b4e938d05e35e96aa7bb2aa3d69b60e to your computer and use it in GitHub Desktop.
Tailwindcss starter script for FastHTML
# httpx is a required dependency for FastHTML, feel free to substitute for something else.
import httpx
import os
import platform
# Constants and Variables
NAME = ""
CACHE_PATH = ".tailwindcss"
INPUT_CSS_FILE = f"{CACHE_PATH}/input.css"
OUTPUT_CSS_FILE = "static/css/style.css"
CONFIG_FILE_PATH = f"{CACHE_PATH}/tailwind.config.js"
# Set default version for now
VERSION = "v3.4.7"
uname = platform.uname()
match uname.system:
case "Darwin" if uname.machine == "arm64":
print("Detected System: Darwin, machine type: arm64")
NAME = "tailwindcss-macos-arm64"
case "Darwin" if uname.machine == "x86_64":
print("Detected System: Darwin, machine type: x86_64")
NAME = "tailwindcss-macos-x64"
#! untested
case "Linux" if uname.machine == "aarch64":
NAME = "tailwindcss-linux-arm64"
case "Linux" if uname.machine == "x86_64":
NAME = "tailwindcss-linux-x64"
case "Windows" if uname.machine == "x86_64":
NAME = "tailwindcss-windows-x64.exe"
case "Windows" if uname.machine == "AMD64":
NAME = "tailwindcss-windows-x64.exe"
case "Windows" if uname.machine == "arm64":
NAME = "tailwindcss-windows-x86.exe"
#! untested
case _:
NAME
if NAME == "":
print("Could not detect system and/or architecture.")
exit(0)
if not os.path.exists(CACHE_PATH):
os.mkdir(CACHE_PATH)
# 1. download the current binary
download_url = (
f"https://github.com/tailwindlabs/tailwindcss/releases/download/{VERSION}/{NAME}"
)
# 2. check if file exists, if not download the binary
if os.path.exists(f"{CACHE_PATH}/{NAME}"):
print(f"Tailwindcss Binary `{NAME}` exists, skipping download...")
else:
print(f"Tailwindcss Binary `{NAME}` not found, downloading...")
res = httpx.get(download_url, timeout=20.0, follow_redirects=True)
if res.status_code == 200:
with open(f"{CACHE_PATH}/{NAME}", "wb") as f:
f.write(res.content)
print("Download complete!")
# make executable
if uname.system != "Windows":
os.chmod(f"{NAME}", 0o700)
else:
print("Download failed")
# 3. configure the binary
input_css = """\
@tailwind base;
@tailwind components;
@tailwind utilities;\
"""
config = """\
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./main.py',
// './**/*.py', // uncomment this to watch all py files. NOT RECOMMENDED!!. Watch specific folders instead.
// './pages/**/*.py', // RECOMMENDED
// './components/**/*.py', // RECOMMENDED
],
}\
"""
if not os.path.exists(INPUT_CSS_FILE):
with open(INPUT_CSS_FILE, "w") as f:
f.write(input_css)
if not os.path.exists(CONFIG_FILE_PATH):
with open(CONFIG_FILE_PATH, "w") as f:
f.write(config)
# 4. run the binary and watch for changes in specified files
print("Running Tailwindcss ...")
os.system(
f"./{NAME} --input {INPUT_CSS_FILE} --output {OUTPUT_CSS_FILE} --config {CONFIG_FILE_PATH} --watch"
) # optionally minify with --minify
@Kaligraphy247
Copy link
Author

Kaligraphy247 commented Aug 3, 2024

This script simplifies setting up and running Tailwindcss for Python. It was specifically written for FastHTML but is pure Python, therefore you can modify it (when necessary) and use it wherever you like.

Dont forget to add CACHE_PATH to your .gitignore file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment