In this article, I will guide you step by step to create a dynamic documentation site, adaptable to any project, where you can connect your documentation to a database to extract and display data, ensuring the information is always up to date. We will also explore how to automate the entire process, from content generation to deployment in the cloud with AWS.
The solution includes support for charts and diagrams, continuous integration (CI/CD) using a simple workflow in GitHub Actions, and automatic deployment using Terraform. Let’s get started!
Tools for Documentation as Code
For the development of these sites, it is essential to understand some practices and tools that allow us to implement this approach. Below is a detailed list of the most important aspects to cover in this tutorial.
- 📝 Markdown: The most common markup language for writing documentation due to its simplicity and integration with version control platforms and static site generators.
- 🗂️ Git: Git allows versioning of documentation just like code. Thanks to Git, every change in the documentation is recorded, enabling teams to track edits, revert changes, and collaborate more efficiently.
- 🔄 Gitflow: This methodology provides a structured workflow to manage versions and revisions of documentation, ensuring that changes are approved and tested before reaching production. Gitflow also facilitates collaboration between teams, allowing for safe and organized change management.
- ☁️ Cloud Services: Using services like AWS S3, Netlify, or GitHub Pages, you can deploy documentation at a low cost. These services allow the creation of fast, secure, and easily accessible static sites.
- 🌐 Static Site Generators: Tools like Docusaurus, Jekyll, or Hugo convert Markdown documentation into a navigable website, allowing you to create rich and organized documentation without a server.
- 🚀 Continuous Integration (CI/CD): CI/CD pipelines (e.g., GitHub Actions, GitLab CI, or Jenkins) allow you to automatically deploy documentation when a new version is merged or modifications are approved. This ensures the documentation is always up-to-date.
🧩 Dynamic Page: Jinja
Jinja is a library that allows embedding variables and data from Python dictionaries into HTML, making web pages dynamic. This library is commonly used for generating dynamic HTML and sending personalized emails.
🦖 Docusaurus Overview
Docusaurus is an open-source project developed by Meta in 2007 that simplifies the creation, deployment, and maintenance of documentation websites in a fast and efficient way. It allows the use of Markdown and MDX to write content, while its core built on React enables full customization of the styles to fit the specific needs of the project.
Additionally, Docusaurus supports Mermaid through the @docusaurus/theme-mermaid plugin, enabling the inclusion of charts and diagrams directly within the documentation.
🎨 Diagram as Code
Diagram as Code is an approach that allows you to create diagrams through code, rather than using traditional graphic tools. Instead of manually building diagrams, you write code in a text file to define the structure, components, and connections of your diagrams.
This code is then translated into graphical images, making it easier to integrate and document in software projects. It's especially useful for creating and updating architectural and flow diagrams programmatically.
🎨 Diagram as Code: Example of Creating Cloud Diagrams
As previously mentioned, Diagrams allows you to generate blueprints using the icons of major cloud technologies. The representation of these diagrams is done through nodes, and in our example, we’ll use all cloud-related nodes and AWS services.
, and the full implementation can be found in this repository:
A tutorial on how to create a documentation project using the 'Doc as diagram' methodology
This code is then translated into graphical images, making it easier to integrate and document in software projects, where it is especially useful for creating and updating architectural and flow diagrams programmatically.
What is Diagrams?
Diagrams is a 🐍Python library that implements the Diagram as Code approach, enabling you to create architectural infrastructure diagrams and other types of diagrams through code. With Diagrams, you can easily define cloud infrastructure components (such as AWS, Azure, and GCP), network elements, software services, and more, all with just a few lines of code.
🎉 Benefits of Diagram-as-Code
- 📝…
templateindex.md, tables.md, architecture.md, glossary.md. Supports Mermaid diagrams, embedded images, and database-driven content.docs)docsupdate.py, including images (img/) and dynamic content populated from template.infraestructure)infraestructuremain.tf, variables.tf) to deploy an S3 bucket for documentation hosting.
📄 Mkdocs: Configuring mkdocs.yml
Once we have our project structure set up, we will configure it step by step, starting with the mkdocs.yml file. This file defines the structure and settings for your documentation site. Here's how it should be structured:
mkdocs.yml
site_name: Hospital Documentation
nav:
- Home: index.md
- Synthea Tables: tables.md
- AWS Architectur: architecture.md
- Glossary: glossary.md
markdown_extensions:
- pymdownx.superfences:
custom_fences:
- name: mermaid
class: mermaid
theme:
name: material
In this configuration file, you can primarily see in the nav section the pages that will be accessible from the menu. Then, we specify the Mermaid extension, which will be explained in the next section. Finally, the theme section applies the Material theme, enabling styling and components available within this library.
✏️ Mkdocs: Mermaid Extension
As mentioned earlier, Mermaid is a JavaScript library for creating diagrams and charts from text. Below, we will see some examples. In our case, we will use it to generate an Entity Relationship Diagram (ERD) on the tables page of the documentation.
.
⚙️ Mkdocs: Dynamic Content with Jinja
To enable dynamic content generation for our documentation site, we’ll use Jinja to process templates and replace placeholders with actual data. Below is a step-by-step breakdown:
Set Up a
templatesFolder
Create a folder namedtemplatesto store all Markdown files for the site. These files should include placeholders. For instance, inindex.md, you might have placeholders like{{database.version_date}}and{{database.version}}.Utilize Placeholders
Placeholders are dynamic variables in the Markdown files. These variables will be updated automatically using Python dictionaries to inject relevant data.
Generate Dynamic Content with
update.py
- Prepare your Markdown templates by identifying the sections where dynamic data is required.
- Use a Python script (
update.py), available in my repository, to process the templates. The script performs the following tasks:
Database Connection: Connects to a SQLite database to fetch the latest values.
Template Rendering: Uses the Jinja library to substitute placeholders with data from the database.
File Generation: Outputs updated Markdown files to thedocsfolder, ready for rendering in MkDocs.
- Prepare your Markdown templates by identifying the sections where dynamic data is required.
🦖 Docusaurus Implementation
In the following sections, I will provide detailed steps and insights on how to implement a documentation site using Docusaurus. This includes setup, customization, and deployment options.
🚀 Key Features of Docusaurus
- 📶 Mermaid Support: Similar to MkDocs, Docusaurus supports Mermaid for embedding diagrams.
- ⚛️ React Components: Built on React, Docusaurus enables the integration of dynamic components into your documentation.
- 🔄 Dynamic Content: Leverages Python scripts to fetch and update content dynamically from an SQLite database.
🔧 Docusaurus Setup: From Scratch
To get started with Docusaurus, we follow a quick setup process, which is very similar to the steps we used for MkDocs but with different tools.
Create a New Docusaurus Project:
First, install Node.js and run the following command to create a new Docusaurus site:
npx create-docusaurus@latest my-website classic
Install Mermaid Package:
To enable Mermaid diagrams, install the required package:
npm install @docusaurus/theme-mermaid
Run the Development Server:
Once installed, navigate to your project directory and run the development server:
cd my-website
npx docusaurus start
Visit the Site:
Your site will be live locally at:http://localhost:3000.
🔧 Docusaurus Customization: Configuration
The configuration file docusaurus.config.js is where we customize the title, theme, navigation, and enable features like Mermaid for diagram rendering.
Example snippet for enabling Mermaid:
module.exports = {
title: 'Hospital Documentation',
tagline: 'Documentation for Hospital Data ML Project',
favicon: 'img/favicon.ico',
url: 'https://your-site-url.com',
markdown: {
mermaid: true, // Enable Mermaid diagrams
},
themeConfig: {
navbar: {
title: 'Hospital Docs',
items: [
{ to: 'docs/', label: 'Home', position: 'left' },
{ to: 'docs/tables', label: 'Tables', position: 'left' },
{ to: 'docs/architecture', label: 'Architecture', position: 'left' },
{ to: 'docs/glossary', label: 'Glossary', position: 'left' },
],
},
footer: {
style: 'dark',
links: [
{ label: 'GitHub', href: 'https://github.com/your-repo' },
],
},
},
};
🔧 Docusaurus Customizing the Homepage
To customize the homepage, we modify the src/components/HomepageFeatures/index.js file. Here, you can adjust the FeatureList object to update the features displayed on the homepage.
⚙️ Dynamic Data with Jinja
To incorporate dynamic content, such as database tables, we use a 🐍Python script named update.py, which you can find in the repository.
This script fetches data from a SQLite database and processes the Markdown files stored in the templates folder. It then updates these files with the fetched data and copies them into the docs folder, preparing them for site rendering.
This workflow ensures that the content remains up-to-date and ready for deployment, following a similar approach to what we implemented with MkDocs.
⚙️ Docusaurus: Final Workflow
Create Templates: Develop your Markdown files within thedocs/templatedirectory.
Run Python Script: Use the script to dynamically populate data into the templates.
Preview Locally: Runnpx docusaurus startto preview the site.
Build for Deployment: Once ready, usenpx docusaurus buildto generate the static site.
Deploy: Host the static files on your preferred platform, such as AWS S3.
🚀 Deployment
In this section, we will cover the deployment process for both MkDocs and Docusaurus using AWS S3 for hosting. While the deployment steps are the same for both tools, the installation processes differ, with MkDocs being Python-based and Docusaurus being JavaScript-based.
Infrastructure Setup with Terraform
To deploy a static documentation site to AWS S3, we use Terraform to provision and configure the required resources. The setup defines the S3 bucket, enables static website hosting, and configures public access with a bucket policy to allow read-only access. You can find the main.tf file in the repository.
🚀 Key Components for S3 Deployment
S3 Bucket Creation: The resource to create the S3 bucket where the documentation will be hosted.
Static Website Hosting: Configuration for static web hosting, setting theindex.htmlanderror.htmlas the main and error documents.
Public Access Configuration: Manages public access to the S3 bucket, ensuring it is configured for read-only access.
Bucket Policy: Allows public access to retrieve the documentation content from the S3 bucket.
You can access the complete Terraform file and the corresponding configurations for deploying the site in the repository:
Terraform Configuration File:
GitHub Action Workflow for Automatic Deployment: A CI/CD pipeline to automate the deployment process is also included in the repository.
GitHub Actions Configuration
Make sure to configure your AWS credentials in the GitHub repository secrets under Settings > Secrets > Actions. This will allow GitHub Actions to securely access your AWS account and perform actions like uploading files to S3 when you push changes to themainbranch.
Repositories
Below are the links to all the code to deploy your documentation site. If you find it useful, you can leave a star ⭐️ and follow me to receive notifications of new articles. This will help me grow in the tech community and create more content.
MkDocs Deployment: /
⚙️ Doc as Code Tutorial
🚀 MkDocs & MkDocs-material
MkDocs is an excellent solution for implementing a documentation portal that can be
easily updated with code, helping to keep your software development project documentation up-to-date and versioned.
In this repository, I have created a simple site to document the data model and machine learning project.
The documentation will include
charts,tables, andarchitectureexamples, providing a comprehensive andeasy-to-understandguide on how to implement this framework in combination with two other 🐍Python libraries.
What is Documentation as Code?
Documentation and its updates are an important process in many companies that develop software, where this process is carried out using different tools, many of which are paid solutions.
Therefore, in recent times, the concept of "doc as code" has emerged. This means using the same tools and workflow used in software development tomanage,version, and…
SOCIAL SHARE CARD GENERATOR