-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathvendor_app.py
162 lines (146 loc) · 5.73 KB
/
vendor_app.py
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
154
155
156
157
158
159
160
161
162
import streamlit as st
from streamlit_gsheets import GSheetsConnection
import pandas as pd
# Display Title and Description
st.title("Vendor Management Portal")
# Constants
BUSINESS_TYPES = [
"Manufacturer",
"Distributor",
"Wholesaler",
"Retailer",
"Service Provider",
]
PRODUCTS = [
"Electronics",
"Apparel",
"Groceries",
"Software",
"Other",
]
# Establishing a Google Sheets connection
conn = st.experimental_connection("gsheets", type=GSheetsConnection)
# Fetch existing vendors data
existing_data = conn.read(worksheet="Vendors", usecols=list(range(6)), ttl=5)
existing_data = existing_data.dropna(how="all")
action = st.selectbox(
"Choose an Action",
[
"Onboard New Vendor",
"Update Existing Vendor",
"View All Vendors",
"Delete Vendor",
],
)
if action == "Onboard New Vendor":
st.markdown("Enter the details of the new vendor below.")
with st.form(key="vendor_form"):
company_name = st.text_input(label="Company Name*")
business_type = st.selectbox(
"Business Type*", options=BUSINESS_TYPES, index=None
)
products = st.multiselect("Products Offered", options=PRODUCTS)
years_in_business = st.slider("Years in Business", 0, 50, 5)
onboarding_date = st.date_input(label="Onboarding Date")
additional_info = st.text_area(label="Additional Notes")
st.markdown("**required*")
submit_button = st.form_submit_button(label="Submit Vendor Details")
if submit_button:
if not company_name or not business_type:
st.warning("Ensure all mandatory fields are filled.")
elif existing_data["CompanyName"].str.contains(company_name).any():
st.warning("A vendor with this company name already exists.")
else:
vendor_data = pd.DataFrame(
[
{
"CompanyName": company_name,
"BusinessType": business_type,
"Products": ", ".join(products),
"YearsInBusiness": years_in_business,
"OnboardingDate": onboarding_date.strftime("%Y-%m-%d"),
"AdditionalInfo": additional_info,
}
]
)
updated_df = pd.concat([existing_data, vendor_data], ignore_index=True)
conn.update(worksheet="Vendors", data=updated_df)
st.success("Vendor details successfully submitted!")
elif action == "Update Existing Vendor":
st.markdown("Select a vendor and update their details.")
vendor_to_update = st.selectbox(
"Select a Vendor to Update", options=existing_data["CompanyName"].tolist()
)
vendor_data = existing_data[existing_data["CompanyName"] == vendor_to_update].iloc[
0
]
with st.form(key="update_form"):
company_name = st.text_input(
label="Company Name*", value=vendor_data["CompanyName"]
)
business_type = st.selectbox(
"Business Type*",
options=BUSINESS_TYPES,
index=BUSINESS_TYPES.index(vendor_data["BusinessType"]),
)
products = st.multiselect(
"Products Offered",
options=PRODUCTS,
default=vendor_data["Products"].split(", "),
)
years_in_business = st.slider(
"Years in Business", 0, 50, int(vendor_data["YearsInBusiness"])
)
onboarding_date = st.date_input(
label="Onboarding Date", value=pd.to_datetime(vendor_data["OnboardingDate"])
)
additional_info = st.text_area(
label="Additional Notes", value=vendor_data["AdditionalInfo"]
)
st.markdown("**required*")
update_button = st.form_submit_button(label="Update Vendor Details")
if update_button:
if not company_name or not business_type:
st.warning("Ensure all mandatory fields are filled.")
else:
# Removing old entry
existing_data.drop(
existing_data[
existing_data["CompanyName"] == vendor_to_update
].index,
inplace=True,
)
# Creating updated data entry
updated_vendor_data = pd.DataFrame(
[
{
"CompanyName": company_name,
"BusinessType": business_type,
"Products": ", ".join(products),
"YearsInBusiness": years_in_business,
"OnboardingDate": onboarding_date.strftime("%Y-%m-%d"),
"AdditionalInfo": additional_info,
}
]
)
# Adding updated data to the dataframe
updated_df = pd.concat(
[existing_data, updated_vendor_data], ignore_index=True
)
conn.update(worksheet="Vendors", data=updated_df)
st.success("Vendor details successfully updated!")
# View All Vendors
elif action == "View All Vendors":
st.dataframe(existing_data)
# Delete Vendor
elif action == "Delete Vendor":
vendor_to_delete = st.selectbox(
"Select a Vendor to Delete", options=existing_data["CompanyName"].tolist()
)
if st.button("Delete"):
existing_data.drop(
existing_data[existing_data["CompanyName"] == vendor_to_delete].index,
inplace=True,
)
conn.update(worksheet="Vendors", data=existing_data)
st.success("Vendor successfully deleted!")