Skip to content

Commit

Permalink
make tool selection local
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mdavid-m committed Jun 19, 2024
1 parent 650e72b commit 60bf9d4
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 79 deletions.
91 changes: 91 additions & 0 deletions .streamlit/pages.toml
Original file line number Diff line number Diff line change
@@ -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 = "👀"
100 changes: 31 additions & 69 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,71 +23,34 @@
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


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.')
Expand All @@ -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():
Expand All @@ -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()
33 changes: 23 additions & 10 deletions src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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("""
<style>
Expand All @@ -114,10 +125,10 @@ def page_setup(page: str = "", help_text: str = "") -> dict[str, Any]:
# Check location
if "local" in sys.argv:
st.session_state.location = "local"
st.session_state.controlo = True
st.session_state.controllo = True
else:
st.session_state.location = "online"
st.session_state.controlo = False
st.session_state.controllo = False
# if we run the packaged windows version, we start within the Python directory -> need to change working directory to ..\streamlit-template
if "windows" in sys.argv:
os.chdir("../flashtaggerviewer")
Expand All @@ -129,7 +140,9 @@ def page_setup(page: str = "", help_text: str = "") -> dict[str, Any]:
st.session_state.workspace = Path(workspaces_dir, "default")

# If run in hosted mode, show captcha as long as it has not been solved
if not st.session_state["controlo"]:
if not st.session_state["controllo"]:
# Hide all pages
hide_pages(ALL_SECTIONS)
# Apply captcha by calling the captcha_control function
captcha_control()

Expand Down

0 comments on commit 60bf9d4

Please sign in to comment.