Dash Plotly component providing Tabulator tables
npm install dash_tabulator
- Dash Tabulator
- Features
- Installation
- Usage
- Themes
- Advanced Usage
- Multiple Row Selection
- Javascript
- Javascript Cell Formatting Example
- Javascript column resizing capture
- Links
Dash tabulator is a Dash / Plotly component providing Tabulator capabilities.
This is not a fully comprehensive implementation of Tabulator just the basics necessary to get the application working.
Under the covers this uses react-tabulator
This is built on the shoulders of the Dash Plotly team, the Tabulator team, and the React Tabulator team.
This readme is probably longer than the code, due to the work of those individuals!
Installation can be done with pip in your dash project
``bash`
pip install dash_tabulator
pip install dash_extensions
`python
import dash_tabulator
import dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc
from textwrap import dedent as d
import json
styles = {
'pre': {
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
}
}
options = { "groupBy": "col", "selectable":1}
downloadButtonType = {"css": "btn btn-primary", "text":"Export", "type":"xlsx"}
app.layout = html.Div([
dash_tabulator.DashTabulator(
id='tabulator',
#columns=columns,
#data=data,
options=options,
downloadButtonType=downloadButtonType,
clearFilterButtonType=clearFilterButtonType
),
html.Div(id='output'),
dcc.Interval(
id='interval-component-iu',
interval=1*10, # in milliseconds
n_intervals=0,
max_intervals=0
)
])
if __name__ == '__main__':
app.run_server(debug=True)
`
Be aware registering a callback for dataChanged will send the entire table back each time a change occurs
Make sure you are conscious of the amount of data you are round tripping.
dataFiltering will return the filters before a match has occurred, usually a partial match
`python`
[{'field': 'col', 'type': 'like', 'value': 'yello'}]
dataFiltered will return the header filter and the row data e.g.
`python`
{
'filters': [{'field': 'col', 'type': 'like', 'value': 'yellow'}],
'rows': [
None,
None,
{'id': 5, 'name': 'Margret Marmajuke', 'age': '16', 'col': 'yellow', 'dob': '31/01/1999'},
{'id': 6, 'name': 'Fred Savage', 'age': '16', 'col': 'yellow', 'rating': '1', 'dob': '31/01/1999'}
]
}
python
app.layout = html.Div([
dash_tabulator.DashTabulator(
id='tabulator',
theme='tabulator_simple', #optional
options=options,
downloadButtonType=downloadButtonType,
clearFilterButtonType=clearFilterButtonType,
),
....
])
`
The following are the built in react-tabulator themes
- tabulator or default => react-tabulator/lib/css/tabulator.min.css
- tabulator_modern => react-tabulator/lib/css/tabulator_modern.min.css
- tabulator_midnight => react-tabulator/lib/css/tabulator_midnight.min.css
- tabulator_simple => react-tabulator/lib/css/tabulator_simple.min.css
- tabulator_site => react-tabulator/lib/css/tabulator_site.min.css
- bootstrap/tabulator_bootstrap => react-tabulator/lib/css/bootstrap/tabulator_bootstrap.min.css
- bootstrap/tabulator_bootstrap4 => react-tabulator/lib/css/bootstrap/tabulator_bootstrap4.min.css
- bulma/tabulator_bulma => react-tabulator/lib/css/bulma/tabulator_bulma.min.css
- materialize/tabulator_materialize => react-tabulator/lib/css/materialize/tabulator_materialize.min.css
- semantic-ui/tabulator_semantic-ui => react-tabulator/lib/css/semantic-ui/tabulator_semantic-ui.min.css
Advanced Usage
Multiple Row Selection
Tabulator supports multiple row selection
To Enable the table option selectable must be set to the STRING true
`python
options = { "selectable":"true", ....}
` Once selectable is set
`python
@app.callback(Output('output', 'children'),
[Input('tabulator', 'rowClicked'),
Input('tabulator', 'multiRowsClicked')]
def clickedRows(rowClicked, multiRowsClicked):
......
......
`
multiRowsClicked will now contain an array of all the rows selected
e.g.
`python
[
{'id': 6, 'name': 'Fred Savage', 'age': '16', 'col': 'yellow', 'rating': '1', 'dob': '31/01/1999', 'print': 'foo'},
{'id': 5, 'name': 'Margret Marmajuke', 'age': '16', 'col': 'yellow', 'dob': '31/01/1999', 'print': 'foo'},
{'id': 4, 'name': 'Brendon Philips', 'age': '125', 'col': 'orange', 'dob': '01/08/1980', 'print': 'foo'},
{'id': 3, 'name': 'Christine Lobowski', 'age': '42', 'col': 'green', 'dob': '22/05/1982', 'print': 'foo'}
]
`Select all / deselect all can be accomplished with a header formatter called rowSelection.
`python
columns = [
{"formatter":"rowSelection", "titleFormatter":"rowSelection", "hozAlign":"center", "headerSort":"false"},
{ "title": "Name", "field": "name", "width": 150, "headerFilter":True, "editor":"input"},
`
For more options check out http://tabulator.info/docs/4.8/select
Javascript
Tabulator offers a significate amount of callbacks that allow for interactivity with tables to be captured
and modified http://tabulator.info/docs/4.8/callbacks
To enable this functionality we can use Dash-Extensions Create an assets folder, add a javascript file with your custom functions
An example is provided in assets/custom_tabulator.js
Please follow the examples below
Javascript Cell Formatting Example
Contributed in https://github.com/preftech/dash-tabulator/pull/11
Tabulator offers Javascript formatting of cells http://tabulator.info/docs/3.4?#formatting
This a browser side javascript method attached to a header colum.* Create an assets directory
* See https://dash.plotly.com/external-resources for customization options
* Add a javascript file with a window. method (below we call it myNamespace)
* An example is provided in the assets/custom_tabulator.js file
* Note the Namespace and the function printIcon
* Register that method in your python app
* Using dash_extensions.javascript.Namespace
* Add the registered function to your colums formatter
Python code:
`python
from dash_extensions.javascript import Namespace
...
ns = Namespace("myNamespace", "tabulator")
...
columns = [{"formatter": ns("printIcon")}, ...]
`Javascript code:
`javascript
window.myNamespace = Object.assign({}, window.myNamespace, {
tabulator: {
printIcon: function (cell, formatterParams, onRendered) {
return "";
}
}
});
`Javascript column resizing capture
Capturing a column resize, when a user drags a column width
From tabulators callbacks page http://tabulator.info/docs/4.8/callbacks#column
We see there's a columnsResized callback
in our python code in the options dict we can specify a javascript methodassets/custom_tabulator.js
`javascript
window.myNamespace = Object.assign({}, window.myNamespace, {
tabulator: {
columnResized : function (column, table) {
// column is the tabulator column component
// table is the tabulator instance, can be used for table.setProps() to send data back to dash
console.log("Column is resized");
console.log(column)
// send data back to dash, still under work, only updates when state changes
// be aware of table rendering and resetting back to original display
table.props.setProps({"columnResized": column._column.field})
}
}
});
`Within your python code, you can register this method as a tabulator option
`python
from dash_extensions.javascript import Namespace
...
ns = Namespace("CustomNamespace", "tabulator")
...
options = { "groupBy": "col", "selectable":"true", "columnResized" : ns("columnResized")}
``A full list of callbacks available exists http://tabulator.info/docs/4.8/callbacks
* Home Page https://github.com/preftech/dash-tabulator
* Tabulator from @olifolkerd http://tabulator.info/
* React-Tabulator from @ngduc https://github.com/ngduc/react-tabulator
* Dash / Plotly from @plotly https://plotly.com/dash/