Skip to content

Commit c43ead0

Browse files
authored
feat: add explicit import_luxon_dependency() helper to enable Tabulator date/time features (#40)
1 parent 4481294 commit c43ead0

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,41 @@ use_theme('bootstrap4', shared=False)
194194
def my_page():
195195
# use the theme only for this page
196196
use_theme('bootstrap4')
197-
```
197+
```
198+
199+
200+
### Dates & Times (Luxon)
201+
202+
Tabulator’s date/time features (e.g. `formatter: "datetime"`, `sorter: "date"` or `sorter: "datetime"`)
203+
require **Luxon**. `nicegui-tabulator` does not bundle Luxon; enable it explicitly:
204+
205+
```python
206+
from nicegui import ui
207+
from nicegui_tabulator import tabulator, import_luxon
208+
209+
# Inject Luxon before creating tables that use date/time formatting/sorting
210+
import_luxon(shared=True) # (app-wide)
211+
212+
tabledata = [
213+
{"id": 1, "name": "Oli Bob", "dob": "1982-05-14"},
214+
{"id": 2, "name": "Mary May", "dob": "1999-01-31"},
215+
]
216+
217+
table_config = {
218+
"layout": "fitColumns",
219+
"data": tabledata,
220+
"columns": [
221+
{"title": "Name", "field": "name"},
222+
{
223+
"title": "Date Of Birth",
224+
"field": "dob",
225+
"formatter": "datetime",
226+
"formatterParams": {"inputFormat": "iso", "outputFormat": "dd/MM/yyyy"},
227+
"sorter": "date",
228+
"sorterParams": {"format": "iso"},
229+
},
230+
],
231+
}
232+
233+
tabulator(table_config)
234+
ui.run()

nicegui_tabulator/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
from .core.tabulator import Tabulator as tabulator
33
from .core.types import CellSlotProps
44
from .core.themes import use_theme
5+
from .core.dependencies import import_luxon
56

6-
__all__ = ["__version__", "tabulator", "CellSlotProps", "use_theme"]
7+
__all__ = ["__version__", "tabulator", "CellSlotProps", "use_theme", "import_luxon"]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import Final, Optional
2+
from nicegui import ui
3+
4+
LUXON_DEFAULT_SCRIPT_URL: Final[str] = "https://cdn.jsdelivr.net/npm/luxon@3/build/global/luxon.min.js"
5+
6+
7+
def import_luxon(shared: Optional[bool] = None, script_url: str = LUXON_DEFAULT_SCRIPT_URL):
8+
"""Inject Luxon into the page so Tabulator date/time features can work.
9+
10+
Tabulator's date/time formatters and sorters require Luxon to be available in the
11+
browser environment. Call this function **before** creating tables that use any of:
12+
- ``formatter: "datetime"``
13+
- ``sorter: "date"`` or ``sorter: "datetime"``
14+
- editors that depend on Luxon (if used in the future)
15+
16+
Args:
17+
shared: Whether to import the dependency for all clients or only the current client.
18+
`None`(default): import the dependency for all clients if the current client is an auto-index client, otherwise use it only for the current client.
19+
`True`: import for all clients.
20+
`False`: import only for the current client.
21+
script_url: The URL of Luxon's global build. Override this if you self-host
22+
Luxon or want to pin to a different version.
23+
24+
Notes:
25+
- Call it once during your page/app setup, before creating Tabulator grids that
26+
require date/time formatting/sorting.
27+
"""
28+
if shared is None:
29+
shared = ui.context.client.is_auto_index_client
30+
31+
ui.add_head_html(f'<script src="{script_url}"></script>', shared=shared)

0 commit comments

Comments
 (0)