Pegase745 / sqlalchemy-datatables

SQLAlchemy integration of jQuery DataTables >= 1.10.x (Pyramid and Flask examples)
MIT License
159 stars 67 forks source link
datatables python sqlalchemy

====================== sqlalchemy-datatables

|Build Status| |PyPi Version| |Scrutinizer| |Coverage|

.. |Build Status| image:: https://travis-ci.org/Pegase745/sqlalchemy-datatables.svg?branch=master :target: https://travis-ci.org/Pegase745/sqlalchemy-datatables

.. |PyPi Version| image:: https://badge.fury.io/py/sqlalchemy-datatables.svg :target: https://pypi.python.org/pypi/sqlalchemy-datatables/

.. |Scrutinizer| image:: https://scrutinizer-ci.com/g/Pegase745/sqlalchemy-datatables/badges/quality-score.png :target: https://scrutinizer-ci.com/g/Pegase745/sqlalchemy-datatables/

.. |Coverage| image:: https://codecov.io/gh/Pegase745/sqlalchemy-datatables/branch/master/graph/badge.svg :target: https://codecov.io/gh/Pegase745/sqlalchemy-datatables

sqlalchemy-datatables is a framework agnostic library providing an SQLAlchemy <http://www.sqlalchemy.org/> integration of jQuery DataTables <http://datatables.net/> >= 1.10, and helping you manage server side requests in your application.

Installation

To install via pip:

Python 3

.. code-block:: bash

$ pip3 install sqlalchemy-datatables

To install from source:

.. code-block:: bash

$ git clone git@github.com:Pegase745/sqlalchemy-datatables.git
$ cd sqlalchemy-datatables
$ pip3 install .

To contribute:

.. code-block:: bash

In a virtualenv
$ git clone git@github.com:Pegase745/sqlalchemy-datatables.git
$ cd sqlalchemy-datatables
$ make all

Usage

.. code-block:: python

@view_config(route_name='data', renderer='json')
def data(request):
    """Return server side data."""
    # defining columns
    #  - explicitly cast date to string, so string searching the date
    #    will search a date formatted equal to how it is presented
    #    in the table
    columns = [
        ColumnDT(User.id),
        ColumnDT(User.name),
        ColumnDT(Address.description),
        ColumnDT(func.strftime('%d-%m-%Y', User.birthday)),
        ColumnDT(User.age)
    ]

    # defining the initial query depending on your purpose
    #  - don't include any columns
    #  - if you need a join, also include a 'select_from'
    query = DBSession.query().\
        select_from(User).\
        join(Address).\
        filter(Address.id > 4)

    # instantiating a DataTable for the query and table needed
    rowTable = DataTables(request.GET, query, columns)

    # returns what is needed by DataTable
    return rowTable.output_result()

Examples

You can find working examples in the repository <examples>, including an integration with the yadcf <https://github.com/vedmack/yadcf/> plugin:

Changelog

All notable changes to this project will be documented in this section <https://github.com/Pegase745/sqlalchemy-datatables/releases>_.

This project adheres to Semantic Versioning <http://semver.org/> and Keep A Changelog <http://keepachangelog.com/>.

License

The project is licensed under the MIT license.