|
# --- |
|
# jupyter: |
|
# jupytext: |
|
# text_representation: |
|
# extension: .py |
|
# format_name: light |
|
# format_version: '1.4' |
|
# jupytext_version: 1.1.1 |
|
# kernelspec: |
|
# display_name: jupyter-datatables |
|
# language: python |
|
# name: jupyter-datatables |
|
# --- |
|
|
|
# + {"toc": true, "cell_type": "markdown"} |
|
# <h1>Table of Contents<span class="tocSkip"></span></h1> |
|
# <div class="toc"><ul class="toc-item"></ul></div> |
|
|
|
# + {"require": ["notebook/js/codecell"]} |
|
# %load_ext jupyter_require |
|
|
|
# + |
|
import numpy as np |
|
import pandas as pd |
|
|
|
# example from http://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html |
|
df = pd.DataFrame({ 'A' : 1., |
|
'B' : pd.Timestamp('20130102'), |
|
'C' : pd.Series(1,index=list(range(4)),dtype='float32'), |
|
'D' : np.array([3] * 4,dtype='int32'), |
|
'E' : pd.Categorical(["test","train","test","train"]), |
|
'F' : 'foo' }) |
|
df |
|
# - |
|
|
|
html = df.to_html(index=False) |
|
|
|
# --- |
|
|
|
# + {"code_folding": [3, 26], "require": [""]} |
|
from collections import OrderedDict |
|
|
|
from jupyter_require import link_css |
|
from jupyter_require import require |
|
|
|
# configure path to the datatables library using requireJS |
|
libs = OrderedDict( |
|
{ |
|
"datatables.net": "https://cdn.datatables.net/1.10.18/js/jquery.dataTables" # FIXME: minified version on prod |
|
} |
|
) |
|
shim = OrderedDict({"datatables.net": {"exports": "$.fn.dataTable"}}) |
|
|
|
bundles = OrderedDict() |
|
|
|
lib = "datatables.net-buttons" |
|
|
|
libs[lib] = "https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min" |
|
shim[lib] = {"deps": ["datatables.net"]} |
|
|
|
# required to export Excel file, must be loaded first |
|
libs["jszip"] = "https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min" |
|
|
|
bundles[lib] = { |
|
"buttons.colvis": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.colVis.min", |
|
"buttons.flash": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.flash.min", |
|
"buttons.html5": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min", |
|
"buttons.print": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min", |
|
} |
|
for bundle, path in bundles[lib].items(): |
|
libs[bundle] = path |
|
shim[bundle] = {"deps": ["jszip", lib]} |
|
|
|
# requirements for correct Buttons functionality |
|
libs["pdfmake"] = "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min" |
|
shim["pdfmake"] = {"deps": ["datatables.net"]} |
|
|
|
libs["vfsfonts"] = "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts" |
|
shim["vfsfonts"] = {"deps": ["datatables.net"]} |
|
|
|
require.config(paths=libs, shim=shim) |
|
|
|
# link stylesheets |
|
link_css( |
|
"https://cdn.datatables.net/v/dt/" |
|
"dt-1.10.18/" # DataTables |
|
"b-1.5.6/" # Buttons |
|
"b-colvis-1.5.6/" # Buttons - Column Visibility |
|
"b-flash-1.5.6/" # Buttons - Flash |
|
"b-html5-1.5.6/" # Buttons - HTML5 |
|
"b-print-1.5.6/" # Buttons - Print View |
|
"datatables.min.css", |
|
{"id": "datatables.min.css"}, |
|
) |
|
|
|
# + {"require": ["datatables.net", "datatables.net-buttons"]} |
|
# %%requirejs datatables.net datatables.net-buttons |
|
|
|
table = $(element).html($$html).find("table.dataframe") |
|
|
|
// Turn existing table into datatable |
|
$(table).ready( () => { |
|
// Turn existing table into datatable |
|
dt = table.DataTable({ |
|
dom: 'Bfrtip', |
|
buttons: ['csv'] |
|
}); |
|
}) |