From 8f06ed09a078ab3dce7582e03cbbd5adbb7ecd85 Mon Sep 17 00:00:00 2001 From: Lasse Server Date: Fri, 7 Jul 2023 19:20:57 +0200 Subject: [PATCH] first commit --- app.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ main.py | 50 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 app.py create mode 100644 main.py create mode 100644 requirements.txt diff --git a/app.py b/app.py new file mode 100644 index 0000000..401db6b --- /dev/null +++ b/app.py @@ -0,0 +1,53 @@ +import streamlit as st +import streamlit_authenticator as stauth +import yaml +from yaml.loader import SafeLoader +import main + +with open('credentials.yaml') as file: + config = yaml.load(file, Loader=SafeLoader) + +authenticator = stauth.Authenticate( + config['credentials'], + config['cookie']['name'], + config['cookie']['key'], + config['cookie']['expiry_days'], + config['preauthorized'] +) + +name, authentication_status, username = authenticator.login('Logga in', 'main') + +if authentication_status: + user_input = str(st.text_input('Skriv in telefonnummer eller Facebook-ID', )).strip() + if user_input != '': + s = [] + for i in user_input: + if i.isnumeric(): + s.append(str(i)) + s = ''.join(s) + + if s[0] == '0': + s = '46' + s[1:] + + # Search for info. + r = main.search(s) + + if r != None: + if r['column_find'] == 'ID': + id = r['find'] + r['find'] = f'{id}' + st.write(f"{r['name']}, {r['column_find']}: {r['find']}", unsafe_allow_html=True) + + with st.expander('Mer information'): + st.dataframe(r['df'], use_container_width=True) + else: + st.text('Not found.') + st.markdown(':red[Vid frågor mejla lasse@edfast.se]') + +elif authentication_status is False: + st.error('Fel användarnamn/lösenord') + +elif authentication_status is None: + st.warning('Skriv in användarnamn och lösenord') + + diff --git a/main.py b/main.py new file mode 100644 index 0000000..368dee1 --- /dev/null +++ b/main.py @@ -0,0 +1,50 @@ +import pandas as pd +from sqlalchemy import create_engine + +def search(s): + # Create the database engine and session + engine = create_engine('sqlite:///fb_sweden.db') + + if any([all([s[:2]=='07', len(s)==10]), all([s[:2]=='46', len(s)==11])]): + column_search = 'phone' + column_find = 'id' + else: + column_search = 'id' + column_find = 'phone' + + # Construct SQL query. + sql = f'select * from sweden where {column_search} = {s}' + + # Make a df from query. + df = pd.read_sql(sql, engine) + + if len(df) > 0: + # Format data returned. + name = f"{df['first_name'][0]} {df['last_name'][0]}" + r = str(df[column_find][0]) + if column_find == 'phone': + r = f'0{r[2:4]} - {r[4:7]} {r[7:9]} {r[9:]}' + column_find = 'telefon' + column_search = 'ID' + else: + column_find = 'ID' + column_search = 'Telefon' + + df = pd.DataFrame(df.transpose()) + df.columns = [s] + df.rename({'first_name': 'Förnamn', + 'last_name':'Efternamn', + 'phone':'Telefon', 'email':'Epost', + 'sex': 'Kön', + 'city': 'Ort', + 'relation_status':'Relation', + 'work': 'Arbetsplats', + 'place': 'Adress', + 'year': 'År', + 'born': 'Född', + 'id': 'ID' + }, axis=0, inplace=True) + df.dropna(inplace=True) + df = df.drop(column_search) + + return ({'find': r, 'column_find': column_find, 'name': name, 'df':df}) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d8bf3d9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,57 @@ +altair==5.0.1 +attrs==23.1.0 +backports.zoneinfo==0.2.1 +bcrypt==4.0.1 +blinker==1.6.2 +cachetools==5.3.1 +certifi==2023.5.7 +charset-normalizer==3.1.0 +click==8.1.4 +decorator==5.1.1 +extra-streamlit-components==0.1.56 +gitdb==4.0.10 +GitPython==3.1.31 +greenlet==2.0.2 +idna==3.4 +importlib-metadata==6.7.0 +importlib-resources==5.12.0 +Jinja2==3.1.2 +jsonschema==4.18.0 +jsonschema-specifications==2023.6.1 +markdown-it-py==3.0.0 +MarkupSafe==2.1.3 +mdurl==0.1.2 +numpy==1.24.4 +packaging==23.1 +pandas==2.0.3 +Pillow==9.5.0 +pkgutil_resolve_name==1.3.10 +protobuf==4.23.4 +pyarrow==12.0.1 +pydeck==0.8.1b0 +Pygments==2.15.1 +PyJWT==2.7.0 +Pympler==1.0.1 +python-dateutil==2.8.2 +pytz==2023.3 +pytz-deprecation-shim==0.1.0.post0 +PyYAML==6.0 +referencing==0.29.1 +requests==2.31.0 +rich==13.4.2 +rpds-py==0.8.8 +six==1.16.0 +smmap==5.0.0 +SQLAlchemy==2.0.18 +streamlit==1.24.0 +streamlit-authenticator==0.2.2 +tenacity==8.2.2 +toml==0.10.2 +toolz==0.12.0 +tornado==6.3.2 +typing_extensions==4.7.1 +tzdata==2023.3 +tzlocal==4.3.1 +urllib3==2.0.3 +validators==0.20.0 +zipp==3.15.0