From 792357fc9f1057e685220be0154802c740b76af6 Mon Sep 17 00:00:00 2001 From: "houhan@gmail.com" Date: Fri, 23 Feb 2024 01:36:42 +0000 Subject: [PATCH 1/2] use dropdown for curriculum selection --- code/Home.py | 93 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/code/Home.py b/code/Home.py index 1afbd44..fa804d0 100644 --- a/code/Home.py +++ b/code/Home.py @@ -79,9 +79,9 @@ 'auto_training_history_x_axis': 'date', 'auto_training_history_sort_by': 'subject_id', 'auto_training_history_sort_order': 'descending', - 'auto_training_curriculum_name': '', - 'auto_training_curriculum_version': '', - 'auto_training_curriculum_schema_version': '', + 'auto_training_curriculum_name': 'Uncoupled Baiting', + 'auto_training_curriculum_version': '1.0', + 'auto_training_curriculum_schema_version': '1.0', } @@ -643,49 +643,74 @@ def app(): with placeholder: # Show curriculum manager dataframe st.markdown("#### Available auto training curriculums") - cols = st.columns([1, 1]) - # Get curriculum from previous selected or the URL - if 'auto_training_curriculum_name' in st.session_state: - selected_row = {'curriculum_name': st.session_state['auto_training_curriculum_name'], - 'curriculum_schema_version': st.session_state['auto_training_curriculum_schema_version'], - 'curriculum_version': st.session_state['auto_training_curriculum_version']} - matched_curriculum = df_curriculums[(df_curriculums[list(selected_row)] == pd.Series(selected_row)).all(axis=1)] - - if len(matched_curriculum): - pre_selected_rows = matched_curriculum.index.to_list() - else: - selected_row = None # Clear selected row if not found - pre_selected_rows = None + aggrid_interactive_table_curriculum(df=df_curriculums) + + # Curriculum drop down selector + cols = st.columns([1, 1, 1, 4]) + options = list(df_curriculums['curriculum_name'].unique()) + selected_curriculum_name = cols[0].selectbox( + 'Curriculum name', + options=options, + index=options.index(st.session_state['auto_training_curriculum_name']) + if ('auto_training_curriculum_name' in st.session_state) and (st.session_state['auto_training_curriculum_name'] != '') else + options.index(st.query_params['auto_training_curriculum_name']) + if 'auto_training_curriculum_name' in st.query_params and st.query_params['auto_training_curriculum_name'] != '' + else 0, + key='auto_training_curriculum_name' + ) - with cols[0]: - aggrid_curriculum_outputs = aggrid_interactive_table_curriculum(df=df_curriculums, - pre_selected_rows=pre_selected_rows) + options = list(df_curriculums[ + df_curriculums['curriculum_name'] == selected_curriculum_name + ]['curriculum_version'].unique()) + if ('auto_training_curriculum_version' in st.session_state) and (st.session_state['auto_training_curriculum_version'] in options): + default = options.index(st.session_state['auto_training_curriculum_version']) + elif 'auto_training_curriculum_version' in st.query_params and st.query_params['auto_training_curriculum_version'] in options: + default = options.index(st.query_params['auto_training_curriculum_version']) + else: + default = 0 + selected_curriculum_version = cols[1].selectbox( + 'Curriculum version', + options=options, + index=default, + key='auto_training_curriculum_version' + ) - # Overriding the selected curriculum if the user selects a different one - if aggrid_curriculum_outputs['selected_rows']: - # Get selected curriculum - selected_row = aggrid_curriculum_outputs['selected_rows'][0] - + options = list(df_curriculums[ + (df_curriculums['curriculum_name'] == selected_curriculum_name) + & (df_curriculums['curriculum_version'] == selected_curriculum_version) + ]['curriculum_schema_version'].unique()) + if ('auto_training_curriculum_schema_version' in st.session_state) and (st.session_state['auto_training_curriculum_schema_version'] in options): + default = options.index(st.session_state['auto_training_curriculum_schema_version']) + elif 'auto_training_curriculum_schema_version' in st.query_params and st.query_params['auto_training_curriculum_schema_version'] in options: + default = options.index(st.query_params['auto_training_curriculum_schema_version']) + else: + default = 0 + selected_curriculum_schema_version = cols[2].selectbox( + 'Curriculum schema version', + options=options, + index=default, + key='auto_training_curriculum_schema_version' + ) - if selected_row: - selected_curriculum = st.session_state.curriculum_manager.get_curriculum( - curriculum_name=selected_row['curriculum_name'], - curriculum_schema_version=selected_row['curriculum_schema_version'], - curriculum_version=selected_row['curriculum_version'], - ) - curriculum = selected_curriculum['curriculum'] + selected_curriculum = st.session_state.curriculum_manager.get_curriculum( + curriculum_name=selected_curriculum_name, + curriculum_schema_version=selected_curriculum_schema_version, + curriculum_version=selected_curriculum_version, + ) + if selected_curriculum is not None: + curriculum = selected_curriculum['curriculum'] # Show diagrams - cols = st.columns([1, 1.5]) + cols = st.columns([1.3, 1.5, 1]) with cols[0]: st.graphviz_chart(curriculum.diagram_rules(render_file_format=''), - use_container_width=True) + use_container_width=True) with cols[1]: st.graphviz_chart(curriculum.diagram_paras(render_file_format=''), use_container_width=True) else: - st.write('Select a curriculum above.') + st.write('load curriculum failed') # Add debug info if chosen_id != "tab_auto_train_curriculum": From 7af6e167b5095799cde1d3ed407cb7c64c772225 Mon Sep 17 00:00:00 2001 From: "houhan@gmail.com" Date: Fri, 23 Feb 2024 01:47:39 +0000 Subject: [PATCH 2/2] highligh selected curriculum; disable selection in the table --- code/Home.py | 26 +++++++++++++++++++++----- code/util/streamlit.py | 6 +++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/code/Home.py b/code/Home.py index fa804d0..8209f51 100644 --- a/code/Home.py +++ b/code/Home.py @@ -642,12 +642,10 @@ def app(): by=['curriculum_schema_version', 'curriculum_name', 'curriculum_version']).reset_index().drop(columns='index') with placeholder: # Show curriculum manager dataframe - st.markdown("#### Available auto training curriculums") - - aggrid_interactive_table_curriculum(df=df_curriculums) - + st.markdown("#### Select auto training curriculums") + # Curriculum drop down selector - cols = st.columns([1, 1, 1, 4]) + cols = st.columns([0.8, 0.5, 0.8, 4]) options = list(df_curriculums['curriculum_name'].unique()) selected_curriculum_name = cols[0].selectbox( 'Curriculum name', @@ -699,6 +697,24 @@ def app(): curriculum_version=selected_curriculum_version, ) + # Get selected curriculum from previous selected or the URL + if 'auto_training_curriculum_name' in st.session_state: + selected_row = {'curriculum_name': st.session_state['auto_training_curriculum_name'], + 'curriculum_schema_version': st.session_state['auto_training_curriculum_schema_version'], + 'curriculum_version': st.session_state['auto_training_curriculum_version']} + matched_curriculum = df_curriculums[(df_curriculums[list(selected_row)] == pd.Series(selected_row)).all(axis=1)] + + if len(matched_curriculum): + pre_selected_rows = matched_curriculum.index.to_list() + else: + selected_row = None # Clear selected row if not found + pre_selected_rows = None + + # Show df_curriculum + aggrid_interactive_table_curriculum(df=df_curriculums, + pre_selected_rows=pre_selected_rows) + + if selected_curriculum is not None: curriculum = selected_curriculum['curriculum'] # Show diagrams diff --git a/code/util/streamlit.py b/code/util/streamlit.py index 63c6c3a..277db5b 100644 --- a/code/util/streamlit.py +++ b/code/util/streamlit.py @@ -112,7 +112,7 @@ def aggrid_interactive_table_curriculum(df: pd.DataFrame, options.configure_side_bar() - options.configure_selection(selection_mode="single", + options.configure_selection(selection_mode=None, pre_selected_rows=pre_selected_rows) selection = AgGrid( @@ -120,9 +120,9 @@ def aggrid_interactive_table_curriculum(df: pd.DataFrame, enable_enterprise_modules=True, gridOptions=options.build(), theme="balham", - update_mode=GridUpdateMode.SELECTION_CHANGED, + update_mode=GridUpdateMode.NO_UPDATE, allow_unsafe_jscode=True, - height=300, + height=200, columns_auto_size_mode=ColumnsAutoSizeMode.FIT_CONTENTS, custom_css=custom_css, )