Skip to main content

Python-SSR: Server-Side Rendering with Python

A library for rendering HTML pages on the server, allowing dynamic content generation from APIs.

Python Version License

PyPI Latest Release PyPI Downloads

Table of Contents

What is SSR

Server-Side Rendering (SSR) is a technique where the content of the application is generated on the server and sent to the client as complete HTML. This provides better initial performance and allows search engines to index content more effectively, improving SEO optimization. With SSR, applications offer a faster and smoother user experience, especially on mobile devices, making content delivery more efficient and accessible.

Use Cases

Python-SSR 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-ssr

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 /html/, and the result is rendered in /dist/. 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_ssr 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-SSR 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 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"
    

Metadata

Release files for python-ssr 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-ssr 0.0.4
File Size Uploaded
python-ssr-0.0.4.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-ssr 0.0.4
File Interpreter ABI Platform
python_ssr-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 16.6 kB

Release files / python-ssr-0.0.4.tar.gz

Download URL python-ssr-0.0.4.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
04575783c39e9ca41bd7ffc7ec60db8e2536685d2a8672306ecb6a2f5339b8fb
BLAKE2b-256 checksum
How to use checksums
f5e9616f4a2bb5c8c3bc47d686275b3b97e6122aeeef8e08e2b31fd99cdebf90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.4

Release files / python_ssr-0.0.4-py3-none-any.whl

Download URL python_ssr-0.0.4-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
63e9cd05b04533fae05c7c9afeb968cfbeb60bf6894ddd3b3898a9be35e313e0
BLAKE2b-256 checksum
How to use checksums
41973ada7f8cf89462bbba9095e53cfa9090fb8b6500107a195ad76ea747e633
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page