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 🔗

You might also like...
Kaktos is a python static site generator
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

barely is a lightweight, but highly extensible static site generator written in pure python.
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

A python-based static site generator for setting up a CV/Resume site
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

The lektor static file content management system
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

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

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

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

HTML Template Linter and Formatter. Use with Django, Jinja, Nunjucks and Handlebars templates.
HTML Template Linter and Formatter. Use with Django, Jinja, Nunjucks and Handlebars templates.

Find common formatting issues and reformat HTML templates. Django · Jinja · Nunjucks · Handlebars · Mustache · GoLang Ps, --check it out on other temp

Use heroicons in your Django and Jinja templates.

heroicons Use heroicons in your Django and Jinja templates. Requirements Python 3.6 to 3.9 supported. Django 2.2 to 3.2 supported. Are your tests slow

JTEX is a command line tool (CLI) for rendering LaTeX documents from jinja-style templates.
JTEX is a command line tool (CLI) for rendering LaTeX documents from jinja-style templates.

JTEX JTEX is a command line tool (CLI) for rendering LaTeX documents from jinja-style templates. This package uses Jinja2 as the template engine with

This is a simple website crawler which asks for a website link from the user to crawl and find specific data from the given website address.

This is a simple website crawler which asks for a website link from the user to crawl and find specific data from the given website address.

Blender Game Engine Game Type Templates Logic Bricks (and Python script) based Game Templates for Blender

Blender-Game-Engine-Templates Blender Game Engine Game Type Templates Logic Bric

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

Static Features Classifier - A static features classifier for Point-Could clusters using an Attention-RNN model

Static Features Classifier This is a static features classifier for Point-Could

Easy to use phishing tool with 63 website templates. Author is not responsible for any misuse.
Easy to use phishing tool with 63 website templates. Author is not responsible for any misuse.

PyPhisher [+] Created By KasRoudra [+] Description : Ultimate phishing tool in python. Includes popular websites like facebook, twitter, instagram, gi

Easy to use phishing tool with 65 website templates. Author is not responsible for any misuse.
Easy to use phishing tool with 65 website templates. Author is not responsible for any misuse.

PyPhisher [+] Description : Ultimate phishing tool in python. Includes popular websites like facebook, twitter, instagram, github, reddit, gmail and m

Jinja is a fast, expressive, extensible templating engine.

Jinja is a fast, expressive, extensible templating engine. Special placeholders in the template allow writing code similar to Python syntax.

Django/Jinja template indenter

DjHTML A pure-Python Django/Jinja template indenter without dependencies. DjHTML is a fully automatic template indenter that works with mixed HTML/CSS

Automatizando a criação de DAGs usando Jinja e YAML

Automatizando a criação de DAGs no Airflow usando Jinja e YAML Arquitetura do Repo: Pastas por contexto de negócio (ex: Marketing, Analytics, HR, etc)

Owner
Hartley Brody
Hartley Brody
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 1, 2023
A static website and blog generator

Nikola, a Static Site and Blog Generator In goes content, out comes a website, ready to deploy. Why Static Websites? Static websites are safer, use fe

Nikola, a static site generator 2.4k Jan 5, 2023
a static website generator to make beautiful customizable pictures galleries that tell a story

Prosopopee Prosopopee. Static site generator for your story. Make beautiful customizable pictures galleries that tell a story using a static website g

Bram 259 Dec 19, 2022
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
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 1, 2023
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 9, 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
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 website.

Kailash Nadh 40 Nov 20, 2022
Static site generator that supports Markdown and reST syntax. Powered by Python.

Pelican Pelican is a static site generator, written in Python. Write content in reStructuredText or Markdown using your editor of choice Includes a si

Pelican dev team 11.3k Jan 4, 2023
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

null 2 Nov 7, 2022