-
Notifications
You must be signed in to change notification settings - Fork 0
/
supplier_form.py
105 lines (85 loc) · 3.41 KB
/
supplier_form.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
import streamlit as st
import pandas as pd
from database import get_session
from models import Suppliers
from utils import validate_and_format_phone, validate_zip_code, validate_email, validate_and_format_dataframe, format_dataframe
def validate_and_format_supplier_data(df):
validations = [
{'func': validate_and_format_phone, 'args': ['ContactPhone1']},
{'func': validate_and_format_phone, 'args': ['ContactPhone2']},
{'func': validate_zip_code, 'args': ['SupplierZipCode']},
{'func': validate_email, 'args': ['ContactEmail1']},
{'func': validate_email, 'args': ['ContactEmail2']},
]
return validate_and_format_dataframe(df, validations)
def sortable_data_editor(df, key, column_config=None):
col1, col2, col3 = st.columns([2, 2, 4])
with col1:
sort_column = st.selectbox(
"Sort by",
options=["None"] + list(df.columns),
key=f"{key}_sort_column"
)
if sort_column != "None":
with col2:
sort_order = st.radio(
"Sort order",
options=["Ascending", "Descending"],
horizontal=True,
key=f"{key}_sort_order"
)
ascending = sort_order == "Ascending"
df = df.sort_values(by=sort_column, ascending=ascending)
edited_df = st.data_editor(
df,
key=f"{key}_editor",
column_config=column_config,
num_rows="dynamic"
)
return edited_df
def display_supplier_form():
st.info("""
How to use this grid:
1. Edit any data directly in the grid below.
2. To add a new record, click the '+' icon on the upper right of the grid.
3. Use the 'Sort by' dropdown to sort the data by a specific column.
4. Use the 'Sort order' radio buttons to choose ascending or descending order.
Note: All edits are temporary until you click 'Save Changes'.
""")
st.header("Suppliers")
session = get_session()
try:
suppliers_data = session.query(Suppliers).all()
suppliers_df = pd.DataFrame([{c.name: getattr(sup, c.name) for c in sup.__table__.columns} for sup in suppliers_data])
suppliers_df = format_dataframe(suppliers_df)
display_df = suppliers_df.drop(columns=['ID'])
edited_df = sortable_data_editor(
display_df,
key="supplier_data",
column_config=None
)
if st.button("Save Changes"):
edited_df['ID'] = suppliers_df['ID']
formatted_df, errors = validate_and_format_supplier_data(edited_df)
if errors:
for error in errors:
st.error(error)
else:
save_changes(Suppliers, formatted_df, session)
finally:
session.close()
def save_changes(model, edited_df, session):
try:
for index, row in edited_df.iterrows():
item = session.query(model).filter_by(ID=row['ID']).first()
if item:
for column in row.index:
setattr(item, column, row[column])
else:
new_item = model(**row.to_dict())
session.add(new_item)
session.commit()
st.success("Changes saved successfully!")
except Exception as e:
st.error(f"An error occurred: {e}")
session.rollback()