GithubHelp home page GithubHelp logo

tmkontra / fastapi-static-digest Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 206 KB

FastAPI plugin to digest and compress static files, and integrate with Jinja templates

License: MIT License

Python 99.08% HTML 0.33% Shell 0.59%

fastapi-static-digest's Introduction

FastAPI Static Digest

A Starlette/FastAPI plugin to digest and compress static files, and integrate with Jinja templates.

pip install fastapi-static-digest

Overview

There are two classes: StaticDigest and StaticDigestCompiler.

The StaticDigestCompiler.compiler method will produce the digested directory. The StaticDigest (and starlette.staticfiles.StaticFiles) should be configured to read this directory.

The compilation can be added to your build process, for instance with a click CLI command (see below). Alternatively, the FASTAPI_STATICDIGEST_RELOAD=1 environment variable can be set to enable "hot reloading" during development.

Usage

# app.py

app_root = Path(__file__).parent
static_src =  app_root / "static"
static = StaticDigest(source_dir=static_src)

routes = [
    Mount('/static', app=StaticFiles(directory=static.directory), name="static"),
]

app = Starlette(routes=routes)
# manage.py

from .app import app_root


@click.command()
def compile():
    src = app_root / "static"
    click.echo("Source dir %s" % src)
    compiler = StaticDigestCompiler(source_directory=src)
    compiler.compile()
    click.echo("Done.")

Jinja2 Integration

# app.py

app_root = Path(__file__).parent
static_src =  app_root / "static"
static = StaticDigest(source_dir=static_src)
templates = Jinja2Templates(app_root / "templates")
static.register_static_url_for(templates)

routes = [
    Mount('/static', app=StaticFiles(directory=static.directory), name="static"),
]

app = Starlette(routes=routes)
<!-- index.html -->
{{ static_url_for("static", "app.css" )}}

renders to

https://myhost.com/static/app.92fede82119d2e012f890e1102080a45.css

Development

PRs and issues are welcome!

fastapi-static-digest's People

Contributors

tmkontra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.