Skip to main content

A flexible data composition tool to simplify writing templates.

Project description

django-nifty-layout

PyPI Version Test with tox Code style: black

Version: 0.2.1

A simple but flexible layout tool for composing and transforming data for structured template components. Inspired by crispy-forms Layout, but without the forms.

django-nifty-layout is free software distributed under the MIT License.

Quick Start

  1. Install the django-nifty-layout package from PyPI

    $ pip install django-nifty-layout
    
  2. Go grab a drink, you are all done!

Sample Usage

layout.py

from django.utils.formats import date_format

from nifty_layout.components import (
   DictCompositeNode as Dct,
   FieldNode as Fld,   
   Seq,
)

# ----- Safe Date Formatter ----- #
date_formatter = lambda val, **kwargs: "" if val is None else date_format(val, SHORT_DATE_FORMAT)

layout = Dct(
    dict(
        overview=Seq(
             "title",
             Fld("date", formatter=date_formatter),
             report_type",
             "location",
             labeller="Overview",
        ),
        contacts=Seq(
            Dct(dict(
                    name="contact_name",
                    contact_methods=Seq("contact_email"),
                ), labeller="Primary Contact"
            ),
            Dct(dict(
                    name="reported_by_name",
                    contact_methods=Seq("reported_by_phone_num", "reported_by_email"),
                 ), labeller="Reported By"
            ),
            labeller="Contacts",
        ),
        ...
    )
)

views.py

def report_view(request, pk):
   ...
   obj = Report.objects.get(pk=pk)
   ...
   return render(request, "template.html", dict(report=layout.bind(obj)))

template.html

...
<div class="report>
  <h2>report.overview.label</h2>
  {% for node in report.overview %}
      <div class="row">
         <div class="col label">{{ node.label }}</div>
         <div class="col value">{{ node.value|default:"" }}</div>
      {% endfor %}
      </div>
  {% endfor %}
  <div class="row">
    {% for contact in report.contacts %}
      <div class="col">
        {% include "contact_card.html" %}
      </div>
    {% endfor %}
  </div>
   ...
</div>   

Get Me Some of That

MIT License

Check Out the Demo App

  1. pip install -e git+https://github.com/powderflask/django-nifty-layout.git#egg=django-nifty-layout
  2. inv demo.install ** coming soon **
  3. python demo_app/manage.py runserver

See demo_app/README

Acknowledgments

This project would be impossible to maintain without the help of our generous contributors

Technology Colophon

Without django and the django dev team, the universe would have fewer rainbows and ponies.

This package was originally created with cookiecutter and the cookiecutter-powder-pypackage project template.

For Developers

Install invoke, pip-tools, tox for all the CLI goodness

 pip install invoke pip-tools tox

Initialise the development environment using the invoke task

inv tox.venv

Or create it with tox directly

tox d -e dev .venv

Or build and install the dev requirements with pip

inv deps.compile-dev
pip install -r requirements_dev.txt

Tests

pytest

or

tox r

or run tox environments in parallel using

tox p

Code Style / Linting

$ isort
$ black
$ flake8

Versioning

Build / Deploy Automation

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

django_nifty_layout-0.2.1.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

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

django_nifty_layout-0.2.1-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file django_nifty_layout-0.2.1.tar.gz.

File metadata

  • Download URL: django_nifty_layout-0.2.1.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_nifty_layout-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f03cb65e517462e47bcc10a9c3c1379194d06d6e6de045902785bc9047d8d991
MD5 97cba4d6ad25c4145478ec0a899f8f21
BLAKE2b-256 cb7c4188ca521c8e6c0bd3cb32e6c5991e0b4e3bb557290a61c11144f3922ef2

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_nifty_layout-0.2.1.tar.gz:

Publisher: release.yaml on powderflask/django-nifty-layout

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

File details

Details for the file django_nifty_layout-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_nifty_layout-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ccd11ead815a5d9039c67803a159677062b611abe11b6220e6e367276bce4de4
MD5 b3a789dabc0d7b0417f45b4b3ad0dd31
BLAKE2b-256 35dff3c8578245daabdaa75b4075cb32069d838c3e32789e7be771d9355a74d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_nifty_layout-0.2.1-py3-none-any.whl:

Publisher: release.yaml on powderflask/django-nifty-layout

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