From 60bf9d494895bfa57d2fcfdb9fb9423a3464192f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20David=20M=C3=BCller?= Date: Wed, 19 Jun 2024 09:59:50 +0200 Subject: [PATCH] make tool selection local --- .streamlit/pages.toml | 91 ++++++++++++++++++++++++++++++++++++++ app.py | 100 +++++++++++++----------------------------- src/common.py | 33 +++++++++----- 3 files changed, 145 insertions(+), 79 deletions(-) create mode 100644 .streamlit/pages.toml diff --git a/.streamlit/pages.toml b/.streamlit/pages.toml new file mode 100644 index 0000000..0b96e57 --- /dev/null +++ b/.streamlit/pages.toml @@ -0,0 +1,91 @@ +[[pages]] +name = "FLASHViewer" +path = "app.py" +icon = "๐Ÿ " + +[[pages]] +name = "FLASHDeconv" +is_section = true +icon = "โšก๏ธ" + +[[pages]] +path = "pages/FLASHDeconvWorkflow.py" +name = "Workflow" +icon = "โš™๏ธ" + +[[pages]] +path = "pages/FileUpload.py" +name = "File Upload" +icon = "๐Ÿ“" + +[[pages]] +path = "pages/SequenceInput.py" +name = "Sequence Input" +icon = "๐Ÿงต" + +[[pages]] +path = "pages/LayoutManager.py" +name = "Layout Manager" +icon = "๐Ÿ“๏ธ" + +[[pages]] +path = "pages/FLASHDeconvViewer.py" +name = "Viewer" +icon = "๐Ÿ‘€" + +[[pages]] +path = "pages/FLASHDeconvDownload.py" +name = "Download" +icon = "โฌ‡๏ธ" + +[[pages]] +path = "pages/FLASHFDR.py" +name = "ECDF Plot" +icon = "๐Ÿ“ˆ" + +[[pages]] +name = "FLASHTnT" +is_section = true +icon = "๐Ÿ’ฅ" + + +[[pages]] +path = "pages/FLASHTaggerWorkflow.py" +name = "Workflow" +icon = "โš™๏ธ" + +[[pages]] +path = "pages/FileUploadTagger.py" +name = "File Upload" +icon = "๐Ÿ“" + +[[pages]] +path = "pages/LayoutManagerTagger.py" +name = "Layout Manager" +icon = "๐Ÿ“๏ธ" + +[[pages]] +path = "pages/FLASHTaggerViewer.py" +name = "Viewer" +icon = "๐Ÿ‘€" + +[[pages]] +path = "pages/FLASHTaggerDownload.py" +name = "Download" +icon = "โฌ‡๏ธ" + +[[pages]] +name = "FLASHQuant" +is_section = true +icon = "๐Ÿ“Š" + + +[[pages]] +path = "pages/FileUpload_FLASHQuant.py" +name = "File Upload" +icon = "๐Ÿ“" + +[[pages]] +path = "pages/FLASHQuantViewer.py" +name = "Viewer" +icon = "๐Ÿ‘€" diff --git a/app.py b/app.py index 4da6d35..91d7a34 100644 --- a/app.py +++ b/app.py @@ -23,59 +23,21 @@ from pathlib import Path from src.captcha_ import captcha_control from src.common import page_setup, save_params -from st_pages import Page, show_pages -params = page_setup(page="main") -def flashdeconvPages(): - show_pages([ - Page("app.py", "FLASHViewer", "๐Ÿ "), - Page("pages/FLASHDeconvWorkflow.py", "Workflow", "โš™๏ธ"), - Page("pages/FileUpload.py", "File Upload", "๐Ÿ“"), - Page("pages/SequenceInput.py", "Sequence Input", "๐Ÿงต"), - Page("pages/LayoutManager.py", "Layout Manager", "๐Ÿ“๏ธ"), - Page("pages/FLASHDeconvViewer.py", "Viewer", "๐Ÿ‘€"), - Page("pages/FLASHDeconvDownload.py", "Download", "โฌ‡๏ธ"), - Page("pages/FLASHFDR.py", "ECDF Plot", "๐Ÿ“ˆ"), - ]) - - -def flashtagPages(): - show_pages([ - Page("app.py", "FLASHViewer", "๐Ÿ "), - Page("pages/FLASHTaggerWorkflow.py", "Workflow", "โš™๏ธ"), - Page("pages/FileUploadTagger.py", "File Upload", "๐Ÿ“"), - Page("pages/LayoutManagerTagger.py", "Layout Manager", "๐Ÿ“๏ธ"), - Page("pages/FLASHTaggerViewer.py", "Viewer", "๐Ÿ‘€"), - Page("pages/FLASHTaggerDownload.py", "Download", "โฌ‡๏ธ"), - ]) - - -def flashquantPages(): - show_pages([ - Page("app.py", "FLASHViewer", "๐Ÿ "), - Page("pages/FileUpload_FLASHQuant.py", "File Upload", "๐Ÿ“"), - Page("pages/FLASHQuantViewer.py", "Viewer", "๐Ÿ‘€"), - ]) - - -page_names_to_funcs = { - "FLASHTagger": flashtagPages, - "FLASHDeconv": flashdeconvPages, - "FLASHQuant": flashquantPages, -} +def onToolChange(): + # Check if the tool has been set before + if 'changed_tool_name' not in st.session_state: + return + # Save parameter - changed_tool_name is bound to the checkbox and + # will be deleted after rerun + st.session_state.current_tool_name = st.session_state.changed_tool_name -def onToolChange(): - if 'changed_tool_name' in st.session_state: - match st.session_state.changed_tool_name: - case 'FLASHDeconv': - st.session_state['tool_index'] = 0 - case 'FLASHTagger': - st.session_state['tool_index'] = 1 - case 'FLASHQuant': - st.session_state['tool_index'] = 2 + # Only rerender the page if the sidebar has changed + if ('changed_tool_name' in st.session_state) and (st.session_state.prev_tool_name != st.session_state.current_tool_name): + st.session_state.prev_tool_name = st.session_state.current_tool_name st.rerun() # reload the page to sync the change @@ -83,11 +45,12 @@ def main(): """ Display main page content. """ - # sidebar to toggle between tools - if 'tool_index' not in st.session_state: - page_names_to_funcs['FLASHDeconv']() - st.session_state['tool_index'] = 0 + if 'current_tool_name' not in st.session_state: + st.session_state.changed_tool_name = 'FLASHDeconv' + st.session_state.current_tool_name = 'FLASHDeconv' + st.session_state.prev_tool_name = 'FLASHDeconv' + st.rerun() # main content st.markdown('#### FLASHViewer visualizes outputs from FLASH\* tools.') @@ -102,9 +65,8 @@ def main(): """) # when entered into other page, key is resetting (emptied) - thus set the value with index - st.selectbox("Choose a tool", ['FLASHDeconv', 'FLASHTagger', 'FLASHQuant'], index=st.session_state.tool_index, + st.selectbox("Choose a tool", ['FLASHDeconv', 'FLASHTnT', 'FLASHQuant'], index=0, on_change=onToolChange(), key='changed_tool_name') - page_names_to_funcs[st.session_state.changed_tool_name]() if Path("OpenMS-App.zip").exists(): @@ -125,22 +87,22 @@ def main(): save_params(params) -# Check if the script is run in local mode (e.g., "streamlit run app.py local") -if "local" in sys.argv: - # In local mode, run the main function without applying captcha - main() -# If not in local mode, assume it's hosted/online mode -else: - show_pages([ - Page("app.py", "FLASHViewer", "๐Ÿ "), - ]) +if __name__ == '__main__': - # WORK LIKE MULTIPAGE APP - if "controllo" not in st.session_state or st.session_state["controllo"] is False: - # Apply captcha control to verify the user - captcha_control() + params = page_setup(page="main") - else: - # Run the main function + # Check if the script is run in local mode (e.g., "streamlit run app.py local") + if "local" in sys.argv: + + # In local mode, run the main function without applying captcha main() + + # If not in local mode, assume it's hosted/online mode + else: + if ("controllo" not in st.session_state) or (st.session_state["controllo"] is False): + # Apply captcha control to verify the user + captcha_control() + else: + # Run the main function + main() diff --git a/src/common.py b/src/common.py index ce9a484..43778d2 100644 --- a/src/common.py +++ b/src/common.py @@ -6,12 +6,14 @@ from typing import Any from pathlib import Path from src.captcha_ import captcha_control +from st_pages import add_page_title, hide_pages, show_pages_from_config import streamlit as st import pandas as pd # set these variables according to your project APP_NAME = "FLASHViewer" +ALL_SECTIONS = [APP_NAME, "FLASHDeconv", "FLASHTnT", "FLASHQuant"] def load_params(default: bool = False) -> dict[str, Any]: @@ -89,13 +91,22 @@ def page_setup(page: str = "", help_text: str = "") -> dict[str, Any]: dict[str, Any]: A dictionary containing the parameters loaded from the parameter file. """ # Set Streamlit page configurations - st.set_page_config( - page_title=APP_NAME, - page_icon="assets/OpenMS.png", - layout="wide", - initial_sidebar_state="auto", - menu_items=None - ) + configuration = { + "page_title" : APP_NAME, + "page_icon" : "assets/OpenMS.png", + "layout" : "wide", + "initial_sidebar_state" : "auto", + "menu_items" : None + } + + # Setup sidebar + if 'current_tool_name' in st.session_state: + hide_pages(list(set(ALL_SECTIONS) - {APP_NAME} - {st.session_state.current_tool_name})) + add_page_title(**configuration) + + else: + show_pages_from_config() + add_page_title(**configuration) st.markdown("""