Skip to main content

Use heroicons in your Django and Jinja templates.

Project description

https://img.shields.io/github/actions/workflow/status/adamchainz/heroicons/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/heroicons.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Use heroicons in your Django and Jinja templates.


Improve your Django and Git skills with my books.


Requirements

Python 3.10 to 3.14 supported.

Django 4.2 to 6.0 supported.

Usage

The heroicons package supports both Django templates and Jinja templates. Follow the appropriate guide below.

Django templates

  1. Install with python -m pip install heroicons[django].

  2. Add to your INSTALLED_APPS:

    INSTALLED_APPS = [
        ...,
        "heroicons",
        ...,
    ]

Now your templates can load the template library with:

{% load heroicons %}

Alternatively, make the library available in all templates by adding it to the builtins option:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        # ...
        "OPTIONS": {
            # ...
            "builtins": [
                ...,
                "heroicons.templatetags.heroicons",
                ...,
            ],
        },
    }
]

The library provides these tags to render SVG icons in their corresponding styles:

  • heroicon_micro

  • heroicon_mini

  • heroicon_outline

  • heroicon_solid

The tags take these arguments:

  • name, positional: the name of the icon to use. You can see the icon names on the heroicons.com grid.

  • size, keyword: an integer that will be used for the width and height attributes of the output <svg> tag. Defaults to the icons’ designed sizes: 24 for outline and solid, 20 for mini, and 16 for micro. Can be None, in which case no width or height attributes will be output.

  • Any number of keyword arguments. These will be added as attributes in the output HTML. Underscores in attribute names will be replaced with dashes, allowing you to define e.g. data- attributes.

    Most attributes will be added to the <svg> tag containing the icon, but these attributes will be attached to the inner <path> tags instead:

    • stroke-linecap

    • stroke-linejoin

    • vector-effect

Note: unlike the SVG code you can copy from heroicons.com, there is no default class.

Examples

An outline “academic-cap” icon:

{% heroicon_outline "academic-cap" %}

The same icon, solid, at 40x40 pixels, and a CSS class:

{% heroicon_outline "academic-cap" size=40 class="mr-4" %}

That icon again, but with the paths changed to a narrower stroke width, and a “data-controller” attribute declared:

{% heroicon_outline "academic-cap" stroke_width=1 data_controller="academia" %}

Jinja templates

  1. Install with python -m pip install heroicons[jinja].

  2. Adjust your Jinja Environment to add the global heroicon_* functions from heroicons.jinja. For example:

    from heroicons.jinja import (
        heroicon_micro,
        heroicon_mini,
        heroicon_outline,
        heroicon_solid,
    )
    from jinja2 import Environment
    
    env = Environment()
    env.globals.update(
        {
            "heroicon_micro": heroicon_micro,
            "heroicon_mini": heroicon_mini,
            "heroicon_outline": heroicon_outline,
            "heroicon_solid": heroicon_solid,
        }
    )

Now in your templates you can call those functions, which render <svg> icons corresponding to the icon styles in the set. The functions take these arguments:

  • name, positional: the name of the icon to use. You can see the icon names on the heroicons.com grid.

  • size, keyword: an integer that will be used for the width and height attributes of the output <svg> tag. Defaults to the icons’ designed sizes: 24 for outline and solid, 20 for mini, and 16 for micro. Can be None, in which case no width or height attributes will be output.

  • Any number of keyword arguments. These will be added as HTML attributes to the output HTML. Underscores in attribute names will be replaced with dashes, allowing you to define e.g. data- attributes.

    Most attributes will be added to the <svg> tag containing the icon, but these attributes will be attached to the inner <path> tags instead:

    • stroke-linecap

    • stroke-linejoin

    • vector-effect

Note: unlike the SVG code you can copy from heroicons.com, there is no default class.

Examples

An outline “academic-cap” icon:

{{ heroicon_outline("academic-cap") }}

The same icon, solid, at 40x40 pixels, and a CSS class:

{{ heroicon_solid("academic-cap", size=40, class="mr-4") %}

That icon again, but with the paths changed to a narrower stroke width, and a “data-controller” attribute declared:

{{ heroicon_outline("academic-cap", stroke_width=1, data_controller="academia") %}

CLI

Many icons were renamed in version 2 of heroicons. To assist you with migrating from version 1, this package includes a CLI that can update your heroicons template tags.

Invoke the CLI like so:

$ python -m heroicons update <filename> <filename2> ...

To run it on all your template files, you can use git ls-files | xargs:

$ git ls-files -- '*.html' | xargs python -m heroicons update

The tool will update icon names for those that were renamed in v2, as per the table in the heroicons release notes. It should find both Django and Jinja template tags:

-{% heroicon_outline "archive" class="mr-2" %}
+{% heroicon_outline "archive-box" class="mr-2" %}

-{{ heroicon_solid("archive", class="mr-2") }}
+{{ heroicon_solid("archive-box", class="mr-2") }}

Also note that solid icons changed their default size from 20px to 24px. If you are using them without specifying a size, they will now be larger, which could break some designs. You can keep the v1 size by specifying it exactly:

{% heroicon_solid "archive-box" size=20 %}
{{ heroicon_solid("archive-box", size=20) }}

Or through other mechanisms:

  • Tailwind’s width and height classes: w-5 h-5

  • other CSS classes

  • sizing the containing elements

Due to the variety of ways to size icons, it’s unfortunately not possible to automatically add the size to unsized solid icons.

Good luck, and may the odds be ever in your favour.

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

heroicons-2.14.0.tar.gz (420.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

heroicons-2.14.0-py3-none-any.whl (415.0 kB view details)

Uploaded Python 3

File details

Details for the file heroicons-2.14.0.tar.gz.

File metadata

  • Download URL: heroicons-2.14.0.tar.gz
  • Upload date:
  • Size: 420.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for heroicons-2.14.0.tar.gz
Algorithm Hash digest
SHA256 e55ecc0a839cf872f55977d2dd04a1855bfdf080bed1d13db5264b0060e65a96
MD5 89e0c45ad25930ac608055916c040112
BLAKE2b-256 5ffeed8a483bbd518f421b891a3db4eb61c6f2a5f84886a92a80b82d0d6637b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for heroicons-2.14.0.tar.gz:

Publisher: main.yml on adamchainz/heroicons

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file heroicons-2.14.0-py3-none-any.whl.

File metadata

  • Download URL: heroicons-2.14.0-py3-none-any.whl
  • Upload date:
  • Size: 415.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for heroicons-2.14.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cdf7fa6de02b7bc5b4513d7f592a9a1faea150b050a3b4270a0dc445ebe930c9
MD5 7692df64a22718ca8f5e15b207894a2e
BLAKE2b-256 08895e1511183a70edb8b10fed0507b76726fe060056c952a53a2484be96db24

See more details on using hashes here.

Provenance

The following attestation bundles were made for heroicons-2.14.0-py3-none-any.whl:

Publisher: main.yml on adamchainz/heroicons

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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