From 6e95d1b2095682a4540e75d411137649c69a0a44 Mon Sep 17 00:00:00 2001 From: cecy07 Date: Thu, 22 Feb 2024 03:36:56 +0900 Subject: [PATCH] [New Features] New UI changes --- app.py | 110 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 28 deletions(-) diff --git a/app.py b/app.py index 1807848..3b5e224 100644 --- a/app.py +++ b/app.py @@ -89,8 +89,8 @@ def check_login(): ) ) col1, col2 = st.columns(2) - st.session_state.id = col1.text_input(_("ID"), st.session_state.id) - st.session_state.pw = col2.text_input(_("PW"), st.session_state.pw, type="password") + st.session_state.id = col1.text_input(_("ID"), st.session_state.id, key="username") + st.session_state.pw = col2.text_input(_("PW"), st.session_state.pw, type="password", key="password") login_button = st.button(_("Login")) if login_button: KTrains = name_to_class[st.session_state.mode] @@ -128,16 +128,26 @@ def check_login(): # Main layout ktrains = st.session_state.ktrains - # Set with two columns - col1, col2 = st.columns(2) - dep = col1.selectbox( - _("Departure"), stations.station_names(), index=107 if mode == "korail" else 0 - ) - arr = col2.selectbox( - _("Arrival"), stations.station_names(), index=44 if mode == "korail" else 5 - ) - dep = stations.convert_station_name(dep) - arr = stations.convert_station_name(arr) + stations_names = stations.station_names() + stations_names = list(map(str, stations_names)) + if 'dep_index' not in st.session_state or 'arr_index' not in st.session_state: + if mode == "korail": + st.session_state.dep_index = 107 + st.session_state.arr_index = 44 + else: + st.session_state.dep_index = 0 + st.session_state.arr_index = 5 + + col1, swap_col, col2 = st.columns([1, 0.2, 1]) + swap_col.write(" ") + if swap_col.button("⇄"): + # Swap the values in session state + st.session_state.dep_index, st.session_state.arr_index = st.session_state.arr_index, st.session_state.dep_index + dep = stations_names[st.session_state.dep_index] + arr = stations_names[st.session_state.arr_index] + # Use session state for indices + dep = col1.selectbox(_("Departure"), stations_names, index=st.session_state.dep_index) + arr = col2.selectbox(_("Arrival"), stations_names, index=st.session_state.arr_index) col1, col2 = st.columns(2) date = col1.date_input("Date", value=st.session_state.cur_date) @@ -145,11 +155,13 @@ def check_login(): st.session_state.cur_date = date st.session_state.cur_time = time date = date.strftime("%Y%m%d") - time = time.strftime("%H%M%S") + time_selected = time.strftime("%H%M%S") + #time = time.strftime("%H%M%S") + time = "000000" table_stations = Stations(mode, language_sched) if st.button(_("Search")): - trains = ktrains.search_train(dep, arr, date, time, available_only=False) + trains = ktrains.search_train(stations.convert_station_name(dep), stations.convert_station_name(arr), date, time, available_only=False) if trains == []: st.error("No trains found") st.session_state.trains = None @@ -242,18 +254,60 @@ def check_login(): date_formatted = date[:4] + "/" + date[4:6] + "/" + date[6:] st.write(_("Search results for {}:").format(date_formatted)) - grid_return = AgGrid( - df, - gridOptions=grid_options, - fit_columns_on_grid_load=False, - allow_unsafe_jscode=True, - columns_auto_size_mode=2, - enable_enterprise_modules=False, - header_checkbox_selection_filtered_only=True, - theme="streamlit", - use_checkbox=True, - width="200%", - ) + # Split the dataframe into chunks for pagination + if 'current_chunk_index' not in st.session_state: + st.session_state['current_chunk_index'] = 0 + + if 'current_selected_time' not in st.session_state: + st.session_state['current_selected_time'] = "111111" + + # Check if DataFrame is not empty + flag_time = time_selected + if not df.empty: + chunk_size = 10 # Default chunk size + if len(df) < chunk_size: + chunk_size = len(df) + chunks = [df[i:i + chunk_size] for i in range(0, len(df), chunk_size)] + total_chunks = len(chunks) + + #Time changed + if st.session_state['current_selected_time'] != time_selected: + st.session_state['current_selected_time'] = time_selected + for i, chunk in enumerate(chunks): + times_chunks = chunk['Time'].str[:2] + if time_selected[:2] in times_chunks.tolist(): + st.session_state['current_chunk_index'] = i + break + + # Assuming grid_options is defined and AgGrid is imported + grid_return = AgGrid( + chunks[st.session_state['current_chunk_index']], + gridOptions=grid_options, + fit_columns_on_grid_load=False, + allow_unsafe_jscode=True, + columns_auto_size_mode=2, + enable_enterprise_modules=False, + header_checkbox_selection_filtered_only=True, + theme="streamlit", + use_checkbox=True, + width="200%", + ) + + # Navigation Buttons + kol2, kol1, kol3 = st.columns([0.7, 1, 1]) + + # Previous Button + with kol1: + if st.session_state['current_chunk_index'] > 0 and st.button("<- Previous"): + st.session_state['current_chunk_index'] -= 1 + st.experimental_rerun() + + # Next Button + with kol3: + if st.session_state['current_chunk_index'] < total_chunks - 1 and st.button("Next ->"): + st.session_state['current_chunk_index'] += 1 + st.experimental_rerun() + new_df = pd.DataFrame(grid_return["selected_rows"]) @@ -360,9 +414,9 @@ def check_login(): "--email-receivers", email_receivers, "--dep", - dep, + stations.convert_station_name(dep), "--arr", - arr, + stations.convert_station_name(arr), "--date", date, "--time",