Flask-FlatPages
Flask-FlatPages provides an easy way to integrate flat static pages, written in formats like Markdown or reStructuredText, into a Flask web application. It is currently at version 0.9.0, primarily focusing on maintenance releases and preparing for future feature additions in an eventual 1.0 release.
Warnings
- deprecated Directly accessing the `FlatPages.app` attribute is deprecated. In versions 0.9 and up, it now wraps `flask.current_app`, and attempting to access it outside of an active Flask application context will raise a `RuntimeError`.
- breaking Support for older Python versions has been progressively dropped. Python 2.7 support was removed in v0.8.2. Python 3.7 and earlier are no longer supported as of v0.9.0, requiring Python 3.8+.
- gotcha Metadata parsing was improved in v0.8.0 to be more consistent with other 'FlatPage' style libraries and less strict for pages without explicit metadata. While generally an enhancement, review your existing flat page metadata to ensure it's parsed as expected, especially if you relied on previous implicit behaviors.
- breaking Multiple releases have included updates to underlying dependencies or dropped support for older Python versions to address security vulnerabilities. Running outdated versions can expose your application to known security risks.
Install
-
pip install Flask-FlatPages
Imports
- FlatPages
from flask_flatpages import FlatPages
- Page
from flask_flatpages import Page
Quickstart
import os
from flask import Flask, render_template
from flask_flatpages import FlatPages
# Configuration
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = '.md'
FLATPAGES_ROOT = 'pages'
FLATPAGES_ENCODING = 'utf-8'
app = Flask(__name__)
app.config.from_object(__name__)
flatpages = FlatPages(app)
# Routes
@app.route('/')
def index():
# All pages are available via flatpages iterable
return render_template('index.html', pages=flatpages)
@app.route('/<path:path>/')
def page(path):
# Get a specific page, or 404
page = flatpages.get_or_404(path)
return render_template('page.html', page=page)
if __name__ == '__main__':
# Create dummy content and templates for runnable quickstart
if not os.path.exists(FLATPAGES_ROOT):
os.makedirs(FLATPAGES_ROOT)
with open(os.path.join(FLATPAGES_ROOT, 'about.md'), 'w') as f:
f.write('---\ntitle: About Us\ndate: 2024-05-15\n---\n\n# Welcome to our About Page\n\nThis is an example flat page managed by Flask-FlatPages.')
if not os.path.exists('templates'):
os.makedirs('templates')
with open('templates/index.html', 'w') as f:
f.write('<!doctype html>\n<html>\n<head><title>FlatPages Index</title></head>\n<body>\n <h1>FlatPages Example</h1>\n <ul>\n {% for page in pages %}\n <li><a href="{{ url_for("page", path=page.path) }}">{{ page.meta.get("title", page.path) }}</a></li>\n {% endfor %}\n </ul>\n</body>\n</html>')
with open('templates/page.html', 'w') as f:
f.write('<!doctype html>\n<html>\n<head><title>{{ page.meta.get("title", "Page") }}</title></head>\n<body>\n <h1>{{ page.meta.get("title", "") }}</h1>\n {{ page.html|safe }}\n</body>\n</html>')
# Run the Flask app
app.run(port=5000, debug=DEBUG)