Skip to main content

None

Project description

Flask-Breadcrumb

build Coverage Status PyPI version Python Versions

A Flask extension that provides automatic breadcrumb generation for your Flask applications. It intelligently builds hierarchical breadcrumb navigation based on your URL structure.

Features

  • Automatic breadcrumb generation based on URL structure
  • Support for dynamic route parameters
  • Customizable breadcrumb text
  • Hierarchical breadcrumb structure with parent-child relationships
  • JSON output for easy integration with frontend frameworks
  • Support for dynamic text generation using route parameters

Installation

pip install flask-breadcrumb

Or with uv:

uv pip install flask-breadcrumb

Quick Start

from flask import Flask, render_template_string, request
from flask_breadcrumb import Breadcrumb, get_breadcrumbs

app = Flask(__name__)
breadcrumb = Breadcrumb(app)

@app.route('/')
@breadcrumb("Home")
def index():
    return 'Home'

@app.route('/categories')
@breadcrumb("Categories")
def categories():
    # Get breadcrumbs as JSON
    breadcrumb_json = get_breadcrumbs()
    return render_template_string(
        '<pre>{{ breadcrumb_json }}</pre>',
        breadcrumb_json=breadcrumb_json
    )

@app.route('/categories/<category>')
@breadcrumb(lambda: f"Category: {request.view_args['category']}")
def category_page(category):
    breadcrumb_json = get_breadcrumbs()
    return render_template_string(
        '<pre>{{ breadcrumb_json }}</pre>',
        breadcrumb_json=breadcrumb_json
    )

if __name__ == '__main__':
    app.run(debug=True)

Usage

Basic Usage

Decorate your route functions with the @breadcrumb decorator to add them to the breadcrumb hierarchy:

@app.route('/path1')
@breadcrumb("Path 1")
def path1():
    return 'Path 1'

@app.route('/path1/subpath')
@breadcrumb("Subpath")
def subpath():
    return 'Subpath'

Dynamic Breadcrumb Text

You can use a function to generate dynamic breadcrumb text based on route parameters:

@app.route('/user/<username>')
@breadcrumb(lambda: f"User: {request.view_args['username']}")
def user_profile(username):
    return f'Profile for {username}'

Getting Breadcrumbs

To get the breadcrumb tree for the current request:

breadcrumb_json = get_breadcrumbs()

You can also get breadcrumbs for a specific URL:

other_breadcrumbs = get_breadcrumbs('/path2')

Advanced Options

Max Depth

You can limit the depth of the breadcrumb tree:

breadcrumb_json = get_breadcrumbs(max_depth=1)

Include Root

By default, the root path ('/') is not included in the breadcrumb tree. You can include it by setting use_root=True:

breadcrumb_json = get_breadcrumbs(use_root=True)

Breadcrumb Structure

The breadcrumb tree is returned as a JSON string with the following structure:

{
  "text": "Home",
  "url": "/",
  "is_current_path": false,
  "children": [
    {
      "text": "Categories",
      "url": "/categories",
      "is_current_path": false,
      "children": [
        {
          "text": "Category: test",
          "url": "/categories/test",
          "is_current_path": true,
          "children": []
        }
      ]
    }
  ]
}

Template Integration

Here's an example of how to render breadcrumbs in a Jinja2 template:

{% set crumbs = breadcrumb_tree() %}
<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item">
      <a href="{{ crumbs.url }}">{{ crumbs.text }}</a>
    </li>
    {% for child in crumbs.children recursive %} {% if child.is_current_path %}
    <li class="breadcrumb-item active" aria-current="page">{{ child.text }}</li>
    {% else %}
    <li class="breadcrumb-item">
      <a href="{{ child.url }}">{{ child.text }}</a>
    </li>
    {% endif %} {% if child.children %} {{ loop(child.children) }} {% endif %}
    {% endfor %}
  </ol>
</nav>

API Reference

Breadcrumb(app=None)

The main extension class.

  • app: Flask application instance (optional). If not provided, you must call init_app later.

Breadcrumb.init_app(app)

Initialize the extension with a Flask application.

  • app: Flask application instance.

@breadcrumb(text)

Decorator to register a view function as a breadcrumb.

  • text: Text to display for the breadcrumb or a function that returns the text.

get_breadcrumbs(url=None, max_depth=None, use_root=False)

Get the breadcrumb tree for a specific URL.

  • url: URL to get breadcrumbs for. If None, uses the current request path.
  • max_depth: Maximum depth to traverse up the breadcrumb tree.
  • use_root: Whether to include the root path ('/') in the breadcrumb tree.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

flask_breadcrumb-1.0.4.tar.gz (35.1 kB view details)

Uploaded Source

Built Distribution

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

flask_breadcrumb-1.0.4-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file flask_breadcrumb-1.0.4.tar.gz.

File metadata

  • Download URL: flask_breadcrumb-1.0.4.tar.gz
  • Upload date:
  • Size: 35.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for flask_breadcrumb-1.0.4.tar.gz
Algorithm Hash digest
SHA256 9caf151cb9d8f7a2f800ec8f01c5768ec311f6d2ba7b4b75266520dac8fb5b73
MD5 46d6527936164a7e2929284ab4731012
BLAKE2b-256 e4bc8142f239997c5f9441435d384c8b152f5f25ed11d38ed62a02c9ef584d78

See more details on using hashes here.

File details

Details for the file flask_breadcrumb-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_breadcrumb-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 59d53c64e0a0f4a57f2fc9f5485942ebe019b3c772efb0aec470d771c3b6e7d8
MD5 c1a6660800f2f240c95104297583506e
BLAKE2b-256 eadcdfe3d77d41eeb86b218df5dde3c5ab6c46f81af86805484162254c1851e7

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