Easy, flexible icons for Django
Project description
Django Easy Icons
Easy, flexible icons for Django templates with support for multiple rendering backends.
Overview
Django Easy Icons provides a simple, consistent way to include icons in your Django templates. It supports multiple icon sources including SVG files, font icon libraries (like Font Awesome), and SVG sprite sheets.
Features
- Multiple Renderers: Support for SVG files, font icons, and sprite sheets
- Template Integration: Simple
{% icon %}template tag - Flexible Configuration: Configure multiple icon sets with different renderers
- Attribute Merging: Easily add classes and attributes to icons
- Caching: Built-in renderer caching for performance
- Dict Attributes: Pass a dict of extra attributes to the template tag via
extrakwargs
Installation
pip install django-easy-icons
Add easy_icons to your INSTALLED_APPS:
INSTALLED_APPS = [
# ... other apps
'easy_icons',
]
Quick Start
1. Configure Icon Renderers
Add configuration to your Django settings:
EASY_ICONS = {
"default": {
"renderer": "easy_icons.renderers.SvgRenderer",
"config": {
"svg_dir": "icons", # Template directory for SVG files
"default_attrs": {
"height": "1em",
"fill": "currentColor"
}
},
"icons": {
"home": "home.svg",
"user": "user.svg",
"settings": "cog.svg"
}
}
}
2. Use in Templates
{% load easy_icons %}
<!-- Basic usage -->
{% icon "home" %}
<!-- With additional attributes -->
{% icon "user" class="nav-icon" height="2em" %}
<!-- Using specific renderer -->
{% icon "heart" renderer="fontawesome" %}
<!-- Passing a dictionary of attributes (e.g. from another library) -->
{% icon "user" extrakwargs=attr_dict %}
<!-- Dictionary + explicit overrides (explicit wins) -->
{% icon "user" extrakwargs=attr_dict class="avatar" %}
3. Use in Python Code
from easy_icons import icon
# Basic usage
home_icon = icon("home")
# With attributes
user_icon = icon("user", **{"class": "large", "data-role": "button"})
# Using specific renderer
fa_icon = icon("heart", renderer="fontawesome")
Renderers
SVG Renderer
Renders icons from SVG template files:
EASY_ICONS = {
"svg": {
"renderer": "easy_icons.renderers.SvgRenderer",
"config": {
"svg_dir": "icons",
"default_attrs": {"class": "svg-icon"}
},
"icons": {
"home": "house.svg",
"user": "person.svg"
}
}
}
Provider Renderer
For font icon libraries like Font Awesome:
EASY_ICONS = {
"fontawesome": {
"renderer": "easy_icons.renderers.ProviderRenderer",
"config": {
"tag": "i"
},
"icons": {
"home": "fas fa-home",
"user": "fas fa-user",
"heart": "fas fa-heart"
}
}
}
Sprites Renderer
For SVG sprite sheets:
EASY_ICONS = {
"sprites": {
"renderer": "easy_icons.renderers.SpritesRenderer",
"config": {
"sprite_url": "/static/icons.svg"
},
"icons": {
"logo": "brand-logo",
"menu": "hamburger-menu"
}
}
}
Configuration
Multiple Renderers
You can configure multiple renderers and choose which one to use:
EASY_ICONS = {
"default": {
"renderer": "easy_icons.renderers.SvgRenderer",
"config": {"svg_dir": "icons"},
"icons": {"home": "home.svg"}
},
"fontawesome": {
"renderer": "easy_icons.renderers.ProviderRenderer",
"config": {"tag": "i"},
"icons": {"heart": "fas fa-heart"}
},
"sprites": {
"renderer": "easy_icons.renderers.SpritesRenderer",
"config": {"sprite_url": "/static/icons.svg"},
"icons": {"logo": "brand"}
}
}
Use in templates:
{% icon "home" %} <!-- Uses default renderer -->
{% icon "heart" renderer="fontawesome" %}
{% icon "logo" renderer="sprites" %}
Default Attributes
Configure default attributes that will be applied to all icons from a renderer:
"config": {
"default_attrs": {
"class": "icon",
"aria-hidden": "true",
"height": "1em"
}
}
Note: Template tag attributes completely override default attributes - they don't merge.
Attribute Overriding
Template tag attributes override default attributes:
- Provided attributes completely replace defaults:
height="1em"+height="2em"=height="2em" - This includes class attributes:
class="icon"+class="large"=class="large"
Advanced Usage
Custom Renderers
Create custom renderers by extending BaseRenderer:
from easy_icons.base import BaseRenderer
from django.utils.safestring import SafeString
class CustomRenderer(BaseRenderer):
def render(self, name: str, **kwargs) -> SafeString:
resolved_name = self.get_icon(name)
attrs = self.build_attrs(**kwargs)
html = f'<custom-icon {attrs}>{resolved_name}</custom-icon>'
return self.safe_return(html)
Disable Default Attributes
Use use_defaults=False to ignore default attributes:
{% icon "home" use_defaults=False class="only-this-class" %}
icon("home", use_defaults=False, **{"class": "only-this-class"})
Testing
Run the test suite:
# Install dependencies
poetry install
# Run tests
poetry run pytest
# Run tests with coverage
poetry run pytest --cov=easy_icons --cov-report=html
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
License
This project is licensed under the MIT License. See the LICENSE file for details.
Changelog
Unreleased
- Add
extrakwargsparameter to{% icon %}template tag allowing a mapping of attributes to be merged with direct kwargs (direct kwargs override collisions).
0.1.0
- Initial release
- SVG, Provider, and Sprites renderers
- Template tag support
- Configuration system
- Comprehensive test suite
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_easy_icons-0.5.0.tar.gz.
File metadata
- Download URL: django_easy_icons-0.5.0.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96c098246e0c3775b4ba3d14958578d61f3be3221815764cad7c6f5ea16822f3
|
|
| MD5 |
2623d4c925126ec5fc4b8525e5a7e723
|
|
| BLAKE2b-256 |
1a0e2d0bef7dcc8a4c4e9969195c985b19d5aafd0ad70b4ed9d488549e6dbec4
|
Provenance
The following attestation bundles were made for django_easy_icons-0.5.0.tar.gz:
Publisher:
on-release-main.yml on SamuelJennings/django-easy-icons
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_easy_icons-0.5.0.tar.gz -
Subject digest:
96c098246e0c3775b4ba3d14958578d61f3be3221815764cad7c6f5ea16822f3 - Sigstore transparency entry: 902249907
- Sigstore integration time:
-
Permalink:
SamuelJennings/django-easy-icons@8a316b0a8bfc4bdeb86722f8ed8ef4db311eee72 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/SamuelJennings
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
on-release-main.yml@8a316b0a8bfc4bdeb86722f8ed8ef4db311eee72 -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_easy_icons-0.5.0-py3-none-any.whl.
File metadata
- Download URL: django_easy_icons-0.5.0-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2f777af98abad8e5b5939515b14e73ba6c31f6708211c50878f097dd6e2a90c
|
|
| MD5 |
073fa85880f6923abecaaaa691ef25f2
|
|
| BLAKE2b-256 |
082aedb638dc1946bccebdd6b7d924fa223eabdaf7f5a031e32892355f43f0e7
|
Provenance
The following attestation bundles were made for django_easy_icons-0.5.0-py3-none-any.whl:
Publisher:
on-release-main.yml on SamuelJennings/django-easy-icons
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_easy_icons-0.5.0-py3-none-any.whl -
Subject digest:
e2f777af98abad8e5b5939515b14e73ba6c31f6708211c50878f097dd6e2a90c - Sigstore transparency entry: 902249969
- Sigstore integration time:
-
Permalink:
SamuelJennings/django-easy-icons@8a316b0a8bfc4bdeb86722f8ed8ef4db311eee72 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/SamuelJennings
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
on-release-main.yml@8a316b0a8bfc4bdeb86722f8ed8ef4db311eee72 -
Trigger Event:
push
-
Statement type: