### Run Django Development Server
Source: https://github.com/samueljennings/django-easy-icons/blob/main/example/README.md
Command to start the Django development server for the easy-icons project. It requires navigating to the project directory and specifying the settings file.
```bash
cd /path/to/django-easy-icons
python manage.py runserver --settings=example.settings
```
--------------------------------
### Install and Configure Django Easy Icons
Source: https://github.com/samueljennings/django-easy-icons/blob/main/README.md
Installation instructions using pip and adding 'easy_icons' to Django's INSTALLED_APPS. This is the initial setup step for using the library.
```bash
pip install django-easy-icons
```
```python
INSTALLED_APPS = [
# ... other apps
'easy_icons',
]
```
--------------------------------
### Python Programmatic Icon Rendering
Source: https://github.com/samueljennings/django-easy-icons/blob/main/example/README.md
Demonstrates how to render icons programmatically in Python using the `easy_icons` library. Includes examples for default rendering, specific renderers, applying classes, and retrieving renderer instances.
```python
import easy_icons
from easy_icons.utils import get_renderer
# Render icons
svg_icon = easy_icons.icon("home")
fa_icon = easy_icons.icon("heart", renderer="fontawesome")
styled_icon = easy_icons.icon("star", renderer="fontawesome", class_="gold")
# Get renderer info (if needed)
fontawesome_renderer = get_renderer("fontawesome")
```
--------------------------------
### Install Dependencies and Run Tests with Poetry
Source: https://github.com/samueljennings/django-easy-icons/blob/main/README.md
Provides commands for setting up the project and running its tests using Poetry. It covers installing project dependencies, running the test suite, and generating a coverage report. This is essential for development and ensuring code quality.
```bash
# Install dependencies
poetry install
# Run tests
poetry run pytest
# Run tests with coverage
poetry run pytest --cov=easy_icons --cov-report=html
```
--------------------------------
### Install and Configure Django Easy Icons
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/index.md
This snippet shows how to install the `django-easy-icons` package using pip and configure it within your Django project's settings. It includes adding the app to `INSTALLED_APPS` and defining basic renderer configurations.
```bash
pip install django-easy-icons
```
```python
INSTALLED_APPS = [
# ... other apps
'easy_icons',
]
EASY_ICONS = {
"default": {
"renderer": "easy_icons.renderers.SvgRenderer",
"config": {
"svg_dir": "icons", # default template directory for SVG files
"default_attrs": {
"height": "1em",
"fill": "currentColor"
}
},
}
}
```
--------------------------------
### Implement Sidebar Navigation with Icons in Django
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
Provides a complete example of creating a sidebar navigation menu using Django templates. It integrates the `icon` template tag to display icons next to navigation links, enhancing visual clarity.
```django
{% load easy_icons %}
```
--------------------------------
### Dynamic Icon Loading Example
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
Demonstrates how to dynamically load icons using Python within a Django application. This snippet would typically be part of a view or utility function that determines which icon to load based on certain conditions or data.
```python
# This is a placeholder for the actual Python code.
# The actual implementation would involve importing necessary modules
# and logic to determine which icon to load dynamically.
# Example:
# from easy_icons.tags import icon
#
def get_dynamic_icon(icon_name, **kwargs):
# Logic to determine icon name or attributes
# For example, based on user role, settings, etc.
# return icon(icon_name, **kwargs)
pass
```
--------------------------------
### Django Template Icon Rendering
Source: https://github.com/samueljennings/django-easy-icons/blob/main/example/README.md
Examples of rendering icons within Django templates using the `easy_icons` template tags. Demonstrates default SVG rendering, specific renderer usage (Font Awesome, Sprites), and attribute customization.
```django
{% load easy_icons %}
{% icon "home" %}
{% icon "home" width="32" height="32" class="large" %}
{% icon "heart" renderer="fontawesome" %}
{% icon "star" renderer="fontawesome" class="gold" %}
{% icon "logo" renderer="sprites" width="24" height="24" %}
```
--------------------------------
### Django Easy Icons Configuration (`settings.py`)
Source: https://github.com/samueljennings/django-easy-icons/blob/main/example/README.md
Configuration for Django Easy Icons, defining multiple renderers (SVG, Font Awesome, Sprites) with their respective settings, default attributes, and icon mappings.
```python
EASY_ICONS = {
# Default SVG renderer
"default": {
"renderer": "easy_icons.renderers.SvgRenderer",
"config": {
"svg_dir": "icons",
"default_attrs": {"height": "1em", "fill": "currentColor"},
},
"icons": {
"home": "home.svg",
"alt_dir": "../alt_dir/alt_dir.svg",
},
},
# Font Awesome provider renderer
"fontawesome": {
"renderer": "easy_icons.renderers.ProviderRenderer",
"config": {
"tag": "i",
"default_attrs": {"class": "fas"},
},
"icons": {
"heart": "fa-heart",
"star": "fa-star",
"admin": "fa-toolbox",
},
},
# SVG sprite renderer (example)
"sprites": {
"renderer": "easy_icons.renderers.SpritesRenderer",
"config": {
"sprite_path": "sprites/icons.svg",
"default_attrs": {"class": "icon"},
},
"icons": {
"logo": "company-logo",
"menu": "hamburger-menu",
},
},
}
```
--------------------------------
### Django Search Form with Icon
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
This example illustrates a search form implementation using Bootstrap's flex utilities and input groups. It features an icon next to the search input field and a 'Search' button with an icon, utilizing the 'search' icon from the library.
```django
{% load easy_icons %}
```
--------------------------------
### Configure Multiple Icon Renderers in Django
Source: https://github.com/samueljennings/django-easy-icons/blob/main/README.md
Example of configuring multiple icon renderers ('default', 'fontawesome', 'sprites') within the `EASY_ICONS` setting in Django. This allows for flexible icon management across different sources.
```python
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"}
}
}
```
--------------------------------
### Testing Renderer Cache with Pytest
Source: https://context7.com/samueljennings/django-easy-icons/llms.txt
An example using `pytest` to test the renderer caching mechanism. It demonstrates how to use `override_settings` to provide a temporary configuration and a fixture to ensure the cache is cleared before each test, guaranteeing isolated test environments.
```python
import pytest
from django.test import override_settings
from easy_icons.utils import clear_cache, icon
@pytest.fixture(autouse=True)
def clear_icon_cache():
"""Clear renderer cache after each test."""
yield
clear_cache()
@override_settings(EASY_ICONS={
"default": {
"renderer": "easy_icons.renderers.SvgRenderer",
"config": {"svg_dir": "test_icons"},
"icons": {"test": "test.svg"}
}
})
def test_custom_config():
# Cache is empty, renderer will be created with test config
result = icon("test")
assert "test.svg" in result
```
--------------------------------
### Render Icons with Template Tags in Django
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
Demonstrates basic and advanced usage of the `icon` template tag to render icons with customizable attributes like CSS classes, height, and title. It also shows how to specify different icon renderers.
```django
{% load easy_icons %}
{% icon 'home' %}
{% icon 'home' class='nav-icon' %}
{% icon 'home' class='nav-icon' height='2em' title='Go Home' %}
{% icon 'heart' renderer='fontawesome' class='text-red' %}
```
--------------------------------
### Configure Default Icon Renderers in Django Settings
Source: https://github.com/samueljennings/django-easy-icons/blob/main/README.md
Example of configuring the default icon set using SvgRenderer, specifying the SVG directory and default attributes for icons. This setup enables basic SVG icon usage.
```python
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"
}
}
}
```
--------------------------------
### Configure Provider Renderer for Font Icons
Source: https://github.com/samueljennings/django-easy-icons/blob/main/README.md
Configuration example for the `ProviderRenderer` in Django settings, suitable for font icon libraries like Font Awesome. It defines the HTML tag to use and maps icon names to font class names.
```python
EASY_ICONS = {
"fontawesome": {
"renderer": "easy_icons.renderers.ProviderRenderer",
"config": {
"tag": "i"
},
"icons": {
"home": "fas fa-home",
"user": "fas fa-user",
"heart": "fas fa-heart"
}
}
}
```
--------------------------------
### Profile Page with Easy Icons
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
Renders a user profile page using django-easy-icons to display various icons for user information, actions, and activity logs. It leverages conditional logic for avatar display and icon selection based on activity type.
```django
{% load easy_icons %}
{% icon 'inbox' class='me-2' %}
No recent activity
{% endfor %}
```
--------------------------------
### Configure SVG Renderer in Django Settings
Source: https://github.com/samueljennings/django-easy-icons/blob/main/README.md
Configuration example for the `SvgRenderer` in Django settings. It specifies the directory for SVG files and default attributes to be applied to rendered SVG icons.
```python
EASY_ICONS = {
"svg": {
"renderer": "easy_icons.renderers.SvgRenderer",
"config": {
"svg_dir": "icons",
"default_attrs": {"class": "svg-icon"}
},
"icons": {
"home": "house.svg",
"user": "person.svg"
}
}
}
```
--------------------------------
### Render Font Awesome Icons with Django Easy Icons
Source: https://github.com/samueljennings/django-easy-icons/blob/main/example/templates/base.html
Demonstrates rendering Font Awesome icons using the `ProviderRenderer`. This requires specifying the renderer in the template tag and uses Font Awesome's CSS classes for icon representation. Examples include basic rendering and applying custom classes.
```django
{% load easy_icons %}
{% icon "heart" renderer="fontawesome" %}
{% icon "star" renderer="fontawesome" class="text-warning" %}
{% icon "user" renderer="fontawesome" class="fs-1 text-primary" %}
{% icon "admin" renderer="fontawesome" %}
```
--------------------------------
### Confirmation Modal with Icons (Django)
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
Implements a Bootstrap confirmation modal using django-easy-icons for the title icon and action buttons. Requires the 'easy_icons' template tag to be loaded and uses standard Bootstrap modal markup.
```django
{% load easy_icons %}
Are you sure you want to delete this item? This action cannot be undone.
```
--------------------------------
### Configure Sprites Renderer for SVG Sprite Sheets
Source: https://github.com/samueljennings/django-easy-icons/blob/main/README.md
Configuration example for the `SpritesRenderer` in Django settings, used for rendering icons from SVG sprite sheets. It requires the URL to the sprite sheet and maps icon names to sprite IDs.
```python
EASY_ICONS = {
"sprites": {
"renderer": "easy_icons.renderers.SpritesRenderer",
"config": {
"sprite_url": "/static/icons.svg"
},
"icons": {
"logo": "brand-logo",
"menu": "hamburger-menu"
}
}
}
```
--------------------------------
### Create Breadcrumb Navigation with Icons in Django
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
Demonstrates how to implement breadcrumb navigation in Django templates using the `icon` template tag. This enhances user experience by providing clear visual cues for navigation hierarchy.
```django
{% load easy_icons %}
```
--------------------------------
### Create Custom Renderer by Extending BaseRenderer
Source: https://context7.com/samueljennings/django-easy-icons/llms.txt
Provides a Python example of creating a custom icon renderer by inheriting from `easy_icons.base.BaseRenderer`. This custom renderer, `CustomRenderer`, generates web component-like HTML tags and can be configured with custom attributes like 'prefix'.
```python
# myapp/renderers.py
from easy_icons.base import BaseRenderer
from django.utils.safestring import SafeString
from typing import Any
class CustomRenderer(BaseRenderer):
"""Custom renderer for web component icons."""
def __init__(self, *, prefix: str = "icon", **kwargs: Any):
super().__init__(**kwargs)
self.prefix = prefix
def render(self, name: str, **kwargs: Any) -> SafeString:
# Resolve icon name through mappings
resolved_name = self.get_icon(name)
# Build HTML attributes from config and kwargs
attrs = self.build_attrs(**kwargs)
# Generate custom HTML structure
html = f'<{self.prefix}-icon name="{resolved_name}" {attrs}>{self.prefix}-icon>'
# Return as safe HTML
return self.safe_return(html)
```
--------------------------------
### Configure Renderers - EASY_ICONS Setting
Source: https://context7.com/samueljennings/django-easy-icons/llms.txt
Provides an example of how to configure the `EASY_ICONS` setting in Django's `settings.py`. This configuration allows for defining multiple icon renderers, specifying their default attributes, and mapping icon names to specific file paths or classes. The setting is a dictionary where keys are renderer names and values are dictionaries of their configurations.
```python
# settings.py
EASY_ICONS = {
"icon_renderers": {
"default": {
"class": "easy_icons.renderers.SvgFragmentRenderer",
"base_dir": "static/svgs", # Directory for SVG files
"default_attrs": {
"fill": "currentColor",
"height": "1em",
"width": "1em"
}
},
"fontawesome": {
"class": "easy_icons.renderers.FontAwesomeRenderer",
"prefix": "fas fa-" # Font Awesome icon prefix
},
"custom_svg": {
"class": "easy_icons.renderers.SvgFragmentRenderer",
"base_dir": "path/to/your/custom/svgs",
"default_attrs": {
"width": "24px",
"height": "24px"
},
"icon_mapping": {
"my_custom_icon": "custom_icon_file.svg"
}
}
}
}
```
--------------------------------
### Django Form Action Buttons with Icons
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
This snippet demonstrates creating styled action buttons for forms (Save, Cancel, Preview, Delete) using the {% icon %} tag. It includes examples for primary, secondary, outline, and danger button styles, incorporating icons like 'save', 'cancel', 'preview', and 'delete'.
```django
{% load easy_icons %}
```
--------------------------------
### Statistics Dashboard with Icons (Django)
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
Displays key statistics using cards, each featuring an icon from django-easy-icons. This snippet requires the 'easy_icons' template tag to be loaded and assumes context variables like 'total_users', 'total_revenue', etc., are available.
```django
{% load easy_icons %}
```
--------------------------------
### Get and Clear Renderer Cache
Source: https://context7.com/samueljennings/django-easy-icons/llms.txt
Explains how to use `get_renderer` to retrieve configured renderer instances, which are cached for performance. `clear_cache` is provided to invalidate the cache, useful for testing or when settings change dynamically.
```python
from easy_icons.utils import get_renderer, clear_cache
from django.core.exceptions import ImproperlyConfigured
# Get a configured renderer instance (cached after first call)
try:
svg_renderer = get_renderer("default")
# Renderer is cached and reused on subsequent calls
fa_renderer = get_renderer("fontawesome")
# Call renderer directly (instances are callable)
home_icon = svg_renderer("home", **{"class": "large"})
heart_icon = fa_renderer("heart")
except ImproperlyConfigured as e:
# Raised when renderer is not configured or cannot be imported
print(f"Configuration error: {e}")
# Clear cache (useful in tests or when settings change)
clear_cache()
```
--------------------------------
### Create SVG Sprite File
Source: https://context7.com/samueljennings/django-easy-icons/llms.txt
An example of an SVG sprite file containing icon symbols. Each symbol is defined with a unique ID (e.g., 'brand-logo') and a path element defining its shape. This file should be placed at the location specified in the sprite_url setting.
```html
```
--------------------------------
### Render SVG Icons with Django Easy Icons
Source: https://github.com/samueljennings/django-easy-icons/blob/main/example/templates/base.html
Example of rendering SVG icons using the default SVG renderer. This shows how to display a basic icon and an icon with custom attributes like width, height, and CSS classes directly within Django templates.
```django
{% load easy_icons %}
{% icon "home" %}
{% icon "home" width="32" height="32" class="text-primary" %}
```
--------------------------------
### Django: Dashboard View with Status Icons
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
This Django view retrieves user data and creates a list of dictionaries, including user information, status icons, and action buttons. It utilizes the `get_status_icon` function to render status icons. The view prepares data for the template, including icons for actions such as viewing and editing user details.
```python
from django.shortcuts import render
from django.urls import reverse
from easy_icons import icon
def dashboard_view(request):
users = User.objects.all()
user_data = []
for user in users:
user_data.append({
'user': user,
'status_icon': get_status_icon(user.status),
'actions': [
{
'icon': icon('eye', class_='text-info'),
'url': reverse('user_detail', args=[user.id]),
'title': 'View'
},
{
'icon': icon('edit', class_='text-warning'),
'url': reverse('user_edit', args=[user.id]),
'title': 'Edit'
}
]
})
return render(request, 'dashboard.html', {'user_data': user_data})
```
--------------------------------
### Alert Messages with Icons (Django)
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/usage-examples.md
Dynamically renders alert messages based on Django's messages framework, using django-easy-icons to display appropriate icons for different message tags (success, error, warning, info). The 'easy_icons' template tag must be loaded.
```django
{% load easy_icons %}
{% if messages %}
{% endif %}
```
--------------------------------
### Configure Sprite Renderer in settings.py
Source: https://context7.com/samueljennings/django-easy-icons/llms.txt
Configures the SpritesRenderer for Django Easy Icons in settings.py. It specifies the path to the sprite file and defines mappings between icon names and their corresponding IDs within the sprite. This setup is essential for rendering icons using SVG sprites.
```python
EASY_ICONS = {
"sprites": {
"renderer": "easy_icons.renderers.SpritesRenderer",
"config": {
"sprite_url": "/static/icons.svg", # Required - path to sprite file
"default_attrs": {
"class": "icon",
"height": "1em",
"width": "1em",
"fill": "currentColor"
}
},
"icons": {
"logo": "brand-logo",
"menu": "hamburger",
"close": "x",
"search": "magnify"
}
}
}
```
--------------------------------
### ProviderRenderer - Font Icon Libraries Configuration and Usage
Source: https://context7.com/samueljennings/django-easy-icons/llms.txt
Configures the ProviderRenderer for integrating font icon libraries like Font Awesome and Material Icons. It defines the HTML tag to use and default attributes, mapping icon names to their respective class names. Includes template usage examples and Python integration.
```python
# settings.py
EASY_ICONS = {
"fontawesome": {
"renderer": "easy_icons.renderers.ProviderRenderer",
"config": {
"tag": "i", # Use or
"default_attrs": {
"aria-hidden": "true"
}
},
"icons": {
"home": "fas fa-home",
"user": "fas fa-user",
"heart": "fas fa-heart",
"star": "fas fa-star",
"check": "fas fa-check-circle"
}
},
"bootstrap": {
"renderer": "easy_icons.renderers.ProviderRenderer",
"config": {
"tag": "i",
},
"icons": {
"house": "bi bi-house",
"person": "bi bi-person",
"cart": "bi bi-cart"
}
}
}
# In template:
{% load easy_icons %}
{% icon "heart" renderer="fontawesome" class="text-danger fs-4" %}
{% icon "cart" renderer="bootstrap" class="ms-2" %}
# Output:
#
#
# Python usage:
from easy_icons import icon
fa_heart = icon("heart", renderer="fontawesome", **{"class": "text-danger"})
#
```
--------------------------------
### Output Example for SpritesRenderer (HTML)
Source: https://github.com/samueljennings/django-easy-icons/blob/main/docs/renderers.md
Demonstrates the HTML output generated by the SpritesRenderer. Each icon is rendered as an SVG element with a nested `