How to Publish a Pelican Blog via GitHub
Posted on Jul 16, 2018 in Tutorial • 12 min read
How to Publish a Pelican Blog via GitHub¶
Set up environment¶
- Create a blog directory for all blog content and styles:
mkdir <blogname>
- Go into the blog directory:
cd <blogname>
- Create a file called
.gitignore
, and add in the content from this file - Install and activate a virtual environment, Anaconda recommended. See this post for basic conda tutorial
- Create a file called
requirements.txt
in the blog directory with the following content:Markdown==2.6.6 pelican==3.6.3 jupyter>=1.0 ipython>=4.0 nbconvert>=4.0 beautifulsoup4 ghp-import==0.4.1 matplotlib==1.5.1
- Run
pip install -r requirements.txt
in the blog directory to install all of the required packages. Refer to the notes in this post
Create blog¶
Run pelican-quickstart
in the blog directory and follow the interactive setup sequence.
Feel free to hit return to accept the default values besides the following lines:
- the title of the website
- the author of the website
- n for the URL prefix
- timezone
Install Jupyter plugin for Pelican¶
Since Pelican doesn't support writing blog posts using Jupyter by default, we must install a plugin which is available from Github. If you don't have git installed, check here for installation instructions.
- Run
git init
in the blog folder mkdir plugins
git submodule add git://github.com/danielfrg/pelican-ipynb.git plugins/ipynb
- Open
pelicanconf.py
and add the following lines:
The first 3 lines of code tell Pelican to activate the plugin when generating HTML and the last line fixes a common error 'Could not find metadata...'MARKUP = ('md', 'ipynb') PLUGIN_PATHS = ['./plugins'] PLUGINS = ['ipynb.markup'] IGNORE_FILES = ['.ipynb_checkpoints']
Create posts from Jupyter Notebook¶
- Write anything (code or markdown cells) in a jupyter notebook
- Copy the file into the
content
directory in the blog directory - Create a txt file with the same name of your notebook file and an
ipynb-meta
extension. For example:first-post.ipynb-meta
- Its content should be formatted as follows:
Title: First Post Slug: first-post Date: 2018-07-16 20:00 Category: posts Tags: python, tutorial, pelican author: Shayne Mei Summary: This is my first post!
Create a GitHub Page¶
- Log in/Sign up for GitHub
- Create a repo in your account and name it
<username>.github.io
. Substitute<username>
with your GitHub username. Refer to this official guide for repo creation cd
to the blog directory and rungit remote add origin git@github.com:<username>/<username>.github.io.git
- Set
SITEURL
inpublishconf.py
tohttp://<username>.github.io
Pick a theme¶
- Choose a theme here
- Go to where you want to store the themes and run
mkdir theme
cd theme
andgit clone --recursive https://github.com/getpelican/pelican-themes pelican-themes
- Open the
pelicanconf.py
and add this line:
THEME = '<path_to_chosen_theme>'
Publish posts¶
- Go to blog directory:
cd <blogdir>
- Create html from notebook files:
pelican content
- Use correct settings for deployment:
pelican content -s publishconf.py
- Import everything in the output folder to the master branch:
ghp-import output -b master
- Push content to GitHub Pages:
git push origin master
In the future, make sure
- pelican virtual environment is activated
- notebook file containing new post is moved into
content
directory - corresponding
ipynb-meta
file has been created
and then repeat the 5 steps above to publish new posts.