Table_column="average_weekly_workout_count", St.session_state.clicked = st.form_submit_button(label="Submit") List(_get_human_filter_names(st.session_state.filters)), """Should include dynamically generated filters""" Return any(pluck("is_enabled", st.session_state.filters))ĭef _get_human_filter_names(_iter: Iterable) -> Iterable: Return filter(lambda _: _.is_enabled, st.session_state.filters) If you rewrite the main.py to import the filter class and draw the sidebar, you’ll see the dynamic filters on the page: from typing import Iterable Return dict(lower_bound=_val, upper_bound=_val) _val = st.session_state.get(self.widget_id)Įlif self.widget_type is st.select_slider: """Custom unpack function that retrieves the value of the filterįrom session state in a format compatible with self._df_method""" Return methodcaller(self.df_method, **(self._get_filter_value()))( """This method turns this class into a functor allowing to filter the dataframe. This will allow us to use them more expressively: def _call_(self, _table: Table): To apply the filters to a sequence of dataframes, let’s make the class callable. # Invocation of the streamlit method to place the widget on the page Widget_kwargs = dict(label=f"", key=self.widget_id) Let’s add a way to render OurFilter in Streamlit and show some text to the user: def create_widget(self): If self.widget_type not in (st.select_slider, st.checkbox): This hides the internals of implementation from the class interface: def _post_init_(self): You can define the _post_init_ dataclass method to automatically populate the value of _max_value and control the value of _df_method. # dataframe method that will be used for filtering the data Can be used for filtering the list of filters # Field to track if the filter is active. # The type of streamlit widget to generate # Column in the table which will be used for filtering Each filter represents its own column in the table, so it’ll be table_column.įor the OurFilter class to have a generic interface, keep the name of the dataframe’s filtering method as an internal variable: from dataclasses import dataclassįrom import OurFilter: I’m working with a single table in Snowflake, so I’ll keep the table_name as yet another class variable. You’ll need a way to connect to Snowflake so the Snowpark session could be shared among all instances of class. Next, add the other side-the one that will work with Snowpark to filter the data. Is_enabled: bool = False # Controls whether the filter has been enabled. Other elements could be implemented similarly Widget_type: callable # Should be one of st.checkbox or st.select_slider. It is created to parametrize the creation of filters from Streamlit and to keep the state.""" """This dataclass represents the filter that can be optionally enabled. Your starting class will look like this: OurFilter: I used a checkbox and slider widgets as an example, but you can extend the code to work with other Streamlit components. Since the filters can't be both static (an on/off checkbox) and dynamic and depend on the source data (slider), you need a way to store the maximum value that’s retrieved from the table - instance field _max_value. To access your filter’s state across the application, give it a widget_id. To control the widget presentation, you’ll need a human_name field (when the page prompts for inputs and it’s showing information in the charts) and a widget_type field (to create an interactive element). It provides a great framework for writing a class and has some convenient methods you’ll be using. Let’s use Python’s awesome dataclasses as the basis for the class. The data is retrieved from Snowflake and transformed by using dataframes, so your filter class will be the bridge between the Streamlit framework and the Snowpark dataframes. Next, implement the dynamic filters in the sidebar. Build out the UI with draw_sidebar and draw_main_ui: To make your app look beautiful, use the sidebar and the main area to visually separate the filters.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |