From dd073bbc7092aa0267607eff2451d5ac8961c4a4 Mon Sep 17 00:00:00 2001 From: Arslan Date: Thu, 21 Sep 2023 12:52:05 +0200 Subject: [PATCH] captcha fixed --- app.py | 47 ----------------- "pages/0_\360\237\223\201_File_Upload.py" | 14 +++++ "pages/1_\342\232\231\357\270\217_Analyze.py" | 30 ++++++++--- "pages/2_\360\237\223\212_Result_View.py" | 16 +++++- src/captcha_.py | 51 ++++++++++++++++++- 5 files changed, 100 insertions(+), 58 deletions(-) diff --git a/app.py b/app.py index d2d55b09..337d6cbb 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,4 @@ import streamlit as st -from captcha.image import ImageCaptcha -import random, string from src.common import * from src.captcha_ import * @@ -28,51 +26,6 @@ def main(): main() else: - # define the costant - length_captcha = 5 - width = 400 - height = 180 - - # define the function for the captcha control - def captcha_control(): - #control if the captcha is correct - if 'controllo' not in st.session_state or st.session_state['controllo'] == False: - st.title("Makesure you are not a robot🤖") - - # define the session state for control if the captcha is correct - st.session_state['controllo'] = False - col1, col2 = st.columns(2) - - # define the session state for the captcha text because it doesn't change during refreshes - if 'Captcha' not in st.session_state: - st.session_state['Captcha'] = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length_captcha)) - - #setup the captcha widget - image = ImageCaptcha(width=width, height=height) - data = image.generate(st.session_state['Captcha']) - col1.image(data) - capta2_text = col2.text_area('Enter captcha text', height=10, placeholder="captcha text") - - col3, col4 = st.columns(2) - with col4: - if st.button("Verify the code"): - capta2_text = capta2_text.replace(" ", "") - # if the captcha is correct, the controllo session state is set to True - if st.session_state['Captcha'].lower() == capta2_text.lower().strip(): - del st.session_state['Captcha'] - col1.empty() - col2.empty() - st.session_state['controllo'] = True - st.experimental_rerun() - else: - # if the captcha is wrong, the controllo session state is set to False and the captcha is regenerated - st.error("🚨 Captcha is wrong") - del st.session_state['Captcha'] - del st.session_state['controllo'] - st.experimental_rerun() - else: - #wait for the button click - st.stop() # WORK LIKE MULTIPAGE APP if 'controllo' not in st.session_state or st.session_state['controllo'] == False: diff --git "a/pages/0_\360\237\223\201_File_Upload.py" "b/pages/0_\360\237\223\201_File_Upload.py" index 475468ad..e6bdd502 100755 --- "a/pages/0_\360\237\223\201_File_Upload.py" +++ "b/pages/0_\360\237\223\201_File_Upload.py" @@ -3,9 +3,23 @@ import pandas as pd from src.common import * from src.fileupload import * +from src.captcha_ import * params = page_setup() +#if local no need captcha +if st.session_state.location == "local": + params["controllo"] = True + st.session_state["controllo"] = True + +#if controllo is false means not captcha applied +if 'controllo' not in st.session_state or params["controllo"] == False: + #apply captcha + captcha_control() + + +### main content of page + # Make sure "selected-mzML-files" is in session state if "selected-mzML-files" not in st.session_state: st.session_state["selected-mzML-files"] = params["selected-mzML-files"] diff --git "a/pages/1_\342\232\231\357\270\217_Analyze.py" "b/pages/1_\342\232\231\357\270\217_Analyze.py" index 5ce3221d..83da6f2f 100755 --- "a/pages/1_\342\232\231\357\270\217_Analyze.py" +++ "b/pages/1_\342\232\231\357\270\217_Analyze.py" @@ -8,9 +8,23 @@ from src.result_files import * from src.ini2dec import * import threading +from src.captcha_ import * params = page_setup() +#if local no need captcha +if st.session_state.location == "local": + params["controllo"] = True + st.session_state["controllo"] = True + +#if controllo is false means not captcha applied +if 'controllo' not in st.session_state or params["controllo"] == False: + #apply captcha + captcha_control() + + +### main content of page + #title of page st.title("⚙️ Run Analysis") @@ -20,7 +34,7 @@ #make sure "selected-fasta-files" is in session state if "selected-fasta-files" not in st.session_state: - st.session_state["selected-fasta-files"] = params.get("selected-fasta-files", []) + st.session_state["selected-fasta-files"] = params.get("selected-fasta-files", []) #make sure mzML example files in current session state load_example_mzML_files() @@ -46,7 +60,7 @@ selected_fasta_file = st.selectbox( "choose fasta file", [f.name for f in Path(st.session_state.workspace, - "fasta-files").iterdir()], + "fasta-files").iterdir()], help="If file not here, please upload at File Upload" ) @@ -91,11 +105,11 @@ #take NuXL config dictionary # (will give every section as 1 entry: # entry = { - #"name": node_name, - #"default": node_default, - #"description": node_desc, - #"restrictions": restrictions_list - # }) + #"name": node_name, + #"default": node_default, + #"description": node_desc, + #"restrictions": restrictions_list + # }) NuXL_config=ini2dict(config_path, sections) #take all variables settings from config dictionary/ take all user configuration @@ -184,7 +198,7 @@ def run_subprocess(args, variables, result_dict): args: command with args variables: variable if any result_dict: contain success (success flag) and log (capture long log) - should contain result_dict["success"], result_dict["log"] + should contain result_dict["success"], result_dict["log"] Returns: None diff --git "a/pages/2_\360\237\223\212_Result_View.py" "b/pages/2_\360\237\223\212_Result_View.py" index 5014884a..2e46cfa4 100755 --- "a/pages/2_\360\237\223\212_Result_View.py" +++ "b/pages/2_\360\237\223\212_Result_View.py" @@ -4,10 +4,22 @@ import plotly.graph_objects as go from src.view import plot_ms2_spectrum from st_aggrid import GridOptionsBuilder, AgGrid, GridUpdateMode, ColumnsAutoSizeMode - +from src.captcha_ import * params = page_setup() +#if local no need captcha +if st.session_state.location == "local": + params["controllo"] = True + st.session_state["controllo"] = True + +#if controllo is false means not captcha applied +if 'controllo' not in st.session_state or params["controllo"] == False: + #apply captcha + captcha_control() + +### main content of page + # Make sure "selected-result-files" is in session state if "selected-result-files" not in st.session_state: st.session_state["selected-result-files"] = params.get("selected-result-files", []) @@ -53,7 +65,7 @@ if CSM_['NuXL:NA'].str.contains('none').any(): st.warning("nonXL CSMs found") else: - + # provide dataframe gb = GridOptionsBuilder.from_dataframe(CSM_[list(CSM_.columns.values)]) diff --git a/src/captcha_.py b/src/captcha_.py index ddfec4b4..90f29062 100644 --- a/src/captcha_.py +++ b/src/captcha_.py @@ -1,5 +1,7 @@ from pathlib import Path import streamlit as st +from captcha.image import ImageCaptcha +import random, string from streamlit.source_util import ( page_icon_and_name, calc_md5, @@ -61,4 +63,51 @@ def add_page(main_script_path_str, page_name): "script_path": script_path_str, } #refresh the page config - _on_pages_changed.send() \ No newline at end of file + _on_pages_changed.send() + + +# define the costant +length_captcha = 5 +width = 400 +height = 180 + +# define the function for the captcha control +def captcha_control(): + #control if the captcha is correct + if 'controllo' not in st.session_state or st.session_state['controllo'] == False: + st.title("Makesure you are not a robot🤖") + + # define the session state for control if the captcha is correct + st.session_state['controllo'] = False + col1, col2 = st.columns(2) + + # define the session state for the captcha text because it doesn't change during refreshes + if 'Captcha' not in st.session_state: + st.session_state['Captcha'] = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length_captcha)) + + #setup the captcha widget + image = ImageCaptcha(width=width, height=height) + data = image.generate(st.session_state['Captcha']) + col1.image(data) + capta2_text = col2.text_area('Enter captcha text', height=10, placeholder="captcha text") + + col3, col4 = st.columns(2) + with col4: + if st.button("Verify the code"): + capta2_text = capta2_text.replace(" ", "") + # if the captcha is correct, the controllo session state is set to True + if st.session_state['Captcha'].lower() == capta2_text.lower().strip(): + del st.session_state['Captcha'] + col1.empty() + col2.empty() + st.session_state['controllo'] = True + st.experimental_rerun() + else: + # if the captcha is wrong, the controllo session state is set to False and the captcha is regenerated + st.error("🚨 Captcha is wrong") + del st.session_state['Captcha'] + del st.session_state['controllo'] + st.experimental_rerun() + else: + #wait for the button click + st.stop() \ No newline at end of file