11"""Run this app with: streamlit run apps/streamlit/itables_app.py"""
22
3- from typing import Sequence , cast
3+ from typing import Optional , Sequence , cast
44
5- import pyarrow
5+ import pyarrow # type: ignore
66import streamlit as st
7+ from typing_extensions import Unpack
78
89try :
910 from st_aggrid import AgGrid # type: ignore
1011except ImportError as e :
1112 import_error = e
1213
1314 class AgGrid :
14- def __init__ (self , * args , ** kwargs ):
15+ def __init__ (self , * args , ** kwargs ): # type: ignore
1516 raise import_error
1617
1718
1819from streamlit .components .v1 .components import MarshallComponentException
1920
20- import itables .options as it_opt
21- from itables .javascript import get_compact_style , get_expanded_classes
22- from itables .sample_dfs import get_countries , get_dict_of_test_dfs
21+ import itables
2322from itables .streamlit import interactive_table
2423
2524st .set_page_config (
@@ -45,51 +44,49 @@ def __init__(self, *args, **kwargs):
4544classes = st .sidebar .multiselect (
4645 "Classes" ,
4746 options = ["display" , "nowrap" , "compact" , "cell-border" , "stripe" ],
48- default = get_expanded_classes (it_opt .classes ),
47+ default = itables . javascript . get_expanded_classes (itables . options .classes ),
4948)
5049buttons = st .sidebar .multiselect (
5150 "Buttons" ,
5251 options = ["pageLength" , "copyHtml5" , "csvHtml5" , "excelHtml5" , "colvis" ],
5352 default = ["copyHtml5" , "csvHtml5" , "excelHtml5" , "colvis" ],
5453)
5554
56- style = st .sidebar .text_input ("Style" , value = get_compact_style (it_opt .style ))
55+ style = st .sidebar .text_input (
56+ "Style" , value = itables .javascript .get_compact_style (itables .options .style )
57+ )
5758
5859render_with = st .sidebar .selectbox (
5960 "Render with" , ["st.dataframe" , "streamlit-aggrid" , "itables" ], index = 2
6061)
6162
6263include_html = st .sidebar .checkbox ("Include HTML" )
63- df = get_countries (html = include_html )
64+ df = itables . sample_dfs . get_countries (html = include_html )
6465
65- it_args = {}
66+ it_args : itables . ITableOptions = {}
6667if select :
6768 it_args ["select" ] = True
6869 it_args ["selected_rows" ] = [0 , 1 , 2 , 100 , 207 ]
69- if classes != get_expanded_classes (it_opt .classes ):
70+ if classes != itables . javascript . get_expanded_classes (itables . options .classes ):
7071 it_args ["classes" ] = classes
71- if style != it_opt .style :
72+ if style != itables . options .style :
7273 it_args ["style" ] = style
7374
7475if buttons :
7576 it_args ["buttons" ] = buttons
7677
77-
78- if caption :
79- it_args = {"caption" : caption , ** it_args }
80-
8178if render_with == "st.dataframe" :
8279
83- def render_table (df , key : str , ** not_used ): # type: ignore
84- return st .dataframe (df , key = key )
80+ def render_table (df , key : str , caption : Optional [ str ], ** not_used ): # type: ignore
81+ return st .dataframe (df , key = key ) # type: ignore
8582
8683 snippet = """st.dataframe(df)
8784"""
8885
8986elif render_with == "streamlit-aggrid" :
9087
91- def render_table (df , key : str , ** not_used ): # type: ignore
92- return AgGrid (df , key = key )
88+ def render_table (df , key : str , caption : Optional [ str ], ** not_used ): # type: ignore
89+ return AgGrid (df , key = key ) # type: ignore
9390
9491 snippet = """from st_aggrid import AgGrid
9592
@@ -102,12 +99,17 @@ def render_table(df, key: str, **not_used): # type: ignore
10299 ]
103100 formatted_args = ",\n " .join (formatted_args )
104101
105- def render_table (df , key , ** it_args ):
106- return interactive_table (df , key = key , ** it_args )
102+ def render_table (
103+ df : itables .DataFrameOrSeries ,
104+ key : str ,
105+ caption : Optional [str ],
106+ ** it_args : Unpack [itables .ITableOptions ],
107+ ):
108+ return interactive_table (df , key = key , caption = caption , ** it_args )
107109
108110 snippet = f"""from itables.streamlit import interactive_table
109111
110- interactive_table({ formatted_args } )
112+ interactive_table(df, caption=' { caption } ', { formatted_args } )
111113"""
112114
113115st .markdown (
@@ -117,17 +119,17 @@ def render_table(df, key, **it_args):
117119"""
118120)
119121
120- t = render_table (df , "my_table" , ** it_args )
122+ t = render_table (df , caption = caption , key = "my_table" , ** it_args )
121123
122124st .header ("Table state" )
123125st .markdown (
124126 """The value returned by `interactive_table` is
125127a dict that contains the index of the selected rows:"""
126128)
127- st .write (t )
129+ st .write (t ) # type: ignore
128130
129131st .header ("More sample dataframes" )
130- test_dfs = get_dict_of_test_dfs ()
132+ test_dfs = itables . sample_dfs . get_dict_of_test_dfs ()
131133tabs = st .tabs (cast (Sequence [str ], test_dfs .keys ()))
132134
133135for (name , df ), tab in zip (test_dfs .items (), tabs ):
@@ -140,7 +142,7 @@ def render_table(df, key, **it_args):
140142 # st.dataframe
141143 ValueError ,
142144 # streamlit-aggrid
143- pyarrow .lib .ArrowInvalid , # type : ignore
145+ pyarrow .lib .ArrowInvalid , # pyright : ignore[reportUnknownMemberType,reportAttributeAccessIssue]
144146 MarshallComponentException ,
145- ) as e :
146- st .warning (e )
147+ ) as e : # pyright: ignore[reportUnknownVariableType]
148+ st .warning (e ) # pyright: ignore[reportUnknownArgumentType]
0 commit comments