Spaces:
Runtime error
Runtime error
File size: 5,572 Bytes
6cb7011 e6720c8 6cb7011 535bd39 6cb7011 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import streamlit as st
import uuid
import boto3
import botocore
import streamlit.components.v1 as components
from streamlit_autorefresh import st_autorefresh
import requests
from all_funcs import *
from create_table import *
AWS_ACCESS_KEY_ID = st.secrets["AWS_ACCESS_KEY"]
AWS_SECRET_ACCESS_KEY = st.secrets["AWS_SECRET_ACCESS_KEY"]
s3 = boto3.client("s3",
region_name='ap-south-1',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
res = boto3.resource("s3",
region_name='ap-south-1',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
st.markdown("""
<style>
#MainMenu{visibility: hidden;}
td.css-57lzw8:nth-of-type(4){}
footer, label.css-zyb2jl, img.css-1jhkrss, button.css-bl767a {visibility: hidden;}
.copy-button{color:red;}
</style>
""", unsafe_allow_html=True)
if "iden" not in st.session_state:
st.session_state["iden"] = None
st.session_state["sesInBucket"] = None
st.session_state['dataInBucket'] = None
st.session_state['linksFinal'] = []
st.session_state['editLinks'] = []
st.session_state['chosen'] = ""
st.session_state["refresh"] = ""
id_place_con = st.sidebar.container()
def from_session():
already_in_body = st.session_state.dataInBucket
sessions_here = already_in_body.split(",")
a = []
indices = [("Comparison "+ str(num)) for num in range(1, len(sessions_here)+1)]
comparison_data_con = st.sidebar.container()
with comparison_data_con:
chosen = st.selectbox("Choose Session:", indices)
a = list(set(sessions_here[indices.index(chosen)].split("\n")))
a.remove("")
if st.session_state["refresh"] == True:
st.session_state.linksFinal = []
if chosen != st.session_state.chosen:
st.session_state.chosen = chosen
st.session_state["a"] = a
st.session_state["linksFinal"] = a
return sessions_here
def main():
#if "hey" not in st.session_state:
count = st_autorefresh(interval=1, limit=2, key="hey")
#st.write(st.session_state)
id_place_con.text("Comparison ID:")
id_place_con.code(st.session_state.iden.replace(".txt", ""))
id_place_con.download_button("Download ID", st.session_state.iden.replace(".txt", ""), file_name="Session ID.txt")
id_place_con.warning("Keep Comparison ID to access and save your comparisons.")
id_place_con.markdown("<hr>", unsafe_allow_html=True)
if st.session_state.sesInBucket==True:
sessions_here = from_session()
else:
sessions_here = []
#st.write(st.session_state.linksFinal)
if len(st.session_state) > 1:
for k in st.session_state:
if st.session_state[k] == True and k.isdigit():
st.session_state["linksFinal"].pop(int(k))
with st.sidebar.form(key='my_form'):
placeholder = st.empty()
s = placeholder.text_input(label='Enter Amazon Product Page URL')
submit = st.form_submit_button(label='Submit')
if submit:
try:
check_paste = requests.get(s)
if s in st.session_state["linksFinal"] or s.find("amazon.in") == -1:
pass
else:
st.session_state["linksFinal"].append(s)
except:
st.error('Not a valid URL')
conf1, refre1 = st.sidebar.columns([1, 1])
confirm = conf1.button("Compare")
refresh = refre1.button("Empty List", key="refresh")
if refresh:
st.session_state.linksFinal = []
if len(st.session_state.linksFinal) == 0:
pass
else:
exp=st.expander("Expand", expanded=True)
with exp:
create_vars(st.columns(len(st.session_state.linksFinal)))
if confirm:
string = create_table(st.session_state.linksFinal)
save_data_in_session(string, st.session_state.sesInBucket, sessions_here)
#count = st_autorefresh(interval=1, limit=2)
if st.session_state.iden != None:
main()
else:
enter_it = st.sidebar.container()
lol2 = st.sidebar.container()
create_it = st.sidebar.container()
with enter_it:
textPlace = st.empty()
produce_error = st.empty()
enter_uni_id = textPlace.text_input("Enter Comparison ID if you have one:")
if enter_uni_id == "":
pass
else:
try:
check_iden = s3.get_object(Bucket="productreviewsdata", Key="sessions/"+enter_uni_id+".txt")
st.session_state.iden = enter_uni_id + ".txt"
st.session_state.sesInBucket = True
st.session_state.dataInBucket = already_in_body = check_iden["Body"].read().decode()
textPlace.empty()
produce_error.empty()
except Exception as e:
produce_error.error("Comparison ID not found!")
with lol2:
or_thing = st.empty()
or_thing.write("OR")
with create_it:
create_it_button = st.empty()
thing = create_it_button.button("Create Comparison ID")
if thing == True:
iden = str(uuid.uuid4())
st.session_state["iden"] = iden + ".txt"
st.session_state.sesInBucket = False
if st.session_state.iden != None:
textPlace.empty()
or_thing.empty()
create_it_button.empty()
produce_error.empty()
main()
|