Create a simple static website using python and jinja templates.

Overview

Simple Static

Create a simple static website using python and jinja templates.

Simple Static has four pieces:

  1. A build command that renders jinja templates to an output directory
  2. A serve command that runs a local server & watches for changes to rebuild the site
  3. A config parser that reads an optional config.yaml file to specify settings & add global context variables
  4. Support for collecting a series of templates as "posts" and gathering common information to render them in a list

That's it.


Getting Started

Install via pip

pip install simple_static

A basic file structure for your project looks like this, pretty typical for static sites

site/           # directory with .html jinja templates and other static files
_build/         # directory where the site gets built to
config.yaml     # optional yaml file

The package has two simple commands you can run from inside the root of your website, which is the directory that contains the site and _build directories as well as the optional config.yaml file.

Neither command takes parameters, everything is controlled via the config.yaml file, as described below.

Run a one-time build of your website

build

Run a local server that serves your site and also watches for changes to files to trigger a rebuild

serve

By default, your site will be served at http://localhost:8383.

The Basics

There's an INPUT_DIR (defaults to site) which points to the folder that contains your jinja templates, as well as other static files (css, javascript, etc). There's also an OUTPUT_DIR (defaults to _build) that gets written to every time the site is rebuilt.

🧹 Don't edit any of the files in the OUTPUT_DIR directly as these are overwritten every time the site is rebuilt.

When setting up your static website:

  • you can use any directory structure you like inside INPUT_DIR
  • jinja templates should end in .html
  • all other files in your site will be copied to OUTPUT_DIR as-is
  • see below for how to setup a collection of posts

You can use all of the jinja features you expect like base templates and inheritance. If you've never worked with jinja before, check out their helpful Template Designer Documentation to learn the basics.

Template file names like about.html will be built to a file like about/index.html so that your site can have "pretty URLs" (ie /about/).

When jinja renders a template, we provide a "context" which is a set of variables that can be accessed in that template file. The two ways to add things to the global site context are via config and posts.

Config

Having a config.yaml at the outer level of your project is optional, but it allows you to both configure the site and also add global context variables you can use in your jinja templates.

Here's an example file that specifies the default site configuration values, and also adds a context variable:

INPUT_DIR: "site"
OUTPUT_DIR: "_build"
LOCAL_HOST: "localhost"
LOCAL_PORT: 8383
SORT_POSTS_BY: created_at

NAV_PAGES:
  - name: About
    url: /about/

  - name: Projects
    url: /projects/

You can add arbitrary keys to config.yaml and they will be added as context variables in all jinja templates. This is great for keeping track of site-wide things like nav menu items or the URL to your site's logo.

Note that the key names are all lowercased before being passed to jinja, so in the example above, you could render the nav items with {% for nav_page in nav_pages %}... in a jinja template.

If you make changes to your config.yaml file, you'll need to restart the serve command for them to be picked up, sorry about that.

Posts

Posts are the basic unit for creating a feed of content. They could be anything -- employee pages, case studies of client work or simply blog entries. A post is no different than a regular page on the site, the main reason to use posts is if you'll want to render some sort of list or archive page that shows a quick snippet of each post on one page.

The posts feature allows you to extract some common metadata from each of the post templates in a collection (via their block names in the jinja templates), and add that to the global site context.

In order to treat a collection of templates as a "series of posts" you simply put them in a directory (for example, projects) and add an empty file called .posts to that directory. That's it!

When the site is built, each of the templates inside that directory will get converted to a dict of block_name: block_content, and then added to a list. This list is sorted by the value inside the block named by SORT_POSTS_BY (which defaults to "created_at"), and the list gets added to the global site context, as the name of the directory (ie projects). Each post also automatically gets a url key that can be used for building links to that page on your site.

Here's an example:

site/
    base.html
    index.html
    projects/
        .posts  # this file is empty, but triggers the "posts" collection
        foo.html
        bar.html

Here's what might be in projects/foo.html

{% extends "base.html" %}

{% block title %}Foo Project{% endblock %}
{% block created_at %}1970-01-01{% endblock %}
{% block desc %}
    Here is a quick blub that describes the first post ever!
{% endblock %}

{% block content %}
    Here is the content of the post that may be much longer.
{% endblock %}

Note that each of those blocks inside the template are completely optional. The only one with any signifigance is created_at since it's the default name of the block used to sort the posts, but that can be changed in your config.yaml by changing the value of SORT_POSTS_BY.

Then, inside your index.html template file (or any other template file), you could have a snippet like this, which would print out the title and desc block for each of the posts, with a handy link to it.

Read more... {% endfor %}">
{% for project in projects %}
    

{{project.title}}

{{project.desc}}

Read more... {% endfor %}

Here, you see that you have access to a context variable called "projects" since that's the name of the directory that contains the .post file. The name for each of the attributes of a post ("title", "desc", etc) is totally up to you, and is taken from the name of the jinja block in the template file for that post. Try to make sure that all of the posts in a collection have the same named blocks, so you can print things correctly when looping through them.

Contributing

This is my first time publishing a python project to PyPi, and I'd love to hear from you if you use it.

Feel free to open an issue if you need any help or submit a PR if you want to fix anything or add features.

You can also tweet me a link if you use this to build a website! I may even add a link to your site here as well 🔗

Owner
Hartley Brody
Hartley Brody
AutoLoader is a plugin for Pelican, a static site generator written in Python.

AutoLoader AutoLoader is a plugin for Pelican, a static site generator written in Python. AutoLoader is designed to autoload the other Pelican plugins

2 Nov 07, 2022
A declarative website generator designed for high-quality websites, with a focus on easy maintenance and localization.

Grow Grow is a declarative tool for rapidly building, launching, and maintaining high-quality static HTML. Easy installation Jinja template engine Con

Grow 385 Dec 03, 2022
Kaktos is a python static site generator

Python static site generator κάκτος Kaktos is a python static site generator. The idea is create a simple static site generator for people that don't

Paulo Coutinho 4 Sep 21, 2022
Jekyll is a simple, blog-aware, static site generator perfect for personal, project, or organization sites.

Jekyll Jekyll is a simple, blog-aware, static site generator perfect for personal, project, or organization sites. Think of it like a file-based CMS,

Jekyll 45.8k Dec 31, 2022
The lektor static file content management system

Lektor Lektor is a static website generator. It builds out an entire project from static files into many individual HTML pages and has a built-in admi

Lektor CMS 3.6k Dec 29, 2022
Simple, lightweight, and magic-free static site/blog generator for Python coders

makesite.py Take full control of your static website/blog generation by writing your own simple, lightweight, and magic-free static site generator in

Sunaina Pai 1.7k Jan 01, 2023
dirmaker is a simple, opinionated static site generator for quickly publishing directory websites.

dirmaker is a simple, opinionated static site generator for publishing directory websites (eg: Indic.page, env.wiki It takes entries from a YAML file and generates a categorised, paginated directory

Kailash Nadh 40 Nov 20, 2022
Static site generator for designers. Uses Python and Django templates.

News Cactus 3 is out! We're happy to announce Cactus 3. It brings a set of great new features like asset fingerprinting, an asset pipeline, pretty url

3.4k Jan 01, 2023
Makes dynamic linked shit "static". Amazing

static.py What does it do? You give it a dynamically linked binary and it will make a directory that has all the dependencies (recursively). It also f

Stephen Tong 24 Dec 16, 2022
Hobby Project. A Python Library to create and generate static web pages using just python.

PyWeb 🕸️ 🐍 Current Release: 0.1 A Hobby Project 🤓 PyWeb is a small Library to generate customized static web pages using python. Aimed for new deve

Abhinav Sinha 2 Nov 18, 2021
barely is a lightweight, but highly extensible static site generator written in pure python.

barely is a lightweight, but highly extensible static site generator. Explore the docs » Quickstart · See available Plugins · Report Bug · Request Fea

40 Dec 01, 2022
Minimal Static Blog Generator in Python

Minimal Static Blog Generator in Python Simple static blog generator, written in Python, with the top Lighthouse scores. Missing 'Best Practices' and

Danial Goodwin 1 Apr 20, 2022
Create a simple static website using python and jinja templates.

Simple Static Create a simple static website using python and jinja templates. Simple Static has four pieces: A build command that renders jinja templ

Hartley Brody 6 Sep 05, 2022
A simple static site generator with deployment to S3/Cloudfront.

Stasis A simple static site generator with deployment to S3/Cloudfront. Features Stasis is a static website generator written in Python, using Pandoc

Scott Czepiel 56 Sep 29, 2022
Tinkerer is a blogging engine/static website generator powered by Sphinx.

Tinkerer What is Tinkerer? Tinkerer is a blogging engine/static website generator powered by Sphinx. It allows blogging in reStructuredText format, co

Vlad Riscutia 307 Dec 06, 2022
Simple Static Site Inductor Made in Python

sssimp 🐍 Simple Static Site Inductor Made in Python How to use Create a folder called input, inside create a folder called content and an empty file

Tina 11 Oct 09, 2022
A python-based static site generator for setting up a CV/Resume site

ezcv A python-based static site generator for setting up a CV/Resume site Table of Contents What does ezcv do? Features & Roadmap Why should I use ezc

Kieran Wood 5 Oct 25, 2022
A Python media index

pyvideo https://pyvideo.org is simply an index of Python-related media records. The raw data being used here comes out of the pyvideo/data repo. Befor

pyvideo 235 Dec 24, 2022
A Python Static Website Generator

Version 0.8.9 Overview Hyde starter kit by merlinrebrovic is a really nice way to get started with hyde. Hyde layout for bootstrap by auzigog is also

Hyde - Static Website Generator 1.6k Jan 01, 2023
A static website generator for people who enjoy the simpler things in life.

A static website generator for people who enjoy the simpler things in life.

Darren Mulholland 93 Dec 22, 2022