siddhartharyaai commited on
Commit
6417b73
Β·
verified Β·
1 Parent(s): 8b37015

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -58
app.py CHANGED
@@ -1,79 +1,119 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import gspread
4
- from oauth2client.service_account import ServiceAccountCredentials
5
  from datetime import datetime
 
6
 
7
- st.set_page_config(page_title="Skype Contact Manager", layout="wide")
8
- st.title("πŸ“‡ Skype Contacts: Saved Lists")
9
-
10
  CONTACTS_FILE = "contacts.xlsx"
11
- CREDS_FILE = "saved_lists_creds.json"
12
- SHEET_NAME = "saved_lists"
 
 
 
 
13
 
14
  # Load contacts
15
  @st.cache_data
16
  def load_contacts():
17
  df = pd.read_excel(CONTACTS_FILE)
18
- df = df[["display_name", "profile.locations[0].country"]].copy()
19
- df.columns = ["display_name", "country"]
20
  return df
21
 
22
- contacts_df = load_contacts()
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # Google Sheets setup
25
- @st.cache_resource
26
- def get_sheet():
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
- sheet = get_sheet()
 
34
 
35
- # Ticking contacts
36
- st.markdown("### βœ… Tick Contacts to Save in a List")
37
- selected_contacts = st.multiselect("Select contacts", contacts_df["display_name"].tolist())
 
 
 
38
 
39
- list_name = st.text_input("πŸ“ Name this list")
40
- tags_input = st.text_input("🏷 Optional tags (comma-separated)")
 
 
 
 
41
 
42
- if st.button("πŸ’Ύ Save List to Google Sheet"):
43
- if not list_name:
44
- st.warning("Please enter a list name.")
45
- elif not selected_contacts:
46
- st.warning("Please select at least one contact.")
47
- else:
48
- contact_csv = ",".join(selected_contacts)
49
- tags = tags_input.strip()
50
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
51
- sheet.append_row([list_name, contact_csv, tags, timestamp])
52
- st.success(f"List '{list_name}' saved successfully!")
 
 
 
 
 
 
 
53
 
54
- # Load and manage saved lists
55
- st.markdown("---")
56
- st.markdown("### πŸ“‚ Load or Manage Saved Lists")
 
 
 
 
 
 
57
 
58
- saved_lists = sheet.get_all_records()
59
- if saved_lists:
60
- list_names = [entry["list_name"] for entry in saved_lists]
61
- selected_list_name = st.selectbox("πŸ“œ Select a list", list_names)
 
 
 
 
 
 
 
 
 
62
 
63
- if selected_list_name:
64
- entry = next((x for x in saved_lists if x["list_name"] == selected_list_name), None)
65
- if entry:
66
- st.markdown(f"**List Name:** {entry['list_name']}")
67
- st.markdown(f"**Contacts:** {entry['contacts_csv']}")
68
- st.markdown(f"**Tags:** {entry['tags']}")
69
- st.markdown(f"**Saved on:** {entry['timestamp']}")
 
 
 
 
 
70
 
71
- # Delete
72
- if st.button("πŸ—‘ Delete this list"):
73
- all_values = sheet.get_all_values()
74
- headers = all_values[0]
75
- for idx, row in enumerate(all_values[1:], start=2):
76
- if row[0] == selected_list_name:
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()