How to create a GitHub Pages using Pelican and Google Colaboratory

Pelican is known as a blog generator and uses Python. I want to update my blog easily with web based.

Work flow

  1. Create a empty repository for GitHub Pages. (username.github.io)
  2. Generate a personal access token. it's alternative way to not use a GitHub password.
  3. Coding on Colaboratory.
  4. Initialize a blog with Pelican.
  5. Write a article and publish.

It'll be using this.

  • Google Colaboratory
  • Google Drive

How to do

Create a empty repository

The repository name needs to be username.github.io (ex. tmya.github.io). and will be URL is https://username.github.io/. username is github account's username.

Generate a personal access token

You can generate it here. Personal access tokens

Press Generate new token button and check repo checkbox. Note field can be anything, or blog for simplicity. Generated access token use it later.

Coding on Colab

Open your Google Drive

Create a folder for blog on Google Drive. (ex. pelican-blog) My Drive > pelican-blog and create Colaboratory. (Right mouse click -> More -> Google Colaboratory)

Next, connect the Colaboratory to Google Drive. Press File and then Mount google drive then it'll ask you allow access for Google Drive. Let's allow.

NOTE: If you save a file in Colaboratory, it'll all disappear after a certain period of time. so you should need to safe save for Google Drive.

Initialize a blog with Pelican.

NOTE: cd is a Linux command. Add ! or % to the head of the command for execute Linux command. but different between ! and %. ! will be executed temporarily and has no other effect. For example, !cd is will be executed cd, but the directory will not be changed at the end of execution. % is continue to have an effect.

Add a new column of code on Colaboratory then type below.

%cd /content/drive/MyDrive/pelican-blog
!pip install "pelican[markdown]" ghp-import
!git init
!pelican-quickstart

Let's execute cell once.

%cd /content/drive/MyDrive/pelican-blog

  • Change the current working directory.

!pip install "pelican[markdown]" ghp-import

  • I want to use markdown, so I'll install pelican[markdown] instead of pelican.
  • ghp-import is an easy way to upload to git. Create a gh-pages branch in the specified directory and push it to the remote master.

!git init

  • initialize git in the current working directory.

!pelican-quickstart

  • It will ask you some questions. Answer them appropriately.
  • Where do you want to create yoru new web site? -> Enter
  • Do you want to upload your website using Github Pages? -> y Enter
  • Is this your personal page (username.github.io)? -> y Enter

Finally, You can see result.

> Where do you want to create your new web site? [.] 
> What will be the title of this web site? blog
> Who will be the author of this web site? user
> What will be the default language of this web site? [en] 
> Do you want to specify a URL prefix? e.g., https://example.com   (Y/n) y
> Do you want to enable article pagination? (Y/n) 
> How many articles per page do you want? [10] 
> What is your time zone? [Etc/UTC] 
ing? (Y/n) 
> Do you want to upload your website using FTP? (y/N) 
> Do you want to upload your website using SSH? (y/N) 
> Do you want to upload your website using Dropbox? (y/N) 
> Do you want to upload your website using S3? (y/N) 
> Do you want to upload your website using Rackspace Cloud Files? (y/N) 
> Do you want to upload your website using GitHub Pages? (y/N) y
> Is this your personal page (username.github.io)? (y/N) y
Done. Your new project is available at /content/drive/My Drive/pelican-blog

Write a article and publish

Write some article and save content directory. ( MyDrive -> pelican-blog -> content)

Add a new column of code different from before then type below.

!pip install "pelican[markdown]" ghp-import > /dev/null
%cd /content/drive/MyDrive/blog
!make publish
!ghp-import output
!git push https://USERNAME:ACCESS_TOKEN@github.com/USERNAME/USERNAME.github.io.git gh-pages:master

NOTE: Again, you will need to reinstall the module because the data in colaboratory will disappear after a certain period of time. but the blog data is safely stored in Google Drive.

!pip install "pelican[markdown]" ghp-import > /dev/null %cd /content/drive/MyDrive/blog

  • Same reason as above.
  • /dev/null is not recommended, but you'll get a long, long log, so used that. As an alternative, you can use > log.txt better than > /dev/null.

!make publish

  • Pelican generate static publish html into output directory for blog.

!ghp-import output

  • Create gh-pages branch and commit all output directory's file.

!git push https://USERNAME:ACCESS_TOKEN@github.com/USERNAME/USERNAME.github.io.git gh-pages:master

  • Change USERNAME to your username. Change ACCESS_TOKEN to your access token obtained in the some previous step.

Let's execute cell. Run the cell every time you write or edit an article.

If result with no errors, you can see blog on https://username.github.io after a few minutes.

By @Akio Tomita in
Tags :