datatables


Namedatatables JSON
Version 0.4.9 PyPI version JSON
download
home_pagehttps://github.com/orf/datatables/
SummaryIntegrates SQLAlchemy with DataTables (framework agnostic)
upload_time2015-01-06 13:49:53
maintainerNone
docs_urlNone
authorTom
requires_pythonNone
licenseMIT
keywords sqlalchemy datatables jquery pyramid flask
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            ===============================================
datatables |PyPi Version| |TravisCI| |Coverage|
===============================================

.. |PyPi Version| image:: http://img.shields.io/pypi/v/datatables.svg?style=flat
    :target: https://pypi.python.org/pypi/datatables

.. |TravisCI| image:: https://api.travis-ci.org/orf/datatables.svg
    :target: https://travis-ci.org/orf/datatables

.. |Coverage| image:: https://coveralls.io/repos/orf/datatables/badge.png?branch=master
  :target: https://coveralls.io/r/orf/datatables?branch=master




Installation
------------

The package is available on `PyPI <https://pypi.python.org/pypi/datatables>`_ and is tested on Python 2.7 to 3.4

.. code-block:: bash

    pip install datatables

Usage
-----

Using Datatables is simple. Construct a DataTable instance by passing it your request parameters (or another dict-like
object), your model class, a base query and a set of columns. The columns list can contain simple strings which are
column names, or tuples containing (datatable_name, model_name), (datatable_name, model_name, filter_function) or
(datatable_name, filter_function).

Additional data such as hyperlinks can be added via DataTable.add_data, which accepts a callable that is called for
each instance. Check out the usage example below for more info.


Example
-------

**models.py**

.. code-block:: python

    class User(Base):
        __tablename__ = 'users'

        id          = Column(Integer, primary_key=True)
        full_name   = Column(Text)
        created_at  = Column(DateTime, default=datetime.datetime.utcnow)

        # Use lazy=joined to prevent O(N) queries
        address     = relationship("Address", uselist=False, backref="user", lazy="joined")

    class Address(Base):
        __tablename__ = 'addresses'

        id          = Column(Integer, primary_key=True)
        description = Column(Text, unique=True)
        user_id     = Column(Integer, ForeignKey('users.id'))

**views.py**

.. code-block:: python

    @view_config(route_name="data", request_method="GET", renderer="json")
    def users_data(request):
        # User.query = session.query(User)
        table = DataTable(request.GET, User, User.query, [
            "id",
            ("name", "full_name", lambda i: "User: {}".format(i.full_name)),
            ("address", "address.description"),
        ])
        table.add_data(link=lambda o: request.route_url("view_user", id=o.id))
        table.searchable(lambda queryset, user_input: perform_some_search(queryset, user_input))

        return table.json()

**template.jinja2**

.. code-block:: html

    <table class="table" id="clients_list">
        <thead>
            <tr>
                <th>Id</th>
                <th>User name</th>
                <th>Address</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

    <script>
        $("#clients_list").dataTable({
            serverSide: true,
            processing: true,
            ajax: "{{ request.route_url("data") }}",
            columns: [
                {
                    data: "id",
                    "render": function(data, type, row){
                        return $("<div>").append($("<a/>").attr("href", row.DT_RowData.link).text(data)).html();
                    }
                },
                { data: "name" },
                { data: "address" }
            ]
    </script>
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/orf/datatables/",
    "name": "datatables",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "sqlalchemy datatables jquery pyramid flask",
    "author": "Tom",
    "author_email": "tom@tomforb.es",
    "download_url": "https://files.pythonhosted.org/packages/d0/a2/53397d6f6f3322007741b1677e203f5c31db5236098cf25123f4ebecd1ea/datatables-0.4.9.zip",
    "platform": "UNKNOWN",
    "description": "===============================================\ndatatables |PyPi Version| |TravisCI| |Coverage|\n===============================================\n\n.. |PyPi Version| image:: http://img.shields.io/pypi/v/datatables.svg?style=flat\n    :target: https://pypi.python.org/pypi/datatables\n\n.. |TravisCI| image:: https://api.travis-ci.org/orf/datatables.svg\n    :target: https://travis-ci.org/orf/datatables\n\n.. |Coverage| image:: https://coveralls.io/repos/orf/datatables/badge.png?branch=master\n  :target: https://coveralls.io/r/orf/datatables?branch=master\n\n\n\n\nInstallation\n------------\n\nThe package is available on `PyPI <https://pypi.python.org/pypi/datatables>`_ and is tested on Python 2.7 to 3.4\n\n.. code-block:: bash\n\n    pip install datatables\n\nUsage\n-----\n\nUsing Datatables is simple. Construct a DataTable instance by passing it your request parameters (or another dict-like\nobject), your model class, a base query and a set of columns. The columns list can contain simple strings which are\ncolumn names, or tuples containing (datatable_name, model_name), (datatable_name, model_name, filter_function) or\n(datatable_name, filter_function).\n\nAdditional data such as hyperlinks can be added via DataTable.add_data, which accepts a callable that is called for\neach instance. Check out the usage example below for more info.\n\n\nExample\n-------\n\n**models.py**\n\n.. code-block:: python\n\n    class User(Base):\n        __tablename__ = 'users'\n\n        id          = Column(Integer, primary_key=True)\n        full_name   = Column(Text)\n        created_at  = Column(DateTime, default=datetime.datetime.utcnow)\n\n        # Use lazy=joined to prevent O(N) queries\n        address     = relationship(\"Address\", uselist=False, backref=\"user\", lazy=\"joined\")\n\n    class Address(Base):\n        __tablename__ = 'addresses'\n\n        id          = Column(Integer, primary_key=True)\n        description = Column(Text, unique=True)\n        user_id     = Column(Integer, ForeignKey('users.id'))\n\n**views.py**\n\n.. code-block:: python\n\n    @view_config(route_name=\"data\", request_method=\"GET\", renderer=\"json\")\n    def users_data(request):\n        # User.query = session.query(User)\n        table = DataTable(request.GET, User, User.query, [\n            \"id\",\n            (\"name\", \"full_name\", lambda i: \"User: {}\".format(i.full_name)),\n            (\"address\", \"address.description\"),\n        ])\n        table.add_data(link=lambda o: request.route_url(\"view_user\", id=o.id))\n        table.searchable(lambda queryset, user_input: perform_some_search(queryset, user_input))\n\n        return table.json()\n\n**template.jinja2**\n\n.. code-block:: html\n\n    <table class=\"table\" id=\"clients_list\">\n        <thead>\n            <tr>\n                <th>Id</th>\n                <th>User name</th>\n                <th>Address</th>\n            </tr>\n        </thead>\n        <tbody>\n        </tbody>\n    </table>\n\n    <script>\n        $(\"#clients_list\").dataTable({\n            serverSide: true,\n            processing: true,\n            ajax: \"{{ request.route_url(\"data\") }}\",\n            columns: [\n                {\n                    data: \"id\",\n                    \"render\": function(data, type, row){\n                        return $(\"<div>\").append($(\"<a/>\").attr(\"href\", row.DT_RowData.link).text(data)).html();\n                    }\n                },\n                { data: \"name\" },\n                { data: \"address\" }\n            ]\n    </script>",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Integrates SQLAlchemy with DataTables (framework agnostic)",
    "version": "0.4.9",
    "split_keywords": [
        "sqlalchemy",
        "datatables",
        "jquery",
        "pyramid",
        "flask"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "a682cc2e0b2d2f58cc7036c76535e5e4",
                "sha256": "c9cbe8261e2abf134987de22a3313558c7f4b1b842596d0987c06326b1ea7a2b"
            },
            "downloads": -1,
            "filename": "datatables-0.4.9.zip",
            "has_sig": false,
            "md5_digest": "a682cc2e0b2d2f58cc7036c76535e5e4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9081,
            "upload_time": "2015-01-06T13:49:53",
            "upload_time_iso_8601": "2015-01-06T13:49:53.488354Z",
            "url": "https://files.pythonhosted.org/packages/d0/a2/53397d6f6f3322007741b1677e203f5c31db5236098cf25123f4ebecd1ea/datatables-0.4.9.zip",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2015-01-06 13:49:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "orf",
    "github_project": "datatables",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "datatables"
}
        
Tom
Elapsed time: 0.15048s