Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,115 +2,211 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
from datetime import datetime
|
4 |
import plotly.express as px
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
if 'data' not in st.session_state:
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
#
|
26 |
-
st.
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
with tab1:
|
32 |
-
|
33 |
-
st.subheader("ورود اطلاعات پایه")
|
34 |
-
with st.form("basic_info_form"):
|
35 |
col1, col2, col3 = st.columns(3)
|
36 |
|
37 |
with col1:
|
38 |
-
|
39 |
channel = st.text_input("کانال")
|
40 |
department = st.text_input("اداره")
|
41 |
-
|
|
|
|
|
42 |
with col2:
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
with col3:
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
if submit_basic:
|
54 |
-
st.success("اطلاعات پایه با موفقیت ثبت شد")
|
55 |
|
56 |
with tab2:
|
57 |
-
|
58 |
-
|
59 |
-
with st.form("nitrogen_info_form"):
|
60 |
col1, col2 = st.columns(2)
|
61 |
-
|
62 |
with col1:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
67 |
with col2:
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
if submit_nitrogen:
|
74 |
-
st.success("اطلاعات نیتروژن با موفقیت ثبت شد")
|
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 |
-
st.
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
st.
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
label="دانلود فایل اکسل",
|
113 |
-
data=output,
|
114 |
-
file_name=f"farm_data_export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx",
|
115 |
-
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
116 |
-
)
|
|
|
2 |
import pandas as pd
|
3 |
from datetime import datetime
|
4 |
import plotly.express as px
|
5 |
+
|
6 |
+
# Set page config - removed direction parameter
|
7 |
+
st.set_page_config(page_title="مدیریت دادههای مزارع", layout="wide")
|
8 |
+
|
9 |
+
# Add custom CSS for RTL support - enhanced CSS
|
10 |
+
st.markdown(
|
11 |
+
"""
|
12 |
+
<style>
|
13 |
+
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;700&display=swap');
|
14 |
+
|
15 |
+
.main {
|
16 |
+
direction: rtl;
|
17 |
+
font-family: 'Vazirmatn', sans-serif;
|
18 |
+
}
|
19 |
+
.stSelectbox, .stMultiSelect {
|
20 |
+
direction: rtl;
|
21 |
+
text-align: right;
|
22 |
+
}
|
23 |
+
div[data-testid="stDataFrame"] {
|
24 |
+
direction: rtl;
|
25 |
+
}
|
26 |
+
.stTextInput > div > div > input {
|
27 |
+
direction: rtl;
|
28 |
+
text-align: right;
|
29 |
+
}
|
30 |
+
button {
|
31 |
+
direction: rtl;
|
32 |
+
text-align: right;
|
33 |
+
}
|
34 |
+
.stMarkdown {
|
35 |
+
direction: rtl;
|
36 |
+
text-align: right;
|
37 |
+
}
|
38 |
+
</style>
|
39 |
+
""", unsafe_allow_html=True
|
40 |
+
)
|
41 |
+
|
42 |
+
# Initialize session state for data persistence
|
43 |
if 'data' not in st.session_state:
|
44 |
+
# Initialize with empty DataFrame with all required columns
|
45 |
+
st.session_state.data = pd.DataFrame({
|
46 |
+
'مزرعه': [], 'کانال': [], 'اداره': [], 'سن': [],
|
47 |
+
'واریته': [], 'مساحت': [], 'مساحت زیرمجموعه': [],
|
48 |
+
'گروه': [], 'ایام هفته': [],
|
49 |
+
'ایستگاه 1': [], 'ایستگاه 2': [], 'ایستگاه 3': [],
|
50 |
+
'ایستگاه 4': [], 'ایستگاه 5': [], 'ارتفاع هفته جاری': [],
|
51 |
+
'ارتفاع هفته گذشته': [], 'رشد هفته جاری': [], 'رشد هفته گذشته': [],
|
52 |
+
'نیتروژن فعلی': [], 'نیتروژن استاندارد فعلی': [], 'نیتروژن قبلی': [],
|
53 |
+
'نیتروژن استاندارد قبلی': [], 'رطوبت غلاف فعلی': [],
|
54 |
+
'رطوبت استاندارد فعلی': [], 'رطوبت غلاف قبلی': [],
|
55 |
+
'رطوبت استاندارد قبلی': [], 'چاهک 1': [], 'تاریخ قرائت 1': [],
|
56 |
+
'چاهک 2': [], 'تاریخ قرائت 2': []
|
57 |
+
})
|
58 |
+
|
59 |
+
def validate_input(value, field_type):
|
60 |
+
"""Validate input values"""
|
61 |
+
if field_type == 'numeric':
|
62 |
+
try:
|
63 |
+
val = float(value)
|
64 |
+
if val < 0:
|
65 |
+
return False, "مقدار نمیتواند منفی باشد"
|
66 |
+
return True, val
|
67 |
+
except:
|
68 |
+
return False, "لطفا یک عدد معتبر وارد کنید"
|
69 |
+
return True, value
|
70 |
+
|
71 |
+
def calculate_growth(current_height, previous_height):
|
72 |
+
"""Calculate growth between weeks"""
|
73 |
+
if current_height and previous_height:
|
74 |
+
return current_height - previous_height
|
75 |
+
return 0
|
76 |
|
77 |
+
# Sidebar filters
|
78 |
+
st.sidebar.header("فیلترها")
|
79 |
|
80 |
+
# Week and day selection
|
81 |
+
days = ["شنبه", "یکشنبه", "دوشنبه", "سهشنبه", "چهارشنبه", "پنجشنبه"]
|
82 |
+
weeks = list(range(1, 23))
|
83 |
+
|
84 |
+
selected_day = st.sidebar.selectbox("روز هفته", days)
|
85 |
+
selected_week = st.sidebar.selectbox("هفته", weeks)
|
86 |
+
|
87 |
+
# Main content
|
88 |
+
st.title("سیستم مدیریت دادههای مزارع")
|
89 |
+
|
90 |
+
# Create tabs for different sections
|
91 |
+
tab1, tab2, tab3 = st.tabs(["ورود داده", "مشاهده دادهها", "نمودارها"])
|
92 |
|
93 |
with tab1:
|
94 |
+
with st.form("data_entry"):
|
|
|
|
|
95 |
col1, col2, col3 = st.columns(3)
|
96 |
|
97 |
with col1:
|
98 |
+
farm = st.text_input("مزرعه")
|
99 |
channel = st.text_input("کانال")
|
100 |
department = st.text_input("اداره")
|
101 |
+
age = st.number_input("سن", min_value=0)
|
102 |
+
variety = st.text_input("واریته")
|
103 |
+
|
104 |
with col2:
|
105 |
+
station1 = st.number_input("ایستگاه 1", min_value=0.0)
|
106 |
+
station2 = st.number_input("ایستگاه 2", min_value=0.0)
|
107 |
+
station3 = st.number_input("ایستگاه 3", min_value=0.0)
|
108 |
+
station4 = st.number_input("ایستگاه 4", min_value=0.0)
|
109 |
+
station5 = st.number_input("ایستگاه 5", min_value=0.0)
|
110 |
+
|
111 |
with col3:
|
112 |
+
current_height = st.number_input("ارتفاع هفته جاری", min_value=0.0)
|
113 |
+
previous_height = st.number_input("ارتفاع هفته گذشته", min_value=0.0)
|
114 |
+
nitrogen = st.number_input("نیتروژن فعلی", min_value=0.0)
|
115 |
+
moisture = st.number_input("رطوبت غلاف فعلی", min_value=0.0)
|
116 |
+
well1 = st.number_input("چاهک 1", min_value=0.0)
|
117 |
+
|
118 |
+
submitted = st.form_submit_button("ثبت اطلاعات")
|
119 |
+
|
120 |
+
if submitted:
|
121 |
+
# Calculate growth rate
|
122 |
+
growth_rate = calculate_growth(current_height, previous_height)
|
123 |
+
|
124 |
+
# Create new data entry
|
125 |
+
new_data = {
|
126 |
+
'مزرعه': farm,
|
127 |
+
'کانال': channel,
|
128 |
+
'اداره': department,
|
129 |
+
'سن': age,
|
130 |
+
'واریته': variety,
|
131 |
+
'ایستگاه 1': station1,
|
132 |
+
'ایستگاه 2': station2,
|
133 |
+
'ایستگاه 3': station3,
|
134 |
+
'ایستگاه 4': station4,
|
135 |
+
'ایستگاه 5': station5,
|
136 |
+
'ارتفاع هفته جاری': current_height,
|
137 |
+
'ارتفاع هفته گذشته': previous_height,
|
138 |
+
'رشد هفته جاری': growth_rate,
|
139 |
+
'نیتروژن فعلی': nitrogen,
|
140 |
+
'رطوبت غلاف فعلی': moisture,
|
141 |
+
'چاهک 1': well1,
|
142 |
+
'تاریخ قرائت 1': datetime.now().strftime('%Y-%m-%d'),
|
143 |
+
'ایام هفته': selected_day,
|
144 |
+
'هفته': selected_week
|
145 |
+
}
|
146 |
+
|
147 |
+
# Update session state
|
148 |
+
st.session_state.data = pd.concat([
|
149 |
+
st.session_state.data,
|
150 |
+
pd.DataFrame([new_data])
|
151 |
+
], ignore_index=True)
|
152 |
|
153 |
+
st.success("اطلاعات با موفقیت ثبت شد")
|
|
|
|
|
154 |
|
155 |
with tab2:
|
156 |
+
if not st.session_state.data.empty:
|
157 |
+
# Add filters
|
|
|
158 |
col1, col2 = st.columns(2)
|
|
|
159 |
with col1:
|
160 |
+
if 'اداره' in st.session_state.data.columns:
|
161 |
+
dept_filter = st.multiselect(
|
162 |
+
'فیلتر بر اساس اداره',
|
163 |
+
options=st.session_state.data['اداره'].unique()
|
164 |
+
)
|
165 |
+
|
166 |
with col2:
|
167 |
+
if 'واریته' in st.session_state.data.columns:
|
168 |
+
variety_filter = st.multiselect(
|
169 |
+
'فیلتر بر اساس واریته',
|
170 |
+
options=st.session_state.data['واریته'].unique()
|
171 |
+
)
|
|
|
|
|
172 |
|
173 |
+
# Apply filters
|
174 |
+
filtered_data = st.session_state.data.copy()
|
175 |
+
if dept_filter:
|
176 |
+
filtered_data = filtered_data[filtered_data['اداره'].isin(dept_filter)]
|
177 |
+
if variety_filter:
|
178 |
+
filtered_data = filtered_data[filtered_data['واریته'].isin(variety_filter)]
|
179 |
+
|
180 |
+
# Display filtered data
|
181 |
+
st.dataframe(filtered_data)
|
182 |
|
183 |
+
# Download button
|
184 |
+
csv = filtered_data.to_csv(index=False).encode('utf-8-sig')
|
185 |
+
st.download_button(
|
186 |
+
label="دانلود دادهها",
|
187 |
+
data=csv,
|
188 |
+
file_name="پایگاه داده.csv",
|
189 |
+
mime="text/csv"
|
190 |
+
)
|
191 |
+
|
192 |
+
with tab3:
|
193 |
+
if not st.session_state.data.empty:
|
194 |
+
# Growth trend chart
|
195 |
+
st.subheader("نمودار رشد")
|
196 |
+
fig1 = px.line(
|
197 |
+
st.session_state.data,
|
198 |
+
x='هفته',
|
199 |
+
y='ارتفاع هفته جاری',
|
200 |
+
color='مزرعه',
|
201 |
+
title='روند رشد بر اساس هفته'
|
202 |
+
)
|
203 |
+
st.plotly_chart(fig1)
|
204 |
+
|
205 |
+
# Station measurements comparison
|
206 |
+
st.subheader("مقایسه ایستگاهها")
|
207 |
+
station_data = st.session_state.data[['ایستگاه 1', 'ایستگاه 2', 'ایستگاه 3', 'ایستگاه 4', 'ایستگاه 5']]
|
208 |
+
fig2 = px.box(station_data, title='مقایسه اندازهگیریهای ایستگاهها')
|
209 |
+
st.plotly_chart(fig2)
|
210 |
+
|
211 |
+
# Add error handling
|
212 |
+
st.error("در صورت بروز خطا، لطفا صفحه را رفرش کنید")
|
|
|
|
|
|
|
|
|
|