Skip to content

Commit

Permalink
refactor(ui): rename "toolbar_select" widget to generic "select"
Browse files Browse the repository at this point in the history
  • Loading branch information
aarthy-dk committed Sep 30, 2024
1 parent 764bd12 commit bed5d1c
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 67 deletions.
2 changes: 1 addition & 1 deletion testgen/ui/components/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
no_flex_gap,
page_header,
text,
toolbar_select,
whitespace,
)
from testgen.ui.components.widgets.paginator import paginator
from testgen.ui.components.widgets.select import select
from testgen.ui.components.widgets.sidebar import sidebar
from testgen.ui.components.widgets.sorting_selector import sorting_selector
from testgen.ui.components.widgets.summary_bar import summary_bar
48 changes: 0 additions & 48 deletions testgen/ui/components/widgets/page.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import pandas as pd
import streamlit as st
from streamlit.delta_generator import DeltaGenerator
from streamlit_extras.no_default_selectbox import selectbox

from testgen.ui.components.widgets.breadcrumbs import Breadcrumb
from testgen.ui.components.widgets.breadcrumbs import breadcrumbs as tg_breadcrumbs
from testgen.ui.navigation.router import Router


def page_header(
Expand Down Expand Up @@ -34,51 +31,6 @@ def page_header(
st.session_state["last_page"] = title


def toolbar_select(
options: pd.DataFrame | list[str],
value_column: str | None = None,
display_column: str | None = None,
default_value = None,
required: bool = False,
bind_to_query: str | None = None,
**kwargs,
):
kwargs = {**kwargs}

if isinstance(options, pd.DataFrame):
value_column = value_column or options.columns[0]
display_column = display_column or value_column
kwargs["options"] = options[display_column]
if default_value in options[value_column].values:
kwargs["index"] = int(options[options[value_column] == default_value].index[0]) + (0 if required else 1)
else:
kwargs["options"] = options
if default_value in options:
kwargs["index"] = options.index(default_value) + (0 if required else 1)

if bind_to_query:
kwargs["key"] = kwargs.get("key", f"toolbar_select_{bind_to_query}")
if default_value is not None and kwargs.get("index") is None:
Router().set_query_params({ bind_to_query: None }) # Unset the query params if the current value is not valid

def update_query_params():
query_value = st.session_state[kwargs["key"]]
if not required and query_value == "---":
query_value = None
elif isinstance(options, pd.DataFrame):
query_value = options.loc[options[display_column] == query_value, value_column].iloc[0]
Router().set_query_params({ bind_to_query: query_value })

kwargs["on_change"] = update_query_params

selected = st.selectbox(**kwargs) if required else selectbox(**kwargs)

if selected and isinstance(options, pd.DataFrame):
return options.loc[options[display_column] == selected, value_column].iloc[0]

return selected


def whitespace(size: float, container: DeltaGenerator | None = None):
_apply_html(f'<div style="height: {size}rem"></div>', container)

Expand Down
52 changes: 52 additions & 0 deletions testgen/ui/components/widgets/select.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pandas as pd
import streamlit as st
from streamlit_extras.no_default_selectbox import selectbox

from testgen.ui.navigation.router import Router


def select(
label: str,
options: pd.DataFrame | list[str],
value_column: str | None = None,
display_column: str | None = None,
default_value = None,
required: bool = False,
bind_to_query: str | None = None,
**kwargs,
):
kwargs = {**kwargs}
kwargs["label"] = label

if isinstance(options, pd.DataFrame):
value_column = value_column or options.columns[0]
display_column = display_column or value_column
kwargs["options"] = options[display_column]
if default_value in options[value_column].values:
kwargs["index"] = int(options[options[value_column] == default_value].index[0]) + (0 if required else 1)
else:
kwargs["options"] = options
if default_value in options:
kwargs["index"] = options.index(default_value) + (0 if required else 1)

if bind_to_query:
kwargs["key"] = kwargs.get("key", f"testgen_select_{bind_to_query}")
if default_value is not None and kwargs.get("index") is None:
Router().set_query_params({ bind_to_query: None }) # Unset the query params if the current value is not valid

def update_query_params():
query_value = st.session_state[kwargs["key"]]
if not required and query_value == "---":
query_value = None
elif isinstance(options, pd.DataFrame):
query_value = options.loc[options[display_column] == query_value, value_column].iloc[0]
Router().set_query_params({ bind_to_query: query_value })

kwargs["on_change"] = update_query_params

selected = st.selectbox(**kwargs) if required else selectbox(**kwargs)

if selected and isinstance(options, pd.DataFrame):
return options.loc[options[display_column] == selected, value_column].iloc[0]

return selected
4 changes: 2 additions & 2 deletions testgen/ui/views/profiling_anomalies.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def render(self, run_id: str, issue_class: str | None = None, issue_type: str |
testgen.flex_row_end(export_button_column)

with liklihood_filter_column:
issue_class = testgen.toolbar_select(
issue_class = testgen.select(
options=["Definite", "Likely", "Possible", "Potential PII"],
default_value=issue_class,
required=False,
Expand All @@ -61,7 +61,7 @@ def render(self, run_id: str, issue_class: str | None = None, issue_type: str |

with issue_type_filter_column:
issue_type_options = get_issue_types()
issue_type_id = testgen.toolbar_select(
issue_type_id = testgen.select(
options=issue_type_options,
default_value=None if issue_class == "Potential PII" else issue_type,
value_column="id",
Expand Down
4 changes: 2 additions & 2 deletions testgen/ui/views/profiling_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def render(self, run_id: str, table_name: str | None = None, column_name: str |
with table_filter_column:
# Table Name filter
df = profiling_queries.run_table_lookup_query(table_group_id)
table_name = testgen.toolbar_select(
table_name = testgen.select(
options=df,
value_column="table_name",
default_value=table_name,
Expand All @@ -62,7 +62,7 @@ def render(self, run_id: str, table_name: str | None = None, column_name: str |
with column_filter_column:
# Column Name filter
df = profiling_queries.run_column_lookup_query(table_group_id, table_name)
column_name = testgen.toolbar_select(
column_name = testgen.select(
options=df,
value_column="column_name",
default_value=column_name,
Expand Down
2 changes: 1 addition & 1 deletion testgen/ui/views/profiling_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def render(self, project_code: str | None = None, table_group_id: str | None = N

with group_filter_column:
table_groups_df = get_db_table_group_choices(project_code)
table_group_id = testgen.toolbar_select(
table_group_id = testgen.select(
options=table_groups_df,
value_column="id",
display_column="table_groups_name",
Expand Down
4 changes: 2 additions & 2 deletions testgen/ui/views/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def render(self, test_suite_id: str, table_name: str | None = None, column_name:

with table_filter_column:
table_options = run_table_lookup_query(table_group["id"])
table_name = testgen.toolbar_select(
table_name = testgen.select(
options=table_options,
value_column="table_name",
default_value=table_name,
Expand All @@ -69,7 +69,7 @@ def render(self, test_suite_id: str, table_name: str | None = None, column_name:
)
with column_filter_column:
column_options = get_column_names(table_group["id"], table_name)
column_name = testgen.toolbar_select(
column_name = testgen.select(
options=column_options,
default_value=column_name,
bind_to_query="column_name",
Expand Down
4 changes: 2 additions & 2 deletions testgen/ui/views/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def render(self, run_id: str, status: str | None = None, test_type: str | None =
"Warning",
"Passed",
]
status = testgen.toolbar_select(
status = testgen.select(
options=status_options,
default_value=status or "Failed + Warning",
required=False,
Expand All @@ -76,7 +76,7 @@ def render(self, run_id: str, status: str | None = None, test_type: str | None =
)

with test_type_filter_column:
test_type = testgen.toolbar_select(
test_type = testgen.select(
options=get_test_types(),
value_column="test_type",
display_column="test_name_short",
Expand Down
8 changes: 4 additions & 4 deletions testgen/ui/views/test_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def render(self, project_code: str | None = None, table_group_id: str | None = N

with group_filter_column:
table_groups_df = get_db_table_group_choices(project_code)
table_groups_id = testgen.toolbar_select(
table_group_id = testgen.select(
options=table_groups_df,
value_column="id",
display_column="table_groups_name",
Expand All @@ -49,8 +49,8 @@ def render(self, project_code: str | None = None, table_group_id: str | None = N
)

with suite_filter_column:
test_suites_df = get_db_test_suite_choices(project_code, table_groups_id)
test_suite_id = testgen.toolbar_select(
test_suites_df = get_db_test_suite_choices(project_code, table_group_id)
test_suite_id = testgen.select(
options=test_suites_df,
value_column="id",
display_column="test_suite",
Expand All @@ -65,7 +65,7 @@ def render(self, project_code: str | None = None, table_group_id: str | None = N
testgen.whitespace(0.5)
list_container = st.container(border=True)

test_runs_df = get_db_test_runs(project_code, table_groups_id, test_suite_id)
test_runs_df = get_db_test_runs(project_code, table_group_id, test_suite_id)

run_count = len(test_runs_df)
page_index = testgen.paginator(count=run_count, page_size=PAGE_SIZE)
Expand Down
11 changes: 6 additions & 5 deletions testgen/ui/views/test_suites.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def render(self, project_code: str | None = None, table_group_id: str | None = N
"https://docs.datakitchen.io/article/dataops-testgen-help/create-a-test-suite",
)

table_groups_df = get_db_table_group_choices(project_code)
add_button_onclick = partial(add_test_suite_dialog, project_code, table_groups_df)
group_filter_column, actions_column = st.columns([.2, .8], vertical_alignment="bottom")
testgen.flex_row_end(actions_column)

with group_filter_column:
df_tg = get_db_table_group_choices(project_code)
table_group_id = testgen.toolbar_select(
options=df_tg,
table_group_id = testgen.select(
options=table_groups_df,
value_column="id",
display_column="table_groups_name",
default_value=table_group_id,
Expand All @@ -59,7 +60,7 @@ def render(self, project_code: str | None = None, table_group_id: str | None = N
":material/add: Add Test Suite",
key="test_suite:keys:add",
help="Add a new test suite",
on_click=lambda: add_test_suite_dialog(project_code, df_tg),
on_click=add_button_onclick,
)

for _, test_suite in df.iterrows():
Expand All @@ -80,7 +81,7 @@ def render(self, project_code: str | None = None, table_group_id: str | None = N
icon="edit",
tooltip="Edit test suite",
tooltip_position="right",
on_click=partial(edit_test_suite_dialog, project_code, df_tg, test_suite),
on_click=partial(edit_test_suite_dialog, project_code, table_groups_df, test_suite),
key=f"test_suite:keys:edit:{test_suite['id']}",
)
testgen.button(
Expand Down

0 comments on commit bed5d1c

Please sign in to comment.