Streamlit: Search filtering and pagination widget

Emily Nguyen
Streamlit
Published in
3 min readJul 12, 2023

Preface

Over the past year, the Streamlit team wrote a blog post detailing how Streamlit developers can auto-generate a dataframe filtering UI in Streamlit, and Carlos Serrano wrote a Medium article detailing how Streamlit developers can paginate dataframes in Streamlit. Even though the dataframe filtering code operates independently of the dataframe pagination code, both content pieces have provided Streamlit developers with Streamlit native options.

Problem

On a recent client engagement, I presented the dataframe filtering and pagination as separate UI options. In a working session, my client provided feedback and surfaced a new requirement where they wanted to combine the search filtering and pagination widgets to reduce user scrolling.

Solution

View the full solution here: streamlit-widgets/search-filtering-and-pagination/app.py.

Solution Walkthrough

Step 1: Identify and declare any necessary dependencies.

The minimum dependencies needed are streamlit, pandas, and snowflake-snowpark-python.

Step 2: Create an initialize_session() function to pass values to Snowpark’s connection parameters and establish a Snowpark Python session with Snowflake.

For the initialize_session() function, rather than specifying the connection parameters’ values in plaintext, I used st.secrets to retrieve them from privately stored secrets.toml file in my directory.

The initialize_session() function is defined and returns the Snowpark Python session variable.

Step 3: Create a load_data() function and leverage the connected Snowpark session to retrieve data from Snowflake.

The load_data() function is defined and returns the query execution result as a Pandas DataFrame.

Step 4: Create the split_frame() and paginate_df() functions to enable dataframe pagination where the dataframe can either appear as an st.dataframe or st.data_editor object.

The split_frame() and paginate_df() functions are defined and return either a df or df displayed as objects.

Step 5: Create a filter_dataframe() function to enable search filtering with multiselect and text input widgets.

The filter_dataframe() function is defined and returns a variable called df.

Step 6: Create and call the main() function, which calls the load_data(), filter_dataframe(), and paginate_df() functions in their respective order.

The main() function is defined and then called.

Streamlit App

Assuming a user can successfully set up their Snowflake connection via Snowpark Python and all else stays the same with the code that has been shared in this article, a user can see their Streamlit app render the following user interface:

Wrapping Up

Thank you for reading my post! To recap, I provided a comprehensive walkthrough on creating a Streamlit widget that combines dataframe filtering and pagination.

If you have any questions, please post them in the comments below or contact me on LinkedIn.

Happy Streamlit-ing! 🎈

--

--