|
|
|
|
@ -31,7 +31,7 @@ def reset_q(): |
|
|
|
|
def download(df): |
|
|
|
|
|
|
|
|
|
st.download_button( |
|
|
|
|
"CSV", |
|
|
|
|
"Ladda ner alla meddelanden som CSV", |
|
|
|
|
df.to_csv(index=False, sep=';').encode('utf-8'), |
|
|
|
|
"file.csv", |
|
|
|
|
"text/csv", |
|
|
|
|
@ -133,6 +133,7 @@ def search_messages(search_for, engine, user=False): |
|
|
|
|
if user: # Search for all messages from/to a single user. |
|
|
|
|
select_columns = 'body, "to" as m2, "from" as m1, senddate_str' |
|
|
|
|
sql = f'select {select_columns} from messages where "to" == "{search_for}" or "from" == "{search_for}"' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: # Search for keywords. |
|
|
|
|
sql = create_sql_query(define_search_terms(search_for), 'messages') |
|
|
|
|
@ -194,56 +195,60 @@ def search_user(search_for, engine): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(): |
|
|
|
|
|
|
|
|
|
# Say hi. |
|
|
|
|
st.markdown(f'<h4 style="color:#ff748c";>Hej {name}!</h4>', unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
# Define connection to sqlite database. |
|
|
|
|
engine = create_engine('sqlite:///db.sqlite') |
|
|
|
|
|
|
|
|
|
#global params |
|
|
|
|
# Create query_params class and set empty parameters. |
|
|
|
|
params = Params(st.experimental_get_query_params()) |
|
|
|
|
|
|
|
|
|
# Let user chose what categorie to search in. |
|
|
|
|
categories = ['Meddelanden', 'Användare'] |
|
|
|
|
if params.category != '': |
|
|
|
|
index_category = categories.index(params.category[0]) |
|
|
|
|
else: |
|
|
|
|
index_category = 0 |
|
|
|
|
search_category = st.selectbox('Vad vill du söka efter?', categories, index_category, on_change=reset_q) |
|
|
|
|
search_category = st.selectbox('Vill du söka efter meddelanden eller användare?', categories, index_category, on_change=reset_q) |
|
|
|
|
params.update('category', search_category) |
|
|
|
|
|
|
|
|
|
# Let user chose what words to search for. |
|
|
|
|
if params.q != '': |
|
|
|
|
placeholder=params.q[0] |
|
|
|
|
else: |
|
|
|
|
placeholder = 'Skriv här' |
|
|
|
|
|
|
|
|
|
search_help = 'Du kan använda asterix (*), minus(-), citattecken ("") och OR.' |
|
|
|
|
search_for = st.text_input('Vad vill du söka efter?', placeholder=placeholder, help=search_help) |
|
|
|
|
# Set text to be shown above search field. |
|
|
|
|
if search_category == 'Meddelanden': |
|
|
|
|
search_text = 'Vilka ord vill du söka efter?' |
|
|
|
|
elif search_category == 'Användare': |
|
|
|
|
search_text = 'Skriv email-adress eller användarnamn.' |
|
|
|
|
|
|
|
|
|
helpt_text = 'Du kan använda asterix (*), minus(-), citattecken ("") och OR.' |
|
|
|
|
search_for = st.text_input(search_text, placeholder=placeholder, help=helpt_text).lower() |
|
|
|
|
|
|
|
|
|
# Set search input from params if nothing else is input. |
|
|
|
|
# This is the case when the user is coming from an url with q param. |
|
|
|
|
if search_for == '' and params.q != '': |
|
|
|
|
search_for = params.q[0] |
|
|
|
|
|
|
|
|
|
# Start the search. |
|
|
|
|
if search_for != '': |
|
|
|
|
params.update('q', search_for) |
|
|
|
|
search_for.replace('å', '?').replace('ä', '?').replace('ö', '?') |
|
|
|
|
|
|
|
|
|
#* Sök meddelanden |
|
|
|
|
# Replace å, ä, ö with ?. |
|
|
|
|
search_for = search_for.replace('å', '?').replace('ä', '?').replace('ö', '?') |
|
|
|
|
|
|
|
|
|
#* Search message. |
|
|
|
|
if search_category == 'Meddelanden': |
|
|
|
|
search_messages(search_for, engine) |
|
|
|
|
|
|
|
|
|
# m1, m2, body, senddate_str = st.columns([1, 1, 5, 1]) |
|
|
|
|
# for row in data['df'].iterrows(): |
|
|
|
|
# row = row[1] |
|
|
|
|
# with m1: |
|
|
|
|
# row['m1'] |
|
|
|
|
# with m2: |
|
|
|
|
# row['m2'] |
|
|
|
|
# with body: |
|
|
|
|
# row['body'] |
|
|
|
|
# with senddate_str: |
|
|
|
|
# row['senddate_str'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#* Sök användare |
|
|
|
|
#* Seach user. |
|
|
|
|
elif search_category == 'Användare': |
|
|
|
|
search_user(search_for, engine) |
|
|
|
|
|
|
|
|
|
#main() |
|
|
|
|
with open('credentials.yaml') as file: |
|
|
|
|
config = yaml.load(file, Loader=SafeLoader) |
|
|
|
|
|
|
|
|
|
@ -258,7 +263,11 @@ authenticator = stauth.Authenticate( |
|
|
|
|
name, authentication_status, username = authenticator.login('Login', 'main') |
|
|
|
|
|
|
|
|
|
if authentication_status: |
|
|
|
|
main() |
|
|
|
|
try: |
|
|
|
|
main() |
|
|
|
|
except Exception as e: |
|
|
|
|
print(e) |
|
|
|
|
st.write('Något blev fel :(') |
|
|
|
|
|
|
|
|
|
elif authentication_status is False: |
|
|
|
|
st.error('Username/password is incorrect') |
|
|
|
|
|