Skip to main content

Super charged Jinja macros for Flask

Project description

Flask-Super-Macros

Super charged Jinja macros for Flask using Jinja Super Macros.

  • Inspired by frontend component frameworks
  • Use macros like html components
  • Auto macro loader
  • Compatible with Storybook

Installation

pip install flask-super-macros

Usage

from flask import Flask
from flask_super_macros import SuperMacros

app = Flask(__name__)
SuperMacros(app)

Macros will be automatically registered from:

  • Macro defined using the macro directive in all template files called __macros__.html
  • Created from files with the extension .macro.html
  • Created from files in the macros folder

(Note: creating macros from files is the equivalent of wrapping the whole file in a macro directive)

Example macros/btn.html:

<{button class="btn" **kwargs}>{{ inner }}</{}>

You can then use this macro in any template (no need to import):

<{ btn onclick="alert('hello')" }>click me</{ btn }>

See Jinja Super Macros documentation to learn more about the new macro calling syntax.

To register macros from other files, the macro registry is available under ̀app.macros:

app.macros.register_from_template("macros.html")

You can also create macros fron functions using app.macro:

from jinja_super_macros import html_class

@app.macro
def btn(**kwargs):
    """<{button class="btn" **kwargs}>{{ inner }}</{}>"""

Storybook

Registered jinja macros can be viewed in Storybook using the server renderer.

Initialize your storybook project:

npx storybook@latest init -t server

(Optionnaly, empty the stories folder first to remove examples)

Create story files:

flask create-macro-stories --watch

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_super_macros-0.1.0.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

flask_super_macros-0.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file flask_super_macros-0.1.0.tar.gz.

File metadata

  • Download URL: flask_super_macros-0.1.0.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-40-generic

File hashes

Hashes for flask_super_macros-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4e80bed45ffbe53ab9544f9a3f2918cb7c14bb6359ccb8033556db763db50cff
MD5 0db710efcb660097b0070f49860cd039
BLAKE2b-256 10dac8bb067cffd007ff704268693c2af37c1e7ecf204369a293e80657914d87

See more details on using hashes here.

File details

Details for the file flask_super_macros-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_super_macros-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ef9aa6d72202c9dbad517954ca0c05ca1228ae172348a800539b3d9c77e6e4c
MD5 f1d22eae7b3d5d377d587d7c7f18e043
BLAKE2b-256 8226d4c6a1576812c308974f96be6549b7af8cdb30253bd2d2fef0accb5b94c0

See more details on using hashes here.

Supported by

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