Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import plotly.express as px | |
import plotly.graph_objects as go | |
import numpy as np | |
from datetime import datetime | |
# Set page config | |
st.set_page_config(page_title="مدیریت دادههای مزارع", layout="wide") | |
# Add custom CSS for RTL support and table styling | |
st.markdown( | |
""" | |
<style> | |
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;700&display=swap'); | |
.main { | |
direction: rtl; | |
font-family: 'Vazirmatn', sans-serif; | |
} | |
.stSelectbox, .stMultiSelect, .stTextInput > div > div > input { | |
direction: rtl; | |
text-align: right; | |
} | |
div[data-testid="stDataFrame"] { | |
direction: rtl; | |
} | |
button, .stMarkdown { | |
direction: rtl; | |
text-align: right; | |
} | |
.styled-table { | |
width: 100%; | |
border-collapse: collapse; | |
margin: 25px 0; | |
font-size: 0.9em; | |
font-family: 'Vazirmatn', sans-serif; | |
min-width: 400px; | |
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15); | |
} | |
.styled-table thead tr { | |
background-color: #009879; | |
color: #ffffff; | |
text-align: right; | |
} | |
.styled-table th, | |
.styled-table td { | |
padding: 12px 15px; | |
text-align: right; | |
} | |
.styled-table tbody tr { | |
border-bottom: 1px solid #dddddd; | |
} | |
.styled-table tbody tr:nth-of-type(even) { | |
background-color: #f3f3f3; | |
} | |
.styled-table tbody tr:last-of-type { | |
border-bottom: 2px solid #009879; | |
} | |
</style> | |
""", unsafe_allow_html=True | |
) | |
# Load CSV file | |
def load_data(): | |
try: | |
data = pd.read_csv('پایگاه داده.csv') | |
return data | |
except FileNotFoundError: | |
return pd.DataFrame({ | |
'ردیف': [], 'کد نماینده': [], 'کانال': [], 'ادار': [], | |
'تولید': [], 'مساحت': [], 'مساحت زیرمجموعه': [], | |
'واریته': [], 'سن': [], 'ایام هفته': [], 'هفته': [] | |
}) | |
# Initialize session state | |
if 'data' not in st.session_state: | |
st.session_state.data = load_data() | |
# Sidebar - Day and Week selection | |
st.sidebar.header("انتخاب روز و هفته") | |
days = ["شنبه", "یکشنبه", "دوشنبه", "سهشنبه", "چهارشنبه", "پنجشنبه"] | |
selected_day = st.sidebar.selectbox("روز هفته", days) | |
selected_week = st.sidebar.selectbox("هفته", range(1, 23)) | |
# Create tabs | |
tab1, tab2 = st.tabs(["ورود داده", "مشاهده و تحلیل دادهها"]) | |
with tab1: | |
st.header(f"ورود دادهها - هفته {selected_week} - {selected_day}") | |
# Filter data for selected day and week | |
filtered_data = st.session_state.data[ | |
(st.session_state.data['ایام هفته'] == selected_day) & | |
(st.session_state.data['هفته'] == selected_week) | |
] if not st.session_state.data.empty else pd.DataFrame() | |
# Create form for data entry | |
with st.form("data_entry_form"): | |
# Convert DataFrame to HTML table with input fields | |
table_html = """ | |
<table class="styled-table"> | |
<thead> | |
<tr> | |
<th>ردیف</th> | |
<th>کد نماینده</th> | |
<th>کانال</th> | |
<th>ادار</th> | |
<th>تولید</th> | |
<th>مساحت</th> | |
<th>واریته</th> | |
<th>سن</th> | |
<th>ایستگاه 1</th> | |
<th>ایستگاه 2</th> | |
<th>ایستگاه 3</th> | |
<th>ایستگاه 4</th> | |
<th>ایستگاه 5</th> | |
</tr> | |
</thead> | |
<tbody> | |
""" | |
for idx, row in filtered_data.iterrows(): | |
table_html += f""" | |
<tr> | |
<td>{row['ردیف']}</td> | |
<td>{row['کد نماینده']}</td> | |
<td>{row['کانال']}</td> | |
<td>{row['ادار']}</td> | |
<td>{row['تولید']}</td> | |
<td>{row['مساحت']}</td> | |
<td>{row['واریته']}</td> | |
<td>{row['سن']}</td> | |
<td><input type="number" name="station1_{idx}" /></td> | |
<td><input type="number" name="station2_{idx}" /></td> | |
<td><input type="number" name="station3_{idx}" /></td> | |
<td><input type="number" name="station4_{idx}" /></td> | |
<td><input type="number" name="station5_{idx}" /></td> | |
</tr> | |
""" | |
table_html += "</tbody></table>" | |
st.markdown(table_html, unsafe_allow_html=True) | |
submitted = st.form_submit_button("ثبت اطلاعات") | |
with tab2: | |
if not st.session_state.data.empty: | |
st.header("تحلیل دادهها") | |
col1, col2 = st.columns(2) | |
with col1: | |
# Growth trend analysis | |
st.subheader("روند رشد") | |
fig_growth = px.line( | |
st.session_state.data, | |
x='هفته', | |
y='ارتفاع هفته جاری', | |
color='کانال', | |
title='روند رشد بر اساس هفته' | |
) | |
st.plotly_chart(fig_growth) | |
with col2: | |
# 3D surface plot | |
st.subheader("نمودار سطح سهبعدی رشد") | |
fig_3d = go.Figure(data=[go.Surface( | |
z=st.session_state.data.pivot_table( | |
values='ارتفاع هفته جاری', | |
index='هفته', | |
columns='کانال' | |
).values, | |
colorscale='Viridis' | |
)]) | |
fig_3d.update_layout( | |
scene=dict( | |
xaxis_title='هفته', | |
yaxis_title='کانال', | |
zaxis_title='ارتفاع' | |
), | |
title='نمودار سهبعدی رشد' | |
) | |
st.plotly_chart(fig_3d) | |
# Statistics | |
st.subheader("آمار و تحلیل") | |
col3, col4, col5 = st.columns(3) | |
with col3: | |
st.metric( | |
"بیشترین رشد", | |
f"{st.session_state.data['رشد هفته جاری'].max():.2f}" | |
) | |
with col4: | |
st.metric( | |
"کمترین رشد", | |
f"{st.session_state.data['رشد هفته جاری'].min():.2f}" | |
) | |
with col5: | |
st.metric( | |
"میانگین رشد", | |
f"{st.session_state.data['رشد هفته جاری'].mean():.2f}" | |
) | |
# Detailed data table | |
st.subheader("جدول دادههای کامل") | |
st.dataframe(st.session_state.data) | |
# Download button for updated data | |
if st.button("دانلود فایل CSV"): | |
csv = st.session_state.data.to_csv(index=False).encode('utf-8-sig') | |
st.download_button( | |
label="دانلود", | |
data=csv, | |
file_name="پایگاه داده.csv", | |
mime="text/csv" | |
) |