Skip to content

Commit

Permalink
Fix: jQuery is already loaded in Shiny apps
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jun 11, 2023
1 parent dbc66c1 commit ccabf98
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
16 changes: 12 additions & 4 deletions docs/supported_editors.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,23 @@ because otherwise the notebooks do not display the interactive tables when they

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:
```python
from itables import to_html_datatable as DT
from itables import to_html_datatable
from itables.sample_dfs import get_countries

df = get_countries()
html = DT(df)
html = to_html_datatable(df)
```

# Using ITables in Shiny

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).
You can use ITables in Web applications generated with [Shiny](https://shiny.rstudio.com/py/) for Python with e.g.
```python
from shiny import ui
from itables.shiny import DT

ui.HTML(DT(df))
```

See also our [tested examples](https://github.com/mwouts/itables/tree/main/tests/sample_python_apps).

ITables can't be used in Dash because jQuery is not usable in Dash (see the [Dash FAQ](https://dash.plotly.com/faqs)).
ITables won't work in Dash because jQuery is not usable in Dash (see the [Dash FAQ](https://dash.plotly.com/faqs)).
12 changes: 11 additions & 1 deletion itables/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ def _datatables_repr_(df=None, tableId=None, **kwargs):
return to_html_datatable(df, tableId, connected=_CONNECTED, **kwargs)


def to_html_datatable(df=None, caption=None, tableId=None, connected=True, **kwargs):
def to_html_datatable(
df=None, caption=None, tableId=None, connected=True, import_jquery=True, **kwargs
):
"""Return the HTML representation of the given dataframe as an interactive datatable"""
# Default options
for option in dir(opt):
Expand Down Expand Up @@ -324,6 +326,14 @@ def to_html_datatable(df=None, caption=None, tableId=None, connected=True, **kwa
else:
output = read_package_file("html/datatables_template.html")

if not import_jquery:
assert (
connected
), "In the offline mode, jQuery is imported through init_notebook_mode"
output = replace_value(
output, " import 'https://code.jquery.com/jquery-3.6.0.min.js';\n", ""
)

tableId = tableId or str(uuid.uuid4())
if isinstance(classes, list):
classes = " ".join(classes)
Expand Down
17 changes: 17 additions & 0 deletions itables/shiny.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .javascript import to_html_datatable


def DT(df, caption=None, tableId=None, **kwargs):
"""jQuery is already loaded in Shiny applications
Cf. https://github.com/mwouts/itables/issues/181
and https://github.com/rstudio/py-shiny/issues/502
"""
return to_html_datatable(
df,
caption=caption,
tableId=tableId,
connected=True,
import_jquery=False,
**kwargs
)
2 changes: 1 addition & 1 deletion tests/sample_python_apps/itables_in_a_shiny_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from shiny import App, ui

from itables import to_html_datatable as DT
from itables.sample_dfs import get_countries
from itables.shiny import DT

df = get_countries()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from shiny import App, ui

from itables import to_html_datatable as DT
from itables.sample_dfs import get_dict_of_test_dfs
from itables.shiny import DT

app_ui = ui.page_fluid(
# Display the different tables in different tabs
Expand Down

0 comments on commit ccabf98

Please sign in to comment.