Pelican
Setting up a Pelican site with Netlify provides a powerful static publishing workflow using Python. This guide covers the complete process, including creating custom templates, managing assets, and configuring Netlify for continuous deployment in a Python environment.
1. Local Pelican Setup and Project Initialization
Pelican is a Python-based static site generator, requiring a Python environment and pip
.
1.1. Install Prerequisites and Pelican
- Install Python (3.7+ recommended).
- Create a virtual environment (recommended to isolate dependencies):
- Install Pelican and Markdown dependencies:
1.2. Create a New Pelican Site
Use Pelican's quickstart script to generate the basic directory structure.
-
Run the quickstart wizard:
-
Follow the prompts:
- Where do you want to create your new web site?
my-pelican-site
(or preferred name) - Answer the questions regarding Title, Author, URL prefix (usually fine to leave blank for local development), and default language.
- Do you want to specify a URL prefix? No
- Do you want to generate a Fabfile/Makefile? Yes (This creates handy build scripts)
- Where do you want to create your new web site?
-
Navigate into the new directory:
1.3. Local Test
Use the provided Makefile
to generate and serve the site locally.
make html # Generates static files into the output directory (default: output)
make serve # Serves the generated site at http://localhost:8000/
Press Ctrl+C to stop the server.
2. Custom Layouts, Partials, and Static Assets
Pelican uses the Jinja2 templating engine, which organizes templates into the theme
directory.
2.1. Project File/Folder Structure Tree
Your project structure, after customization, will look like this:
my-pelican-site/
├── venv/ # ⚠️ Python Virtual Environment (ignored by Git)
├── output/ # ⚠️ Generated static site (ignored by Git)
├── content/ # Source content files (Markdown, reStructuredText)
│ ├── pages/
│ │ └── about.md # Content for an 'About' page
│ └── first-post.md # Blog post content
├── theme/ # Custom theme files
│ ├── static/ # Static files unique to the theme (CSS, JS)
│ │ └── style.css # Custom CSS file
│ └── templates/ # Jinja2 templates for structure
│ ├── base.html # The main HTML skeleton (Layout)
│ ├── includes/ # Reusable snippets (Partials)
│ │ └── header.html
│ ├── article.html # Template for individual blog posts
│ └── index.html # Template for the homepage/article listing
├── pelicanconf.py # Main site settings (theme path, language, pagination)
├── publishconf.py # Settings specific to production/publishing
├── requirements.txt # Lists Python dependencies for Netlify
└── Makefile # Build scripts
2.2. Configure the Custom Theme
- Create the theme directory structure:
- Update
pelicanconf.py
: Tell Pelican where to find the custom theme. Openpelicanconf.py
and ensure theTHEME
variable points to your custom theme directory:
2.3. Create the Base Layout
The base template defines the common HTML structure and includes the header/footer.
- Create the Base Template:
- File:
theme/templates/base.html
<!DOCTYPE html> <html lang="{{ lang }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{% block title %}{{ SITENAME }}{% endblock %}</title> <link rel="stylesheet" href="{{ SITEURL }}/theme/css/style.css"> </head> <body> {% include 'includes/header.html' %} <main id="content"> {% block content %}{% endblock %} </main> <footer> <p>© {{ CURRENT_YEAR }} {{ AUTHOR }}</p> </footer> </body> </html>
- File:
2.4. Create Partials (Includes)
Partials are reusable snippets placed in the includes
folder.
- Create Header Partial:
- File:
theme/templates/includes/header.html
- File:
2.5. Implement the Article Template
This template extends the base layout and adds specific structure for content pages.
- Create the Article Template:
- File:
theme/templates/article.html
- File:
2.6. Manage Static Assets
Pelican serves assets from the theme's static/
folder using the path /theme/
.
- Add Custom CSS:
- File:
theme/static/css/style.css
- File:
3. GitHub Setup
3.1. Create Dependencies File
Since Netlify will run the build remotely, you need a list of dependencies.
- Create the dependency file:
This file should contain
pelican
andmarkdown
.
3.2. Verify .gitignore
Ensure the build output, environment, and caches are ignored.
touch .gitignore
echo "/venv/" >> .gitignore
echo "/output/" >> .gitignore
echo "/__pycache__/" >> .gitignore
3.3. Commit and Push to GitHub
- Initialize Git:
- Add files and commit:
- Create a new repository on GitHub (e.g., named
my-pelican-site
). - Link your local repo and push: (Replace placeholders)
4. Netlify Setup for Continuous Deployment
Since Pelican is a Python tool, you must configure the Netlify build process to install the necessary environment.
4.1. Sign Up and Import Project
- Go to the Netlify website and log in.
- In the Netlify dashboard, click "Add new site" then "Import an existing project".
- Select "GitHub" and choose your repository (
my-pelican-site
).
4.2. Configure Build Settings
Because Pelican requires a custom environment and command, you must specify them manually.
Setting | Value | Note |
---|---|---|
Branch to deploy | main |
The branch Netlify monitors. |
Build command | pelican content -s publishconf.py |
This command uses the production settings file to generate the final site. |
Publish directory | output |
The directory specified by Pelican's default settings. |
4.3. Netlify Build Environment
Netlify will automatically detect the requirements.txt
file and install Python dependencies. You may optionally set the Python version.
- In the Netlify deploy settings, click "Show advanced".
- Add a new environment variable:
- Key:
PYTHON_VERSION
- Value: (e.g.,
3.10
)
- Key:
4.4. Deploy the Site
Click the "Deploy site" button. Netlify will:
- Detect and install Python and the packages listed in
requirements.txt
. - Run the build command:
pelican content -s publishconf.py
. - Publish the contents of the
output
directory.
Your Pelican site is now live with continuous deployment.
5. Ongoing Workflow
Your Continuous Deployment (CD) pipeline is now fully automated:
- Make changes locally (update content in
content/
, edit templates intheme/templates/
). - Commit your changes:
- Push to GitHub:
- Netlify automatically detects the push, runs the Pelican build process, and deploys the new version live.