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 your blog on GitHub

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces how to use Python to run your blog on GitHub, it has certain reference value, interested friends can refer to it, I hope you can learn a lot after reading this article, let the editor take you to know it.

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

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

In addition to providing a user interface for the code repository, GitHub also allows users to publish web pages directly from the repository. The website generation package recommended by GitHub is Jekll, which is written in Ruby. Because I am 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 Web sites, and both generators support custom themes.

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

Install Pelican and create a warehouse

First, you must install Pelican and ghp-import on your local computer. Use the Python package installation tool pip (you have it, right? This is very easy:

$pip install pelican ghp-import Markdown

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

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

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

Using the command line (to make sure it is correct), clone the empty Git repository to the local computer:

$git clone blog$ cd blog strange trick...

There is a less noticeable technique for publishing Web content on GitHub. For user pages hosted in a repository called username.github.io, the content is serviced by the master branch.

I strongly recommend that all Pelican configuration files and original Markdown files should not be kept in master, and that only Web content should be retained in master. So I keep the Pelican configuration and the original content in a separate branch that I like to call content. You can create a branch at will, but the following content follows content. I like this structure because I can discard all the files in master and repopulate it with a content branch.

$git checkout-b contentSwitched to a new branch 'content' configure Pelican

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

$pelican-quickstartWelcome 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 filesneeded 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 (YPAPO) n > Do you want to enable article pagination? (YAsia) > 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? (YBO) y > Do you want an auto-reload & simpleHTTP script to assist with theme and site development? 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 Do you want to upload your website using N) n > 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 > Is this your personal page (username.github.io)? (Ycompan N) yDone. Your new project is available at / Users/username/blog

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

Website title, should be unique and special

The website author, which can be a personal user name or your full name

Time zone. Maybe you're not in Paris.

Upload to the GitHub page, we select y

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

$lsMakefile content/ develop_server.sh*fabfile.py output/ pelicanconf.pypublishconf.py

You can check the Pelican documentation to see how to use these files, but now all we have to do is finish the task at hand. To be honest, I didn't read the document either.

Go ahead

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

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

This is not particularly exciting, but it will be very convenient if we need to undo changes to one of these files.

finally

Finally, now you have a blog! All your blog posts, photos, images, PDF, etc., will be located in the content directory, which is initially empty. To start creating the first blog and about the 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 Blogdate:! [So Schmexy] [my_sweet_photo] Hi, I am and I wrote this epic collection of Interwebwisdom. In days of yore, much of this would have been deemed sorceryand I would probably have been burned at the stake. ?

The content directory will now contain three new Web content, and there will be a lot more in the content branch.

Publish

Don't worry, we are about to see the results!

What's left to do is:

Run Pelican to generate a static HTML file 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 content

OMG, I did it.

Now is the most exciting time, when you want to see the blog content you posted to everyone, open the browser and type:

Https://username.github.io

Congratulations on posting your blog on GitHub! When you want to add more pages or articles, you can follow the steps above.

Thank you for reading this article carefully. I hope the article "how to use Python to run your blog on GitHub" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

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: 0

*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

Internet Technology

Wechat

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

12
Report