GithubHelp home page GithubHelp logo

biggleszx / django-simpleinliner Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 28 KB

A Django app for inlining static files in templates, taking inspiration from django-compressor, django-pipeline and django-inlinecss

Home Page: https://pypi.org/project/django-simpleinliner/

License: MIT License

Python 100.00%
python django performance

django-simpleinliner's Introduction

django-simpleinliner

A simple Django app for inlining static CSS and JS files in templates. Read CSS, JS or generic files from your static directories and insert their contents into your Django templates, wrapping in appropriate HTML tags if necessary.

Rationale

Provides a quick and easy way to inline an entire JS or CSS file from staticfiles into a template, wrapping it in appropriate tags. django-compressor does this but I wanted something more lightweight, and also wanted to try my hand at writing a Django extension. Some inspiration and staticfile-handling code was taken from django-inlinecss.

Compatibility

I've not exhaustively tested all the below combinations, however I believe this table to be accurate.

Django 1.11 2.0 2.1 2.2 3.0 3.1 3.2
Python 2.7
3.6
3.7
3.8

Installation

$ pip install django-simpleinliner

Add simpleinliner to your INSTALLED_APPS setting.

Usage

Load the app at the top of your template:

{% load simpleinliner %}

Call inlinecss, inlinejs or inlinegeneric where you want to pull in a static file:

{% inlinejs 'path/to/my.js' %}

{% inlinecss 'path/to/my.css' %}

{% inlinegeneric 'path/to/my.svg' %}

The file will be inserted into the template each time the template is rendered, keeping it up to date.

You can override the default attributes given to <script> and <style> tags generated by simpleinliner by including the following in your project settings:

SIMPLEINLINER_DEFAULT_TAG_ATTRIBUTES = {
    'script': {
        'charset': 'utf-8',
        'type': 'text/javascript',
    },
    'style': {
        'charset': 'utf-8',
        'type': 'text/css',
    },
}

Add or edit these as desired to change the attributes applied to these tags.

By default simpleinliner will silently fail (including an empty tag if using inlinejs or inlinecss) if the specified path doesn't exist. You can force it to raise an exception by setting SIMPLEINLINER_RAISE_EXCEPTIONS to True in your project settings.

Development Installation

If working locally on the package you can install the development tools via pip:

$ pip install -e .[dev]

To lint with flake8:

$ flake8

Credits

Since version 0.2.5 this library bundles a lightly modified version 1.16 of the html library (in html.py), as that library does not correctly install itself in modern python/setuptools environments and cannot be correctly imported.

Issues, Suggestions, Contributions

...are welcome on GitHub. Thanks for your interest in simpleinliner!

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.