Skip to content

Commit f2fd659

Browse files
authored
New css option in show & itables.options (#106)
* Use a div with class "itables" and move style before the div * Move the default css to itables.options * Document how to use the custom CSS
1 parent 31689f8 commit f2fd659

File tree

9 files changed

+68
-11
lines changed

9 files changed

+68
-11
lines changed

docs/_toc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ root: quick_start
33
chapters:
44
- file: supported_editors
55
- file: advanced_parameters
6+
- file: custom_css
67
- file: downsampling
78
- file: sample_dataframes
89
- file: references

docs/changelog.md

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

4+
1.3.1 (2022-11-01)
5+
------------------
6+
7+
**Added**
8+
- The `show` method has a new `css` argument (defaults to `itables.options.css`) ([#104](https://github.com/mwouts/itables/issues/104)).
9+
10+
411
1.3.0 (2022-09-04)
512
------------------
613

docs/custom_css.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
jupytext:
3+
formats: md:myst
4+
text_representation:
5+
extension: .md
6+
format_name: myst
7+
format_version: 0.13
8+
jupytext_version: 1.13.8
9+
kernelspec:
10+
display_name: itables
11+
language: python
12+
name: itables
13+
---
14+
15+
## Custom CSS
16+
17+
You can change the CSS used to render the tables
18+
by either passing a custom CSS to the `show` function, or by
19+
changing `opt.css`. Note that the CSS must be the same for all the tables
20+
in a given notebook.
21+
22+
```{code-cell}
23+
from itables import init_notebook_mode, show
24+
from itables.sample_dfs import get_countries
25+
import itables.options as opt
26+
27+
28+
opt.css = """
29+
.itables table td { font-style: italic; }
30+
.itables table th { font-style: oblique; }
31+
"""
32+
33+
init_notebook_mode(all_interactive=True)
34+
```
35+
36+
```{code-cell}
37+
get_countries()
38+
```

itables/html/datatables_template.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
<table id="table_id"><thead><tr><th>A</th></tr></thead></table>
21
<style></style>
2+
<div class="itables">
3+
<table id="table_id"><thead><tr><th>A</th></tr></thead></table>
34
<script>
45
// We use a namespace per table (= the next curly bracket)
56
// to avoid conflicts between the various tables' data & dt_args
@@ -18,3 +19,4 @@
1819
});
1920
}
2021
</script>
22+
</div>

itables/html/datatables_template_connected.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
<table id="table_id"><thead><tr><th>A</th></tr></thead></table>
21
<style></style>
2+
<div class="itables">
3+
<table id="table_id"><thead><tr><th>A</th></tr></thead></table>
34
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
45
<script type="module">
56
// Import jquery and DataTable
@@ -19,3 +20,4 @@
1920
$('#table_id').DataTable(dt_args);
2021
});
2122
</script>
23+
</div>

itables/html/style.css renamed to itables/html/itables.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
table td {
1+
.itables table td {
22
text-overflow: ellipsis;
33
overflow: hidden;
44
}
55

6-
table th {
6+
.itables table th {
77
text-overflow: ellipsis;
88
overflow: hidden;
99
}
1010

11-
thead input {
11+
.itables thead input {
1212
width: 100%;
1313
padding: 3px;
1414
box-sizing: border-box;
1515
}
1616

17-
tfoot input {
17+
.itables tfoot input {
1818
width: 100%;
1919
padding: 3px;
2020
box-sizing: border-box;

itables/javascript.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,17 @@ def to_html_datatable(df=None, tableId=None, connected=True, **kwargs):
230230
"""Return the HTML representation of the given dataframe as an interactive datatable"""
231231
# Default options
232232
for option in dir(opt):
233-
if option not in kwargs and not option.startswith("__"):
233+
if (
234+
option not in kwargs
235+
and not option.startswith("__")
236+
and option != "read_package_file"
237+
):
234238
kwargs[option] = getattr(opt, option)
235239

236240
# These options are used here, not in DataTable
237241
classes = kwargs.pop("classes")
238242
style = kwargs.pop("style")
243+
css = kwargs.pop("css")
239244
tags = kwargs.pop("tags")
240245

241246
showIndex = kwargs.pop("showIndex")
@@ -311,9 +316,7 @@ def to_html_datatable(df=None, tableId=None, connected=True, **kwargs):
311316
output = replace_value(
312317
output,
313318
"<style></style>",
314-
f"""<style>
315-
{read_package_file("html/style.css")}
316-
</style>""",
319+
f"""<style>{css}</style>""",
317320
)
318321

319322
if column_filters:

itables/options.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Global options for the Interactive Tables"""
2+
from .utils import read_package_file
23

34
"""Show the index? Possible values: True, False and 'auto'. In mode 'auto', the index is not shown
45
if it has no name and its content is range(N)"""
@@ -28,3 +29,6 @@
2829

2930
"""Column filters"""
3031
column_filters = False
32+
33+
"""Table CSS"""
34+
css = read_package_file("html/itables.css")

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.3.0"
3+
__version__ = "1.3.1"

0 commit comments

Comments
 (0)