Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,110 @@
|
|
|
|
1 |
import pandas as pd
|
2 |
-
import
|
3 |
import plotly.graph_objects as go
|
4 |
-
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
id='farm-dropdown',
|
24 |
-
options=[{'label': name, 'value': name} for name in farm_names],
|
25 |
-
placeholder='Select a farm...'
|
26 |
-
),
|
27 |
-
html.Div(id='output-container')
|
28 |
-
], width=12)
|
29 |
-
]),
|
30 |
-
dbc.Row([
|
31 |
-
dbc.Col([
|
32 |
-
dcc.Graph(id='height-histogram')
|
33 |
-
], width=12)
|
34 |
-
])
|
35 |
-
], fluid=True)
|
36 |
-
|
37 |
-
@app.callback(
|
38 |
-
Output('height-histogram', 'figure'),
|
39 |
-
Output('output-container', 'children'),
|
40 |
-
Input('farm-dropdown', 'value')
|
41 |
-
)
|
42 |
-
def update_graph(selected_farm):
|
43 |
-
if selected_farm is None:
|
44 |
-
return go.Figure(), "Please select a farm."
|
45 |
-
|
46 |
-
# Filter data based on selected farm
|
47 |
-
filtered_df = df[df[' مزرعه '] == selected_farm]
|
48 |
-
|
49 |
-
# Prepare 3D surface data
|
50 |
-
weeks = np.arange(1, 19)
|
51 |
-
heights = filtered_df.iloc[:, 6:24].values # Adjusted to match height columns
|
52 |
-
|
53 |
-
fig = make_subplots(rows=1, cols=1, specs=[[{'type': 'surface'}]])
|
54 |
-
|
55 |
-
fig.add_trace(
|
56 |
-
go.Surface(
|
57 |
-
z=heights,
|
58 |
-
x=weeks,
|
59 |
-
y=filtered_df[' مزرعه '].values,
|
60 |
-
colorscale='Viridis'
|
61 |
-
)
|
62 |
-
)
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
)
|
72 |
|
73 |
-
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
if __name__ ==
|
76 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
import plotly.graph_objects as go
|
5 |
+
import json
|
6 |
+
from mpl_toolkits.mplot3d import Axes3D
|
7 |
+
import base64
|
8 |
+
from io import BytesIO
|
9 |
+
|
10 |
+
# بارگذاری دادهها
|
11 |
+
@st.cache_data
|
12 |
+
def load_data():
|
13 |
+
with open('farm_data.json', 'r') as f:
|
14 |
+
data = json.load(f)
|
15 |
+
return pd.DataFrame(data)
|
16 |
+
|
17 |
+
# فیلتر کردن دادهها بر اساس نام مزرعه
|
18 |
+
def filter_data(df, farm_name):
|
19 |
+
return df[df['farm_name'].str.contains(farm_name, case=False)]
|
20 |
+
|
21 |
+
# نمایش جدول اطلاعات مزرعه
|
22 |
+
def show_farm_table(df):
|
23 |
+
st.dataframe(df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# اضافه کردن ارتفاع هفتگی
|
26 |
+
def add_weekly_growth(df):
|
27 |
+
df['weekly_height'] = df['height'] + (df['age'] * 0.5)
|
28 |
+
return df
|
29 |
+
|
30 |
+
# ایجاد نمودار 3D
|
31 |
+
def create_3d_plot(df):
|
32 |
+
fig = plt.figure(figsize=(10, 8))
|
33 |
+
ax = fig.add_subplot(111, projection='3d')
|
34 |
+
ax.scatter(df['x'], df['y'], df['height'])
|
35 |
+
ax.set_xlabel('X')
|
36 |
+
ax.set_ylabel('Y')
|
37 |
+
ax.set_zlabel('Height')
|
38 |
+
ax.set_title('3D Farm Plot')
|
39 |
+
return fig
|
40 |
+
|
41 |
+
# ایجاد نمودار انیمیشنی Plotly
|
42 |
+
def create_animated_plot(df):
|
43 |
+
frames = []
|
44 |
+
for week in range(5):
|
45 |
+
df['animated_height'] = df['height'] + (week * 0.5)
|
46 |
+
frame = go.Frame(data=[go.Scatter3d(x=df['x'], y=df['y'], z=df['animated_height'],
|
47 |
+
mode='markers', marker=dict(size=5))])
|
48 |
+
frames.append(frame)
|
49 |
+
|
50 |
+
fig = go.Figure(
|
51 |
+
data=[go.Scatter3d(x=df['x'], y=df['y'], z=df['height'], mode='markers', marker=dict(size=5))],
|
52 |
+
layout=go.Layout(
|
53 |
+
scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Height'),
|
54 |
+
updatemenus=[dict(type='buttons', showactive=False, buttons=[dict(label='Play',
|
55 |
+
method='animate', args=[None, dict(frame=dict(duration=500, redraw=True), fromcurrent=True)])])]
|
56 |
+
),
|
57 |
+
frames=frames
|
58 |
)
|
59 |
|
60 |
+
return fig
|
61 |
+
|
62 |
+
# تابع اصلی Streamlit
|
63 |
+
def main():
|
64 |
+
st.title("داشبورد هوشمند مزرعه")
|
65 |
+
|
66 |
+
# بارگذاری دادهها
|
67 |
+
df = load_data()
|
68 |
+
|
69 |
+
# جستجوی نام مزرعه
|
70 |
+
farm_name = st.text_input("نام مزرعه را جستجو کنید:")
|
71 |
+
if farm_name:
|
72 |
+
df = filter_data(df, farm_name)
|
73 |
+
|
74 |
+
# نمایش جدول اطلاعات مزرعه
|
75 |
+
show_farm_table(df)
|
76 |
+
|
77 |
+
# فیلد رشد هفتگی
|
78 |
+
weekly_growth = st.checkbox("نمایش رشد هفتگی")
|
79 |
+
if weekly_growth:
|
80 |
+
df = add_weekly_growth(df)
|
81 |
+
show_farm_table(df)
|
82 |
+
|
83 |
+
# نمایش نمودارها
|
84 |
+
if st.button("نمایش نمودارها"):
|
85 |
+
plot_type = st.selectbox("نوع نمودار را انتخاب کنید:", ["نمودار 3D ثابت", "نمودار 3D انیمیشنی"])
|
86 |
+
|
87 |
+
if plot_type == "نمودار 3D ثابت":
|
88 |
+
fig = create_3d_plot(df)
|
89 |
+
st.pyplot(fig)
|
90 |
+
else:
|
91 |
+
fig = create_animated_plot(df)
|
92 |
+
st.plotly_chart(fig)
|
93 |
+
|
94 |
+
# دانلود داده و نمودار
|
95 |
+
if st.button("دانلود دادهها (CSV)"):
|
96 |
+
csv = df.to_csv(index=False)
|
97 |
+
b64 = base64.b64encode(csv.encode()).decode()
|
98 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="farm_data.csv">دانلود فایل CSV</a>'
|
99 |
+
st.markdown(href, unsafe_allow_html=True)
|
100 |
+
|
101 |
+
if st.button("دانلود نمودار (PNG)"):
|
102 |
+
fig = create_3d_plot(df)
|
103 |
+
buf = BytesIO()
|
104 |
+
fig.savefig(buf, format="png")
|
105 |
+
b64 = base64.b64encode(buf.getvalue()).decode()
|
106 |
+
href = f'<a href="data:image/png;base64,{b64}" download="farm_plot.png">دانلود تصویر PNG</a>'
|
107 |
+
st.markdown(href, unsafe_allow_html=True)
|
108 |
|
109 |
+
if __name__ == "__main__":
|
110 |
+
main()
|