Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,119 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
-
from oauth2client.service_account import ServiceAccountCredentials
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
st.title("π Skype Contacts: Saved Lists")
|
| 9 |
-
|
| 10 |
CONTACTS_FILE = "contacts.xlsx"
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Load contacts
|
| 15 |
@st.cache_data
|
| 16 |
def load_contacts():
|
| 17 |
df = pd.read_excel(CONTACTS_FILE)
|
| 18 |
-
df
|
| 19 |
-
df.columns = ["display_name", "country"]
|
| 20 |
return df
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
| 28 |
-
creds = ServiceAccountCredentials.from_json_keyfile_name(CREDS_FILE, scope)
|
| 29 |
-
client = gspread.authorize(creds)
|
| 30 |
-
sheet = client.open_by_url("https://docs.google.com/spreadsheets/d/1bYRrOokZ_D85fj_0bbByc6dfaxW50Hj2PqDRG_r25yU")
|
| 31 |
-
return sheet.worksheet(SHEET_NAME)
|
| 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 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
sheet.delete_rows(idx)
|
| 78 |
-
st.success("List deleted. Please refresh.")
|
| 79 |
-
st.stop()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import json
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Constants
|
|
|
|
|
|
|
| 8 |
CONTACTS_FILE = "contacts.xlsx"
|
| 9 |
+
SAVED_LISTS_FILE = "saved_lists.json"
|
| 10 |
+
CHANNEL_MATRIX_FILE = "channel_matrix.xlsx"
|
| 11 |
+
TAGS = [
|
| 12 |
+
"+mini", "+hdy", "+smx", "+pmx", "+cape", "+med", "+atl", "+rsea",
|
| 13 |
+
"+safr", "+pg", "+wci", "+eci", "+seas", "+feast", "+nopac", "+aus", "+aust"
|
| 14 |
+
]
|
| 15 |
|
| 16 |
# Load contacts
|
| 17 |
@st.cache_data
|
| 18 |
def load_contacts():
|
| 19 |
df = pd.read_excel(CONTACTS_FILE)
|
| 20 |
+
df["checkbox"] = False
|
|
|
|
| 21 |
return df
|
| 22 |
|
| 23 |
+
# Save lists locally
|
| 24 |
+
def save_new_list(name, contacts, tags):
|
| 25 |
+
new_entry = {
|
| 26 |
+
"list_name": name,
|
| 27 |
+
"contacts": contacts,
|
| 28 |
+
"tags": tags,
|
| 29 |
+
"timestamp": datetime.now().isoformat()
|
| 30 |
+
}
|
| 31 |
+
if os.path.exists(SAVED_LISTS_FILE):
|
| 32 |
+
with open(SAVED_LISTS_FILE, "r") as f:
|
| 33 |
+
data = json.load(f)
|
| 34 |
+
else:
|
| 35 |
+
data = []
|
| 36 |
|
| 37 |
+
# Overwrite if same name
|
| 38 |
+
data = [d for d in data if d["list_name"] != name]
|
| 39 |
+
data.append(new_entry)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
with open(SAVED_LISTS_FILE, "w") as f:
|
| 42 |
+
json.dump(data, f, indent=2)
|
| 43 |
|
| 44 |
+
# Load lists
|
| 45 |
+
def load_saved_lists():
|
| 46 |
+
if os.path.exists(SAVED_LISTS_FILE):
|
| 47 |
+
with open(SAVED_LISTS_FILE, "r") as f:
|
| 48 |
+
return json.load(f)
|
| 49 |
+
return []
|
| 50 |
|
| 51 |
+
# Delete list
|
| 52 |
+
def delete_list(name):
|
| 53 |
+
data = load_saved_lists()
|
| 54 |
+
data = [d for d in data if d["list_name"] != name]
|
| 55 |
+
with open(SAVED_LISTS_FILE, "w") as f:
|
| 56 |
+
json.dump(data, f, indent=2)
|
| 57 |
|
| 58 |
+
# UI: Contact Filter Tab
|
| 59 |
+
def contact_filter_ui():
|
| 60 |
+
st.markdown("## π Skype Contacts: Saved Lists")
|
| 61 |
+
|
| 62 |
+
df = load_contacts()
|
| 63 |
+
search = st.text_input("π Search by Display Name")
|
| 64 |
+
if search:
|
| 65 |
+
df = df[df["display_name"].str.contains(search, case=False)]
|
| 66 |
+
|
| 67 |
+
tag_filters = st.multiselect("π Filter by Tags", TAGS)
|
| 68 |
+
if tag_filters:
|
| 69 |
+
tag_mask = df["display_name"].apply(lambda x: any(tag in x for tag in tag_filters))
|
| 70 |
+
df = df[tag_mask]
|
| 71 |
+
|
| 72 |
+
# Checkbox table
|
| 73 |
+
st.markdown("### β
Tick Contacts to Save in a List")
|
| 74 |
+
edited_df = st.data_editor(df[["display_name", "country", "checkbox"]],
|
| 75 |
+
num_rows="dynamic", use_container_width=True)
|
| 76 |
|
| 77 |
+
# Save List
|
| 78 |
+
ticked = edited_df[edited_df["checkbox"] == True]
|
| 79 |
+
list_name = st.text_input("πΎ Name this list")
|
| 80 |
+
if st.button("Save List"):
|
| 81 |
+
if list_name and not ticked.empty:
|
| 82 |
+
save_new_list(list_name, ticked["display_name"].tolist(), ", ".join(tag_filters))
|
| 83 |
+
st.success(f"List '{list_name}' saved.")
|
| 84 |
+
else:
|
| 85 |
+
st.warning("List name and at least one contact must be selected.")
|
| 86 |
|
| 87 |
+
# Show saved lists
|
| 88 |
+
st.markdown("---\n### π Manage Saved Lists")
|
| 89 |
+
saved_lists = load_saved_lists()
|
| 90 |
+
if not saved_lists:
|
| 91 |
+
st.info("No saved lists yet.")
|
| 92 |
+
for entry in saved_lists:
|
| 93 |
+
with st.expander(f"π {entry['list_name']} ({len(entry['contacts'])} contacts)"):
|
| 94 |
+
st.write("Tags:", entry["tags"])
|
| 95 |
+
st.write("Saved on:", entry["timestamp"])
|
| 96 |
+
st.write("Contacts:", entry["contacts"])
|
| 97 |
+
if st.button(f"ποΈ Delete '{entry['list_name']}'", key=entry["list_name"]):
|
| 98 |
+
delete_list(entry["list_name"])
|
| 99 |
+
st.rerun()
|
| 100 |
|
| 101 |
+
# UI: Channel Matrix Tab
|
| 102 |
+
def channel_matrix_ui():
|
| 103 |
+
st.markdown("## π Channel Matrix Checker")
|
| 104 |
+
df = pd.read_excel(CHANNEL_MATRIX_FILE)
|
| 105 |
+
charterers = list(df.columns[1:])
|
| 106 |
+
selected_charterer = st.selectbox("Select a Charterer", charterers)
|
| 107 |
+
yes_df = df[df[selected_charterer].str.lower() == "yes"]
|
| 108 |
+
no_df = df[df[selected_charterer].str.lower() == "no"]
|
| 109 |
+
st.markdown(f"### β
Operators who work with us for **{selected_charterer}**'s cargoes:")
|
| 110 |
+
st.dataframe(yes_df[["Operator"]], use_container_width=True)
|
| 111 |
+
st.markdown(f"### β Operators who won't work with us for **{selected_charterer}**'s cargoes:")
|
| 112 |
+
st.dataframe(no_df[["Operator"]], use_container_width=True)
|
| 113 |
|
| 114 |
+
# Tabs
|
| 115 |
+
tab1, tab2 = st.tabs(["π Contact Filter", "π Channel Matrix"])
|
| 116 |
+
with tab1:
|
| 117 |
+
contact_filter_ui()
|
| 118 |
+
with tab2:
|
| 119 |
+
channel_matrix_ui()
|
|
|
|
|
|
|
|
|