Skip to content
Python "Code over configuration" static site generator
Python HTML CSS
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs
lightweight
tests
.gitignore
.pr-pipeline.yml
.release-pipeline.yml
.use-python.yml
LICENSE
README.md
mypy.ini
requirements.txt
setup.py

README.md

lightweight: a static site generator

PyPI Build Status Test Coverage Supported Python

Code over configuration.

Documentation

Examples

Features

  • Jinja2 templates
  • Markdown rendering with YAML frontmatter
  • Sass/SCSS rendering
  • RSS/Atom feeds (basic implementation with ability to pass only plaintext to feeds)
  • Site nesting
  • Dev server
  • Template project
  • Clean extensible API
  • Fast Enough
  • Fails Fast

Installation

Available from PyPI:

pip install lightweight

Quick Example

from lightweight import Site, markdown, paths, jinja, template, rss, atom, sass


def blog_posts(source):
    post_template = template('_templates_/blog/post.html')
    # Use globs to select files. # source = 'posts/**.md'
    return (markdown(path, post_template) for path in paths(source))

def example(url):
    site = Site(url)
    
    # Render an index page from Jinja2 template.
    site.include('index.html', jinja('index.html'))
    
    # Render markdown blog posts.
    [site.include(f'blog/{post.source_path.stem}.html', post) for post in blog_posts('posts/**.md')]
    site.include('blog.html', jinja('posts.html'))
    
    # Syndicate RSS and Atom feeds.
    site.include('blog.atom.xml', atom(site['blog']))
    site.include('blog.rss.xml', rss(site['blog']))
    
    # Render SASS to CSS.
    site.include('css/global.css', sass('styles/main.scss'))
    
    # Include a copy of a directory.
    site.include('img')
    site.include('js')
    
    return site   

if __name__ == '__main__':
    # Create a site and generate by writing all of it’s content. 
    example(url='https://example.org/').generate()

Create a new project

Initialize a new project using init command:

lw init <location>

It accepts multiple optional arguments:

lw init -h
usage: lw.py init [-h] [--url URL] [--title TITLE] [--authors AUTHORS]
                  location

Generate Lightweight skeleton application

positional arguments:
  location           the directory to initialize site generator in

optional arguments:
  -h, --help         show this help message and exit
  --url URL          the url of the generated site
  --title TITLE      the title of of the generated site
  --authors AUTHORS  comma-separated list of names

Dev Server

Lightweight includes a simple static web server with live reload serving at localhost:8080:

lw serve run:dev

Here site is a Python module

Host and port can be set via:

lw serve run:dev --host 0.0.0.0 --port 80

The directory to resolve the module is provided as --source. It defaults to cwd. The source directory is watched for live reload.

lw serve run:dev --source ~/Projects/example

The live reload can be disabled with --no-live-reload flag:

lw serve run:dev --no-live-reload

Otherwise every served HTML file will be injected with a javascript that polls /__live_reload_id__. The script reloads the page when value at that location changes. That happens after regenerating the site upon change in --source directory.

To stop the server press Ctrl+C in terminal.

You can’t perform that action at this time.