Skip to main content

Flask-File-Routes

File-based routing with a new mix-code file format that combines python code and a jinja template.
Inspired by Astro pages.

This uses Jinjapy for the page format.

Installation

pip install flask-file-routes

Usage

Activate the extension:

from flask import Flask
from flask_file_routes import FileRoutes

app = Flask(__name__)
FileRoutes(app)

Create a pages/index.jpy file:

---
page.message = "hello world"
def post():
    page.message = f"hello {request.form['name']}"
---
{{ message }}
<hr>
<form method="post">
    <input type="text" name="name" placeholder="your name">
    <button type="submit">say hello</button>
</form>

Go to http://localhost:5000.

Dynamic pages

Dynamic pages can use the following formats:

  • jpy: jinjapy hybrid format that let you execute code before rendering the template
  • py: python code only

A jinjapy file contains 2 sections:

  • Some Python code enclosed by lines containg 3 dashes "---"
  • A body containing some Jinja template code

Both are optional:

  • If the python code is missing, the file only contains a Jinja template
  • If the python code is left unclosed (the file starts with "---" on a single line followed by some python code), the file has no template

The python code has a few global variables injected when executed: page, request, abort, redirect, url_for, current_app, render_template.

Content pages

No python code will be executed on content pages.

Content pages can use the following formats:

  • html: a standard jinja template
  • md: a standard jinja template that will be rendered using markdown

All formats can have a YAML frontmatter defining variables that will be injected in the page object.

How routing works

The URL rule is constructed using the following rules:

  • index.ext file are roots
  • folder hierarchy are transformed to url paths:
    • posts/release-annoucement.ext converts to /posts/release-annoucement
    • posts/index.ext converts to /posts
    • folder/subfolder/page.ext converts to /folder/subfolder/page
  • placeholders are allowed in filenames as they would in url rules
    • posts/<slug>.ext converts to /posts/<slug>
  • surround a folder name with parentheses so that it does not appear in the url
    • folder/(subfolder)/page.ext converts to /folder/page

Allowed HTTP methods are determined by, in order:

  1. A comment at the start of the python code listing allowed http methods in the following format: # methods=GET,POST
  2. functions with HTTP method names in lower case

The page object

The page object is accessible under flask_file_routes.page or g.page in all endpoints of your app.

Any properties set onto the object will be available as a variable in the template.

You can send a response immediatly and stop any further processing using page.respond(response).

View args are available as read-only properties of the page object.

from flask_file_routes import page

@app.route("/")
def index():
    page.message = "hello world"
    return render_template_string("{{message}}")

Helpers can be registered on the page object:

file_routes = FileRoutes(app)

@file_routes.page_helper
def method(page):
    return request.method

pages/index.jpy

---
#methods=GET,POST
if page.method == "POST": # call the helper
    page.message = "hello from post"
else:
    page.message = "hello from get"
---
{{message}}

Special page properties

Setting some properties on the page obejct will affect how the template is rendered:

  • page.is_markdown: after the jinja template is rendered, it will be processed by the markdown converter
  • page.layout: wrap the page output in a layout. This should be a template path that will be extended. You can specify the name of the block to override after a colon (eg: layout.html:my_block)

Download files

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

Source Distribution

flask_file_routes-0.3.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

flask_file_routes-0.3.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file flask_file_routes-0.3.2.tar.gz.

File metadata

  • Download URL: flask_file_routes-0.3.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for flask_file_routes-0.3.2.tar.gz
Algorithm Hash digest
SHA256 288250bbbefcf24602da53d9723d472a16217b22514dc1f4a1c7b0c21446dd97
MD5 c9a803796777abb0dd2b7e696ef499fb
BLAKE2b-256 0a0896108be104b37b2424a3a37434ffbab0ee2c617bdae85884f483e8452d31

See more details on using hashes here.

File details

Details for the file flask_file_routes-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_file_routes-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f91ea5cab20e4675162abae6fbac056f7f149d4f25040ecb95096ef79d0b8437
MD5 3761f48aef294e022a77c5833d6a330a
BLAKE2b-256 b224ec3b4e4e34a9d5c4361f0f39dff5da7e7bf174293d75290608f75d9710f9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.1

2 files

Supported by

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