Skip to content

Commit 29ec54c

Browse files
authored
New JavascriptCode function to execute Javascript code like columns.render (#157)
* New class JavascriptCode to encapsulate JS code * Add an example with columns.render * define dt_args after initializing datatables * rename dt init module * version date
1 parent fe27982 commit 29ec54c

File tree

8 files changed

+70
-25
lines changed

8 files changed

+70
-25
lines changed

docs/advanced_parameters.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,31 @@ with pd.option_context("display.float_format", "${:,.2f}".format):
244244
show(pd.Series([i * math.pi for i in range(1, 6)]))
245245
```
246246

247+
## Javascript formatting
248+
249+
Numbers are formatted using Pandas, then are converted back to float to ensure they come in the right order when sorted.
250+
Therefore, to achieve a particular formatting you might have to resort to the
251+
[`columns.render` option](https://datatables.net/examples/advanced_init/column_render.html)
252+
of datatables.
253+
254+
For instance, this [example](https://datatables.net/forums/discussion/61407/how-to-apply-a-numeric-format-to-a-column)
255+
can be ported like this:
256+
257+
```{code-cell}
258+
from itables import JavascriptCode
259+
260+
261+
show(
262+
pd.Series([i * math.pi * 1e4 for i in range(1, 6)]),
263+
columnDefs=[
264+
{
265+
"targets": "_all",
266+
"render": JavascriptCode("$.fn.dataTable.render.number(',', '.', 3, '$')"),
267+
}
268+
],
269+
)
270+
```
271+
247272
## Row order
248273

249274
Since `itables>=1.3.0`, the interactive datatable shows the rows in the same order as the original dataframe:

docs/changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
ITables ChangeLog
22
=================
33

4+
1.4.6 (2023-01-31)
5+
------------------
6+
7+
**Added**
8+
- We have added a new `JavascriptCode` class to encapsulate JS Code.
9+
This will let the user set JS values for some options like `columnDefs.render` ([#154](https://github.com/mwouts/itables/issues/154)).
10+
11+
412
1.4.5 (2023-01-23)
513
------------------
614

itables/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
from .javascript import JavascriptFunction, init_notebook_mode, show, to_html_datatable
1+
from .javascript import (
2+
JavascriptCode,
3+
JavascriptFunction,
4+
init_notebook_mode,
5+
show,
6+
to_html_datatable,
7+
)
28
from .version import __version__
39

410
__all__ = [
511
"__version__",
612
"to_html_datatable",
713
"show",
814
"init_notebook_mode",
15+
"JavascriptCode",
916
"JavascriptFunction",
1017
]

itables/html/datatables_template.html

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
{
88
// Define the table data
99
const data = [];
10-
11-
// Define the dt_args
12-
let dt_args = {};
13-
dt_args["data"] = data;
14-
1510
// Display the table
1611
$(document).ready(function () {
17-
// [pre-dt-code]
18-
window.__itables_render('#table_id', dt_args);
19-
});
12+
window.initializeDataTable().then(() => {
13+
14+
// Define the dt_args
15+
let dt_args = {};
16+
dt_args["data"] = data;
17+
18+
// [pre-dt-code]
19+
$('#table_id').DataTable(dt_args);
20+
});
21+
})
2022
}
2123
</script>
2224
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script type="module">
2+
window.initializeDataTable = async () => {
3+
if ($.prototype.DataTable) {
4+
return;
5+
}
6+
const dt = (await import("dt_src")).default;
7+
dt(window.$);
8+
}
9+
</script>

itables/html/itables_render.html

Lines changed: 0 additions & 14 deletions
This file was deleted.

itables/javascript.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def init_notebook_mode(
8787
display(
8888
HTML(
8989
replace_value(
90-
read_package_file("html/itables_render.html"),
90+
read_package_file("html/initialize_offline_datatable.html"),
9191
"dt_src",
9292
"data:text/javascript;base64,{}".format(dt64),
9393
)
@@ -163,6 +163,8 @@ def json_dumps(obj, eval_functions):
163163
if isinstance(obj, JavascriptFunction):
164164
assert obj.lstrip().startswith("function")
165165
return obj
166+
if isinstance(obj, JavascriptCode):
167+
return obj
166168
if isinstance(obj, str) and obj.lstrip().startswith("function"):
167169
if eval_functions is True:
168170
return obj
@@ -205,6 +207,12 @@ def __init__(self, value):
205207
), "A Javascript function is expected to start with 'function'"
206208

207209

210+
class JavascriptCode(str):
211+
"""A class that explicitly states that a string is a Javascript code"""
212+
213+
pass
214+
215+
208216
def _datatables_repr_(df=None, tableId=None, **kwargs):
209217
return to_html_datatable(df, tableId, connected=_CONNECTED, **kwargs)
210218

itables/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""ITables' version number"""
22

3-
__version__ = "1.4.5"
3+
__version__ = "1.4.6"

0 commit comments

Comments
 (0)