Skip to main content

Python library that helps create searchable, custom filterable datatables

Project description

Jinja-Datatables

A python library to simplify rendering datatables in jinja templates

Note: This tool is in development and doesnt 100% work yet, all of it is untested

TODO

  • Write test for html
  • Add detail row functionality
  • Make filters an option

Install

pip install jinja-datatables

Usage

See this repo's tests folder for an example Currently only flask-ajax completed

1.) Create a table view object

There are currently three options

From Ajax

If you want to get all your data asynchronously from a endpoint you can create a datatable oject like this

from jinja_datatables.datatable_classes import DatatableColumn, AjaxDatatable
columns = [
    DatatableColumn("data_name", "Column Name", "filter_text"
]
endpoint = "/get_data"
table_view = AjaxDatatable(
    columns,
    None,
    None,
    endpoint,
)

From a JS array

If you have all your data defined in a JS array you can create a datatable object like this:

from jinja_datatables.datatable_classes import DatatableColumn, JSArrayDatatable
columns = [
    DatatableColumn("data_name", "Column Name", "text"),
]
js_array = "dataArray"
table_view = JSArrayDatatable(
    columns,
    None,
    None,
    js_array,
)

For your endpoint, make sure it returns just an array of your data, functionality to customize this behavior might be added in future

From HTML

This is completely untested

If you already have your table in html, then you can easily instantiate a datatable on it like this:

from jinja_datatables.datatable_classes import DatatableColumn, HTMLDatatable
columns = [
    DatatableColumn("data_name", "Column Name", "filter_text"),
]
table_view = HTMLDatatable(
    columns,
    None,
    None,
)

2.) Instantiate Jinja environment

from jinja2 import Environment
from jinja_datatables.jinja_extensions.datatableext import DatatableExt
env = Environment(extensions=[DatatableExt])

For flask this will be handled by attaching an extension like this:

app = Flask(__name__)  # , instance_relative_config=True)
app.jinja_options["extensions"].append(DatatableExt)

3.) Render Template

In python

template = env.get_template("datatable_template.html")
template.render(table_view=table_view)

In your template file

{% datatable table_view %}

Contributing

I have no idea how contributing works. Make a pull request? Send me a message?

I would greatly appreciate help on how to test this

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

jinja_datatables-0.0.8.tar.gz (6.8 kB view hashes)

Uploaded Source

Built Distribution

jinja_datatables-0.0.8-py3-none-any.whl (9.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page