Skip to main content

A Python SSG library for dynamically generating static websites

Project description

Python-SSG: Static Site Generation with Python

A library for generating HTML pages on the server, allowing dynamic content updates from APIs while serving static files.

Python Version License

PyPI Latest Release PyPI Downloads

Table of Contents

What is SSG

Static Site Generation (SSG) is a technique where HTML pages are generated on the server and served as static files. This allows for better initial performance and improved SEO optimization, as search engines can easily index the content. With SSG, applications provide a faster and smoother user experience, especially on mobile devices, making content delivery more efficient and accessible. The key advantage of SSG is that the pages can be dynamically updated with data from APIs while remaining static in nature.

Use Cases

Python-SSG is ideal for those who need to generate static pages that dynamically update with data from APIs. Here are some use cases:

  • Personal Blogs: Dynamically update the main and post pages with the latest content at defined intervals.
  • Report Pages: Generate pages with report data that automatically update, using APIs that provide statistics or charts.
  • Dynamic Landing Pages: Create landing pages that update as the API receives new data, such as promotions or product updates.

Installation

To install the library, you can use the following command:

pip install python-ssg

Usage

  1. Create a configuration file (config.json):

    The file should contain the following structure:

    {
      "pages": {
          "index": {
              "api": {
                  "url": "http://127.0.0.1:5000/api/index",
                  "method": "GET"
              },
              "render_interval": 5
          }
      }
    }
    

Each page corresponds to an HTML template file in the /html/ directory, and the result is rendered in the /dist/ directory. The defined API returns JSON, which is used to populate the template. This JSON can come from local APIs, as in the example, or any external API you wish to use.

  1. Start Rendering:

    To start rendering, you should call the start_rendering function and pass the path of your configuration file.

    from python_ssg import start_rendering
    
    if __name__ == "__main__":
        start_rendering("config.json")
    

Example Structure for Usage

Here is an example directory structure for using the library:

your_project/
  - config.json
  - html/
    - assets/
      - (Your folders and files)
    - index.html
    - (Your HTML files)
  - dist/
    - (rendered HTML files)
  - main.py

Example of index.html

An example HTML file that uses variables for rendering:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="assets/style.css">
    <title>{{ title }}</title>
</head>
<body>
    <h1>{{ title }}</h1>
    <p>{{ description }}</p>
    <script src="assets/script.js"></script>
</body>
</html>

Contribution

Feel free to explore, open issues, suggest pull requests, or just enjoy learning from the code!

Future Implementations: Integration with AWS S3

A planned future improvement for the project is the possibility of integrating Python-SSG with AWS S3. This will allow the generated static pages to be automatically uploaded to a bucket on S3, keeping the site online and always updated with the latest content without the need to manage servers.

It’s not implemented yet, but it would be extremely useful for those looking to automate the update of static pages hosted in the cloud, keeping everything synchronized with real-time APIs.

Running Tests

To ensure that all library features are working correctly, it is important to run tests. You can do this using Python's unittest module.

Follow the steps below to run the tests:

  1. Navigate to the root directory of your project.

  2. Execute the following command:

    python -m unittest discover -s tests -p "*.py"
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-ssg-0.0.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_ssg-0.0.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file python-ssg-0.0.1.tar.gz.

File metadata

  • Download URL: python-ssg-0.0.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for python-ssg-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a6fe95054ca28105d01b11da2d063db973f500aeadc1925c550d5749a93ee416
MD5 2c2f80a72965af1881572dfc8a4b4cd7
BLAKE2b-256 3206456dbe6f3c0b07379e8baed0b6a22026f5b175357d8fcc52d86351c7d716

See more details on using hashes here.

File details

Details for the file python_ssg-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: python_ssg-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for python_ssg-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e0a9c1bea099209ddbf13784ecc6440ba88c47e6644f16b7a2b87d3fdc8cbffb
MD5 4120004a2c8fee6de890a952ca3a0d1e
BLAKE2b-256 403448c27e5a7882761c4cbf3e6c4b1dee061015fa9051f9a1efc7cdd8185072

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page