-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_data.py
55 lines (45 loc) · 1.77 KB
/
add_data.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
#import libraries
import streamlit as st
import pandas as pd
#load data set
def add_data():
df=pd.read_csv("sales.csv")
#clear hutumika kufuta form akishasubmit form
with st.form("form 2",clear_on_submit=True):
col1,col2=st.columns(2)
orderdate=col1.date_input(label="order date")
region=col2.selectbox("region",df["Region"].unique())
col11,col22=st.columns(2)
city=col11.selectbox("city",df["City"].unique())
category=col22.selectbox("category",df["Category"].unique())
col111,col222,col333=st.columns(3)
product=col111.selectbox("product name",df["Product"].unique())
quantity=col222.number_input("quantity")
unitprice=col333.number_input("unitprice")
#form Button
btn=st.form_submit_button("Save Data To Excel", type="primary")
#if btn is clicked
#validate
if btn:
if orderdate=="" or region=="" or city==""or category==""or product==""or quantity==""or unitprice==""or quantity==0.00 or unitprice==0.00:
st.warning("All fields are required")
return False
else:
df = pd.concat([df, pd.DataFrame.from_records([{
'OrderDate': orderdate,
'Region':region,
'City':city,
'Category':category,
'Product':product,
'Quantity':quantity,
'UnitPrice':float(unitprice),
'TotalPrice':float(quantity)*float(unitprice),
}])])
try:
df.to_csv("data.csv",index=False)
st.success(product+ " Has been Added successfully !")
return True
except:
st.warning("Unable to write, Please close your dataset !!")
return False
st.experimental_rerun