Skip to content

Commit 4e25da6

Browse files
authored
ITables components (Jupyter widget, Dash and Streamlit components) have the same options as show (#377)
* Update datatables.net to 2.3.0 * Parse BigInts in JavaScript * Parse NaN and +/-Infinity in JavaScript * Components get the table data in JSON format * Move the streamlit example app to the main repo * New option 'text_in_header_can_be_selected' * New ITable class in JavaScript * New argument table_html in ITable * Common argument preparation for the html representation and the app components * Evaluate the keys that are encoded with JavascriptCode or Function * New option allow_html which default to False * Pandas style objects use a table_style * Move the doc on app components to a dedicated folder * Document each option separately
1 parent 419281b commit 4e25da6

File tree

93 files changed

+3922
-3444
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+3922
-3444
lines changed

README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
[![Conda Version](https://img.shields.io/conda/vn/conda-forge/itables.svg)](https://anaconda.org/conda-forge/itables)
88
[![pyversions](https://img.shields.io/pypi/pyversions/itables.svg)](https://pypi.python.org/pypi/itables)
99
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
10-
[![Jupyter Widget](https://img.shields.io/badge/Jupyter-Widget-F37626.svg?style=flat&logo=Jupyter)](https://mwouts.github.io/itables/widget.html)
11-
[![Dash Component](https://img.shields.io/badge/Dash-Plotly-1098F7.svg?style=flat&logo=Plotly)](https://mwouts.github.io/itables/dash.html)
10+
[![Jupyter Widget](https://img.shields.io/badge/Jupyter-Widget-F37626.svg?style=flat&logo=Jupyter)](https://mwouts.github.io/itables/apps/widget.html)
11+
[![Dash Component](https://img.shields.io/badge/Dash-Plotly-1098F7.svg?style=flat&logo=Plotly)](https://mwouts.github.io/itables/apps/dash.html)
1212
[![Streamlit App](https://static.streamlit.io/badges/streamlit_badge_black_red.svg)](https://itables.streamlit.app)
1313

1414
This packages changes how Pandas and Polars DataFrames are rendered in Python notebooks and applications.
@@ -51,14 +51,8 @@ and then render any DataFrame as an interactive table that you can sort, search
5151
If you prefer to render only selected DataFrames as interactive tables, call `itables.init_notebook_mode(all_interactive=False)`, then use `itables.show` to show just one Series or DataFrame as an interactive table:
5252
![show](docs/show_df.png)
5353

54-
Since ITables v1.0, the [jQuery](https://jquery.com/) and [DataTables](https://datatables.net/) libraries and CSS
55-
are injected in the notebook when you execute `init_notebook_mode` with its default argument `connected=False`.
56-
Thanks to this the interactive tables will work even without a connection to the internet.
5754

58-
If you prefer to load the libraries dynamically (and keep the notebook lighter), use `connected=True` when you
59-
execute `init_notebook_mode`.
60-
61-
## Supported notebook environments
55+
## ITables in Notebooks
6256

6357
ITables works in all the usual Jupyter Notebook environments, including Jupyter Notebook, Jupyter Lab, Jupyter nbconvert (i.e. the tables are still interactive in the HTML export of a notebook), Jupyter Book, Google Colab and Kaggle.
6458

@@ -68,7 +62,7 @@ ITables works well in VS Code, both in Jupyter Notebooks and in interactive Pyth
6862

6963
## ITables in Python applications
7064

71-
Last but not least, ITables is also available as
65+
ITables is also available as
7266
- a [Jupyter Widget](https://mwouts.github.io/itables/widget.html)
7367
- a [Dash](https://mwouts.github.io/itables/dash.html) component
7468
- a [Streamlit](https://mwouts.github.io/itables/streamlit.html) component,

apps/streamlit/itables_app.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
"""Run this app with: streamlit run apps/streamlit/itables_app.py"""
2+
3+
from typing import Sequence, cast
4+
5+
import pyarrow
6+
import streamlit as st
7+
from st_aggrid import AgGrid # type: ignore
8+
from streamlit.components.v1.components import MarshallComponentException
9+
10+
import itables.options as it_opt
11+
from itables.javascript import get_compact_style, get_expanded_classes
12+
from itables.sample_dfs import get_countries, get_dict_of_test_dfs
13+
from itables.streamlit import interactive_table
14+
15+
st.set_page_config(
16+
page_title="ITables in Streamlit",
17+
page_icon="https://raw.githubusercontent.com/mwouts/itables/main/src/itables/logo/loading.svg",
18+
)
19+
st.logo(
20+
"https://raw.githubusercontent.com/mwouts/itables/main/src/itables/logo/logo.svg",
21+
link="https://mwouts.github.io/itables/streamlit.html",
22+
)
23+
st.markdown(
24+
"![ITables](https://raw.githubusercontent.com/mwouts/itables/main/src/itables/logo/wide.svg)"
25+
)
26+
st.sidebar.markdown(
27+
"""
28+
# ITables in Streamlit
29+
![Stars](https://img.shields.io/github/stars/mwouts/itables)
30+
"""
31+
)
32+
33+
caption = st.sidebar.text_input("Caption", value="Countries")
34+
select = st.sidebar.toggle("Row selection", value=True)
35+
classes = st.sidebar.multiselect(
36+
"Classes",
37+
options=["display", "nowrap", "compact", "cell-border", "stripe"],
38+
default=get_expanded_classes(it_opt.classes),
39+
)
40+
buttons = st.sidebar.multiselect(
41+
"Buttons",
42+
options=["pageLength", "copyHtml5", "csvHtml5", "excelHtml5", "colvis"],
43+
default=["copyHtml5", "csvHtml5", "excelHtml5", "colvis"],
44+
)
45+
46+
style = st.sidebar.text_input("Style", value=get_compact_style(it_opt.style))
47+
48+
render_with = st.sidebar.selectbox(
49+
"Render with", ["st.dataframe", "streamlit-aggrid", "itables"], index=2
50+
)
51+
52+
include_html = st.sidebar.checkbox("Include HTML")
53+
df = get_countries(html=include_html)
54+
55+
it_args = {}
56+
if select:
57+
it_args["select"] = True
58+
it_args["selected_rows"] = [0, 1, 2, 100, 207]
59+
if classes != get_expanded_classes(it_opt.classes):
60+
it_args["classes"] = classes
61+
if style != it_opt.style:
62+
it_args["style"] = style
63+
64+
if buttons:
65+
it_args["buttons"] = buttons
66+
67+
68+
if caption:
69+
it_args = {"caption": caption, **it_args}
70+
71+
if render_with == "st.dataframe":
72+
73+
def render_table(df, key: str, **not_used): # type: ignore
74+
return st.dataframe(df, key=key)
75+
76+
snippet = """st.dataframe(df)
77+
"""
78+
79+
elif render_with == "streamlit-aggrid":
80+
81+
def render_table(df, key: str, **not_used): # type: ignore
82+
return AgGrid(df, key=key)
83+
84+
snippet = """from st_aggrid import AgGrid
85+
86+
AgGrid(df, key=None)
87+
"""
88+
else:
89+
formatted_args = ["df"] + [
90+
f"{key}='{value}'" if isinstance(value, str) else f"{key}={value}"
91+
for key, value in it_args.items()
92+
]
93+
formatted_args = ",\n ".join(formatted_args)
94+
95+
def render_table(df, key, **it_args):
96+
return interactive_table(df, key=key, **it_args)
97+
98+
snippet = f"""from itables.streamlit import interactive_table
99+
100+
interactive_table({formatted_args})
101+
"""
102+
103+
st.markdown(
104+
f"""```python
105+
{snippet}
106+
```
107+
"""
108+
)
109+
110+
t = render_table(df, "my_table", **it_args)
111+
112+
st.header("Table state")
113+
st.markdown(
114+
"""The value returned by `interactive_table` is
115+
a dict that contains the index of the selected rows:"""
116+
)
117+
st.write(t)
118+
119+
st.header("More sample dataframes")
120+
test_dfs = get_dict_of_test_dfs()
121+
tabs = st.tabs(cast(Sequence[str], test_dfs.keys()))
122+
123+
for (name, df), tab in zip(test_dfs.items(), tabs):
124+
with tab:
125+
try:
126+
interactive_table(df, key=name, classes=classes, style=style)
127+
except (
128+
# ITables
129+
NotImplementedError,
130+
# st.dataframe
131+
ValueError,
132+
# streamlit-aggrid
133+
pyarrow.lib.ArrowInvalid, # type: ignore
134+
MarshallComponentException,
135+
) as e:
136+
st.warning(e)

apps/streamlit/requirements.txt

Whitespace-only changes.

docs/_toc.yml

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,59 @@
11
format: jb-book
22
root: quick_start
33
parts:
4-
- caption: ITables in Notebooks
4+
- caption: Using ITables
55
chapters:
6-
- file: advanced_parameters
7-
- file: select
8-
- file: extensions
9-
- file: custom_extensions
10-
- file: custom_css
6+
- file: apps/notebook
7+
- file: apps/html
8+
- file: apps/widget
9+
- file: apps/dash
10+
- file: apps/streamlit
11+
- file: apps/shiny
12+
- file: apps/quarto
13+
- caption: Options
14+
chapters:
15+
- file: options/options
16+
- file: options/classes
17+
- file: options/style
18+
- file: options/caption
19+
- file: options/layout
20+
- file: options/column_defs
21+
- file: options/search
22+
- file: options/length_menu
23+
- file: options/paging
24+
- file: options/scroll_y
25+
- file: options/scroll_x
26+
- file: options/footer
27+
- file: options/column_filters
28+
- file: options/order
29+
- file: options/state_save
30+
- file: options/select
31+
- file: options/buttons
32+
- file: options/colvis
33+
- file: options/search_panes
34+
- file: options/search_builder
35+
- file: options/fixed_columns
36+
- file: options/row_group
37+
- file: options/keys
38+
- file: options/allow_html
39+
- file: options/show_index
40+
- caption: More about ITables
41+
chapters:
42+
- file: downsampling
1143
- file: formatting
12-
- file: pandas_style
1344
- file: dark_mode
14-
- file: quarto
15-
- file: downsampling
45+
- file: css
46+
- file: custom_extensions
1647
- file: references
1748
- file: supported_editors
18-
- caption: ITables in Applications
19-
chapters:
20-
- file: widget
21-
- file: streamlit
22-
- file: shiny
23-
- file: dash
24-
- file: html_export
2549
- caption: Contributing to ITables
2650
chapters:
2751
- file: contributing
2852
- file: developing
2953
- file: troubleshooting
3054
- file: changelog
31-
- caption: Example DataFrames
55+
- caption: Examples
3256
chapters:
33-
- file: sample_dataframes
57+
- file: pandas_dataframes
58+
- file: pandas_style
3459
- file: polars_dataframes

0 commit comments

Comments
 (0)