Skip to content

Commit

Permalink
captcha fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Arslan-Siraj committed Sep 21, 2023
1 parent ec9c706 commit dd073bb
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 58 deletions.
47 changes: 0 additions & 47 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import streamlit as st
from captcha.image import ImageCaptcha
import random, string

from src.common import *
from src.captcha_ import *
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions pages/0_📁_File_Upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
30 changes: 22 additions & 8 deletions pages/1_⚙️_Analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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()
Expand All @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions pages/2_📊_Result_View.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", [])
Expand Down Expand Up @@ -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)])

Expand Down
51 changes: 50 additions & 1 deletion src/captcha_.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()
_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()

0 comments on commit dd073bb

Please sign in to comment.