Skip to content

Commit ffed002

Browse files
authored
Itables works with Shiny for Python (#92)
* New function 'to_html_datatable' * ITables works with Shiny * Skip sample apps test in Python 3.6
1 parent 30f2079 commit ffed002

File tree

11 files changed

+97
-8
lines changed

11 files changed

+97
-8
lines changed

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.2.0 (2022-08-15)
5+
------------------
6+
7+
**Added**
8+
- New `to_html_datatable` function to export a DataFrame to an HTML div (#88)
9+
- We have added examples on how to use `itables` in [Shiny](https://shiny.rstudio.com/py/) for Python (#86)
10+
11+
412
1.1.3 (2022-08-11)
513
------------------
614

docs/supported_editors.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,20 @@ In PyCharm we recommend to call `init_notebook_mode` with the `connected=True` a
4242
because otherwise the notebooks do not display the interactive tables when they are reloaded.
4343

4444
![](images/pycharm.png)
45+
46+
# Exporting a DataFrame to an HTML table
47+
48+
To get the HTML representation of a Pandas DataFrame `df` as an interactive [datatable](https://datatables.net/), you can use `to_html_datatable` as below:
49+
```python
50+
from itables import to_html_datatable as DT
51+
from itables.sample_dfs import get_countries
52+
53+
df = get_countries()
54+
html = DT(df)
55+
```
56+
57+
# Using ITables in Shiny
58+
59+
You can use ITables in Web applications generated with [Shiny](https://shiny.rstudio.com/py/) for Python, see our [tested examples](https://github.com/mwouts/itables/tree/main/tests/sample_python_apps).
60+
61+
ITables can't be used in Dash because jQuery is not usable in Dash (see the [Dash FAQ](https://dash.plotly.com/faqs)).

environment.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ dependencies:
2323
- setuptools
2424
- twine
2525
- ghp-import
26+
# Interactive applications
27+
- shiny
2628
- pip:
2729
- world_bank_data
2830
- jupyter_book>=0.12 # jupyter-book-0.12.2-pyhd8ed1ab_0 requires jupytext >=1.11.2,<1.12

itables/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
from .javascript import JavascriptFunction, init_notebook_mode, show
1+
from .javascript import JavascriptFunction, init_notebook_mode, show, to_html_datatable
22
from .version import __version__
33

4-
__all__ = ["__version__", "show", "init_notebook_mode", "JavascriptFunction"]
4+
__all__ = [
5+
"__version__",
6+
"to_html_datatable",
7+
"show",
8+
"init_notebook_mode",
9+
"JavascriptFunction",
10+
]

itables/javascript.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,11 @@ def __init__(self, value):
220220

221221

222222
def _datatables_repr_(df=None, tableId=None, **kwargs):
223-
"""Return the HTML/javascript representation of the table"""
223+
return to_html_datatable(df, tableId, connected=_CONNECTED, **kwargs)
224224

225+
226+
def to_html_datatable(df=None, tableId=None, connected=True, **kwargs):
227+
"""Return the HTML representation of the given dataframe as an interactive datatable"""
225228
# Default options
226229
for option in dir(opt):
227230
if option not in kwargs and not option.startswith("__"):
@@ -264,7 +267,7 @@ def _datatables_repr_(df=None, tableId=None, **kwargs):
264267
kwargs["paging"] = False
265268

266269
# Load the HTML template
267-
if _CONNECTED:
270+
if connected:
268271
output = read_package_file("html/datatables_template_connected.html")
269272
else:
270273
output = read_package_file("html/datatables_template.html")
@@ -337,5 +340,5 @@ def _datatables_repr_(df=None, tableId=None, **kwargs):
337340

338341
def show(df=None, **kwargs):
339342
"""Show a dataframe"""
340-
html = _datatables_repr_(df, **kwargs)
343+
html = to_html_datatable(df, connected=_CONNECTED, **kwargs)
341344
display(HTML(html))

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.1.3"
3+
__version__ = "1.2.0"

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ nbconvert
99
jupyter_client
1010
ipykernel
1111
world_bank_data
12+
shiny
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from shiny import App, ui
2+
3+
from itables import to_html_datatable as DT
4+
from itables.sample_dfs import get_countries
5+
6+
df = get_countries()
7+
8+
app_ui = ui.page_fluid(ui.HTML(DT(df)))
9+
10+
app = App(app_ui, server=None)
11+
# app.run()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from shiny import App, ui
2+
3+
from itables import to_html_datatable as DT
4+
from itables.sample_dfs import get_dict_of_test_dfs
5+
6+
app_ui = ui.page_fluid(
7+
# Display the different tables in different tabs
8+
ui.navset_tab(
9+
*[ui.nav(name, ui.HTML(DT(df))) for name, df in get_dict_of_test_dfs().items()]
10+
)
11+
)
12+
13+
app = App(app_ui, server=None)
14+
# app.run()

tests/test_javascript.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from itables import javascript
44

55

6-
def test_datatables_repr_max_columns_none():
6+
def test_to_html_datatable_max_columns_none():
77
test_df = pd.DataFrame([1, 2])
88
with pd.option_context("display.max_columns", None):
9-
html = javascript._datatables_repr_(test_df)
9+
html = javascript.to_html_datatable(test_df)
1010
assert html

0 commit comments

Comments
 (0)