npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

django-lazy-image

v0.5.2

Published

An image utility that renders images lazily as well as intrinsically sizes them.

Downloads

1

Readme

Django Lazy Image

django-lazy-image is a small Django utility that allows you to render images lazily on the frontend to minimise initial page load.

Installation

To install django-lazy-image just run:

pip install django-lazy-image

Requirements

  • Jinja2
  • Django >= 1.11 (It's only been tested on 1.11 but i'm pretty sure it will work on 1.8+)
  • Django Jinja

Configuration

Add django_lazy_image to your INSTALLED_APPS

INSTALLED_APPS = [
    ...
    'django_lazy_image',
    ...
]

Add url(r'^assets/', include('django_lazy_image.urls', namespace='assets')), to your base urls.py

urlpatterns = [
    ...
    url(r'^assets/', include('django_lazy_image.urls', namespace='assets')),
    ...

Then there are 2 ways to use the static files with Django:

NPM (The preferred way)

You can simply do

npm install django-lazy-image

or

yarn add django-lazy-image

Then include the Javascript / CSS as a module

file.js

import LazyImage from 'django-lazy-image';

// Only run the below code if the page has a lazy image instance
const lazyImage = document.querySelector('.js-LazyImage')
if (lazyImage) {
  // Collect all the lazy images on the page
  const lazyImages = document.querySelectorAll('.js-LazyImage')
  // Set up our IntersectionObserver callback
  const callback = (entries, observer) => {
    Array.from(entries).forEach((entry, index) => {
      // If any of the images have come in to view, activate them sequentially
      if (entry.isIntersecting && !entry.target.dataset.activating) {
        entry.target.dataset.activating = true
        window.setTimeout(() => {
          new LazyImage({ el: entry.target })
          observer.unobserve(entry.target)
        }, 150 * index)
      }
    })
  }
  const observer = new IntersectionObserver(callback, {
    threshold: 0.4
  })
  Array.from(lazyImages).forEach(image => observer.observe(image))
}

file.css

@import 'django-lazy-image/dist/css/lazy-image.css';

Django Static

Somewhere in your base.html add:

{{ static('django_lazy_image/css/lazy-image.css') }}
{{ static('django_lazy_image/js/lazy-image.js') }}

You can then use the lazyImage function that is on the global object

var images = [].slice.call(document.querySelectorAll('.js-LazyImage'))
images.forEach(function (el) {
  var image = window.lazyImage(el)
  ...
})