Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to run a blog on GitHub

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use Python to run blogs on GitHub. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.

Create blogs with Pelican, a Python-based platform that works well with GitHub.

GitHub is a very popular Web service for source code control that uses Git to synchronize local files with copies kept on GitHub servers so you can easily share and back up your work.

In addition to providing a user interface for code repositories, GitHub also allows users to publish web pages directly from the repository. GitHub's recommended website builder package is Jekll, written in Ruby. Because I'm a big fan of Python, I prefer Pelican, a Python-based blogging platform that works well with GitHub.

Both Pelican and Jekll can convert content written in Markdown or reStructuredText to HTML to generate static websites, and both generators support custom themes.

In this article, I'll show you how to install Pelican, set up the GitHub repository, run the Quick Start Help, write some Markdown files, and publish your first blog post. I assume you have a GitHub account, are familiar with basic Git commands, and want to publish a blog using Pelican.

Install Pelican and create a repository

First, you must install Pelican and ghp-import on your local computer. Using the Python package installer pip(you have one, right?), This is very easy:

$ pip install pelican ghp-import Markdown

Then, open your browser and create a new repository for your fresh blog on GitHub, named as follows (replace username with GitHub username here and throughout the tutorial):

https://GitHub.com/username/username.github.io

Leave it empty and we'll fill it later with compelling blog content.

Using the command line (make sure it's correct), clone this empty Git repository to your local machine:

$ git clone blog $ cd blog Strange tricks…

There is a less obtrusive trick to publishing Web content on GitHub, for user pages hosted in a repository called username.github.io whose content is served by the master branch.

I strongly recommend that all Pelican configuration files and original Markdown files not be kept in master, where only Web content is kept. So I keep the Pelican configuration and original content in a separate branch that I like to call content. (You can create any branch you want, but the following content follows.) I like this structure because I can discard all the files in master and repopulate it with the content branch.

$ git checkout -b content Switched to a new branch 'content' Configuration Pelican

Now it's time to configure the content. Pelican provides a great initialization tool pelican-quickstarter that asks you a series of questions about your blog.

$ pelican-quickstart Welcome to pelican-quickstart v3.7.1. This script will help you create a new Pelican-based website. Please answer the following questions so this script can generate the files needed by Pelican. > Where do you want to create your new web site? [.] > What will be the title of this web site? Super blog > Who will be the author of this web site? username > What will be the default language of this web site? [en] > Do you want to specify a URL prefix? e.g., http://example.com (Y/n) n > Do you want to enable article pagination? (Y/n) > How many articles per page do you want? [10] > What is your time zone? [Europe/Paris] US/Central > Do you want to generate a Fabfile/Makefile to automate generation and publishing? (Y/n) y > Do you want an auto-reload & simpleHTTP script to assist with theme and site development? (Y/n) y > Do you want to upload your website using FTP? (y/N) n > Do you want to upload your website using SSH? (y/N) n > Do you want to upload your website using Dropbox? (y/N) n > Do you want to upload your website using S3? (y/N) n > Do you want to upload your website using Rackspace Cloud Files? (y/N) 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 /Users/username/blog

You can use default values for every question except for the following:

The website title should be unique and special.

Website author, which can be your username or your full name

Time zone, maybe you're not in Paris

Upload to GitHub page, we choose y

After answering all the questions, Pelican leaves the following in the current directory:

$ ls Makefile content/ develop_server.sh* fabfile.py output/ pelicanconf.py publishconf.py

You can check out the Pelican documentation to see how to use these files, but for now we need to finish the job at hand. To be honest, I haven't read the document either.

continue

Add all Pelican generated files to the content branch of your local Git repository, commit your changes, and then push your local changes to a remote repository hosted on Github:

$ git add . $ git commit -m 'initial pelican commit to content' $ git push origin content

This isn't particularly exciting, but it's handy if we need to undo a modification to one of these files.

finally

Finally, you have a blog! All of your blog posts, photos, images, PDFs, etc. will be located in the content directory, which is initially empty. To start creating your first blog post and about page, type:

$ cd content $ mkdir pages images $ cp /Users/username/SecretStash/HotPhotoOfMe.jpg images $ touch first-post.md $ touch pages/about.md

Next, open first-post.md in your favorite text editor and add the following:

title: First Post on My Sweet New Blog date: ! [So Schmexy][my_sweet_photo] Hi, I am and I wrote this epic collection of Interweb wisdom. In days of yore, much of this would have been deemed sorcery and I would probably have been burned at the stake.

The content directory now contains three new Web content items, and there are many more in the content branch.

released

Don't worry, we'll see the results soon!

All that remains is:

Run Pelican to generate static HTML files in output:

$ pelican content -o output -s publishconf.py

Use ghp-import to add the contents of the output directory to the master branch:

$ ghp-import -m "Generate Pelican site" --no-jekyll -b master output

Push the local master branch to the remote repository:

$ git push origin master

Submit new content and push it to the content branch

$ git add content $ git commit -m 'added a first post, a photo and an about page' $ git push origin contentOMG, I succeeded

Now comes the most exciting time, when you want to see what you've posted to everyone, open your browser and type:

https://username.github.io About "How to use Python to run a blog on GitHub" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 211

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report