Lo-Fi Python

Dec 04, 2022

An Example Pelican + Git Blog Post Workflow

In my Ubuntu Linux environment, I'm now publishing new blog posts following this Pelican + Python + git workflow.

  1. Activate the Python environment:
# Create with a virtual env with venv: python -m venv env_name
source env_name/bin/activate
  1. Clone your repo and go to the project folder:
git clone https://github.com/erickbytes/lofipython.git && cd lofipython && ls
  1. Run Python script to create new markdown or .rst file from a template:
python new_post.py
  1. Compile the new post with the Pelican content command:
pelican content
  1. Use the Pelican listen command to serve blog to the default port 8000:
pelican -l
  1. Preview your new post at localhost:8000 in Firefox:
firefox -new-tab http://127.0.0.1:8000
  1. Use git to stage, commit and push the files to a Github repo:
git add .
git commit -m "new post edits and fixes"
git push -u origin main

The new blog post is now live! This is my own workflow for my Pelican blog, this blog which is hosted for free with Cloudflare Pages. You can read more about connecting Pelican and Cloudflare in this past post I wrote.


Github SSH Required

You will need to create a SSH key and connect it to your Github account to get this working, as it's required by Github now. Make sure you write down your passphrase. I was able to create an ssh key with this command:

ssh-keygen -t ed25519 -C "yourname@example.com"

Scripting New Post Creation

Below is the short Python script I wrote for generating the markdown file for a new post, "new_post.py" in the above workflow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from datetime import date


def post_name():
    """Ask for the new post's name."""
    name = input("Enter the new post's title:\n")
    return name


def post_template(title):
    """Return str, post draft"""
    post = f"Title: {title} \nDate: {date.today()} 4:20 \nCategory: Essay"
    return post


def save_draft(name, post):
    """Save new post draft to content folder."""
    content = "~/projects/lofipython/content"
    name = name.replace(" ", "-")
    md = f"{content}/{name}.md"
    with open(md, "w") as fhand:
        fhand.write(post)
    return None


name = post_name()
post = post_template(name)
save_draft(name, post)

I've enjoyed working this out on my new blog. I can easily edit, improve and fire off blog posts rapidly with this command line workflow.