Before looking at how to build a static website using Python, we will look at exactly what a static site is and what the advantages are.
And, since there are already a number of free static site generators, why would you want to write your own?
A static website simply consists of a collection of HTML files on a webserver. When you access a particular URL, the server just sends the file to your browser, as is, unchanged.
PythonInformer is a static website. When you access this page:
our webserver simply sends you the file:
Our webserver has an html file for every single article on the site. It also has an html file for all the special pages, such as the recent articles page. There is also a separate page for each tag and category, listing links to the pages that are tagged or categorised with that string.
Of course, that doesn't mean all those html files are maintained manually - that would be madness! Instead all the article pages are written as ordinary text files in a simple format called markdown. Our custom static site generator combines these files with an html template to create the html files automatically. It also creates the special pages, including the tag and category pages, by collating information from the markdown files:
A dynamic website is one where the html is created dynamically each time a page is accessed. It typically stores the main information in a database. When a page is accessed, it reads data from the database, and combines it with an html template to create the page.
You can use an existing content management system (CMS), of which there are many, including Wordpress, Drupal, Joomla, or Django (the most popular Python based system). Or you can roll your own using various frameworks
This is very useful in some cases. For example, if you have a retail website, each time a user looks at a product you can be sure that they are seeing the current price, and how many of that item are in stock. Since the page is created separately for each user, you can also use their buying history to suggest alternatives, additional purchases, or special offers that might interest them.
However, for a blog or an information site, such as PythonInformer, there are few advantages to using a dynamic system, because you will be showing every user the exact same page. Dynamic systems can be quite heavy - you need to worry about security, backups, updates, etc. Adding even simple features to your site often means using a third party plugin - what if it has bugs, or the person who wrote it stops updating it in a year's time? And all your pages are stored in a database in the specific format of whatever system you are using, so if you started out with Drupal but now think Wordpress might be a better idea, you have a world of pain.
If you site contains static content, there are a number of advantages to using a static site generator rather than a dynamic CMS. The full source of the website is stored offline, as a collection of text files, so:
On the server side:
There are quite a few existing static site generators, such as Jekyll (written in Ruby), Hugo (written in Go), and several written in Python (for example Pelican). They are usually well set up to create a simple blog with very little effort, and have a variety of themes available that range from very good to embarrassingly awful. You can also use them for static non-blog sites such as project documentation, company home pages and tutorial sites.
Another plus point is that they tend to store their content in markdown files, often similar to each other and similar to the method we will develop in this series. So you get all the advantages of text based content and the ability to swap between different static site generators relatively easily.
In fact, if you are new to static site generators, you might even decide to start out using an existing one (I personally would recommend Hugo). It won't be that difficult to change to your own site generator later, the content pages should be almost identical.
So why write your own?
The possible problems with third party generators are:
The benefits of writing your own are:
In the next few articles we will see how to write a fully featured static site generator in Python.<<Prev
Copyright (c) Axlesoft Ltd 2020