Skip to main content

A simple library for class based view breadcrumbs for any python web framework

Project description

crumbles

A simple library for class based view breadcrumbs for any python web framework.

CI MIT License PyPI version Python 3.10 Python 3.11 Python 3.12 Python 3.13 Ruff Checked with mypy No Dependencies

Usage

First define a specific mixin for your framework. For example Django:

from django.urls import reverse


class DjangoCrumblesViewMixin(CrumblesViewMixin):
    def url_resolve(self, *args, **kwargs):
        return reverse(*args, **kwargs)

Use the Mixin in your View classes and define the breadcrumbs:

class MyListView(View, DjangoCrumblesViewMixin):
    crumbles = (
        CrumbleDefinition(url_name="my-list-view", title="Home"),
    )

Add a default breadcrumb to the mixin like so:

class DjangoCrumblesViewMixin(CrumblesViewMixin):
    def __init__(self):
        self.crumbles = (
            CrumbleDefinition(url_name="index", title="Home"),
        ) + type(self).crumbles

    def url_resolve(self, *args, **kwargs):
        return reverse(*args, **kwargs)

You can reuse "parent" crumbles for detail pages and use a callable for url resolve arguments or for defining the title:

from operator import attrgetter, methodcaller


class MyDetailView(View, DjangoCrumblesViewMixin):
    crumbles = MyListView.crumbles + (
        CrumbleDefinition(
            url_name="my-detail-view",
            url_resolve_kwargs={"pk": attrgetter("pk")},
            title=methodcaller("__str__"),
        ),
    )

The attributes context will be retrieved from instance.object. If this is not available it will default to the View class instance itself. You can set a custom context variable (including dotted syntakt) by using crumbles_context_attribute:

class MyDetailView(View, DjangoCrumblesViewMixin):
    crumbles_context_attribute = "my_object.parent"
    ...

Rendering the breadcrumb should be done in a template. An example for Django (with bootstrap):

<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        {% for item in view.resolve_crumbles %}
            {% if forloop.last %}
                <li class="breadcrumb-item active" aria-current="page">{{ item.title }}</li>
            {% else %}
                <li class="breadcrumb-item">
                    {% if item.url %}
                        <a href="{{ item.url }}">{{ item.title}}</a>
                    {% else %}
                        {{ item.title }}
                    {% endif %}
                </li>
            {% endif %}
        {% endfor %}
    </ol>
</nav>

Development setup

First clone this repository

git clone https://github.com/maerteijn/crumbles.git

Install the python project

pyenv virtualenv crumbles  # or your alternative to create a venv
pyenv activate crumbles
make install

Linting

ruff and mypy are installed and configured

make lint

Formatting

ruff are configured

make format

Test

Pytest with coverage is default enabled

make cov

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

crumbles-0.1.2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

crumbles-0.1.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file crumbles-0.1.2.tar.gz.

File metadata

  • Download URL: crumbles-0.1.2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for crumbles-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c61b7bca857bb102cde511f81169949c32ff74fd37f2b66462076edec22178f8
MD5 c5af6fc2be13c31335b02069d7caba90
BLAKE2b-256 644deaa494d07e1040d58cd5520f7aca33b786e7c9335c26e183a0bcdfd4d13f

See more details on using hashes here.

Provenance

The following attestation bundles were made for crumbles-0.1.2.tar.gz:

Publisher: publish-pypi.yaml on maerteijn/crumbles

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file crumbles-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: crumbles-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for crumbles-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a80669b764f82957696da5874568201fcf4774e4fc0b8a584a52bbc0741045ec
MD5 4eb2df804309edc82202a7df2a631db3
BLAKE2b-256 7ca779089290952697188b72b029a3ed698597acec73c0fe0010f54f1c223fd3

See more details on using hashes here.

Provenance

The following attestation bundles were made for crumbles-0.1.2-py3-none-any.whl:

Publisher: publish-pypi.yaml on maerteijn/crumbles

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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