Skip to main content

Easily browse your Django models

Project description

django-pivot is a wrapper around numpy, pandas and django-pandas letting you easily manipulate pivot tables on your queryset.

Install

pip install django-pivot2

Modify your model(s) to use django_pandas.managers.DataFrameManager or django_pandas.managers.DataFrameQuerySet:

class LongTimeSeries(models.Model):
  date_ix = models.DateTimeField()
  series_name = models.CharField(max_length=100)
  value = models.FloatField()

  objects = DataFrameManager()

Usage

This app basicaly gives APIs to request a pivot table. Everything is not automatic and developer must always declare:

  • The possible values

  • The possible rows and columns

And for a end-user the API will ask

  • Values

  • Rows

  • Columns

  • Aggregation functions

  • Applied function

  • Format

As class-based view

A mixin is available to compose you own pivot. It has the following behavior: - Display a form to collect parameters of pivot table - Display pivot table as HTML if valid input is given - Download data as file with format given in form

Example:

from django_filters.views import FilterView
from django_pivot.views import PivotView
from myapp import models

class LongTimeSeriesPivotView(PivotView, FilterView):
    template_name = "pivot.html
    model = models.LongTimeSeries

    values_choices = ['date_ix', 'value']
    rows_choices = cols_choices = ['serie_name', 'date_ix', 'value']

PivotView is compatible with Django’s ListView, django-filters’ FilterView or any kind of view with the same behavior.

With graphene-django

Coming soon

With graphene-django-extras

Example of schema.py:

from graphene_django import DjangoObjectType
from django_pivot.contrib.graphene_django_extras import PivotField
from myapp import models

class LongTimeSeriesType(DjangoObjectType):
    class Meta:
        model = models.LongTimeSeries

class Query:
    pivot_long_time_series = PivotField(
        LongTimeSeriesType,
        filterset_class=filtersets.LongTimeSeriesFilter,
        values_choices=['date_ix', 'value'],
        rows_choices=['serie_name', 'date_ix', 'value'],
        cols_choices=['serie_name', 'date_ix', 'value'],
    )

Example of request:

query {
  pivot_long_time_series (values: ["value"], rows: ["serie_name"], cols: ["value"], aggfuncs: ["mean"])
}

As GraphQL is supposed to return JSON only and pandas JSON format is ..hum… weird, we convert pivot_table into CSV and after into a list of list, so fully compatible.

With Django REST Framework

Coming soon

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-pivot2-0.2.6.tar.gz (16.3 kB view details)

Uploaded Source

File details

Details for the file django-pivot2-0.2.6.tar.gz.

File metadata

  • Download URL: django-pivot2-0.2.6.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.19.4 CPython/3.5.2

File hashes

Hashes for django-pivot2-0.2.6.tar.gz
Algorithm Hash digest
SHA256 1e1df7ea3180c0189606d8b7bff81b4a9afbd096fbc172c5594edbd9a66e6348
MD5 352d194c21bf88d7c0973274ec4a8520
BLAKE2b-256 761807a3ef7fc423e5667f9426eb39090aa1c3c82e4de27c63771a5001299ce6

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