Esmaeilkiani commited on
Commit
339880e
·
verified ·
1 Parent(s): 9ed34d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -83
app.py CHANGED
@@ -1,96 +1,36 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import altair as alt
4
 
5
- # توابع مورد نیاز
6
- def calculate_population_difference(df, year):
7
- # مثال از محاسبه تفاوت جمعیت
8
- df['population_difference'] = df['population'] - df['population'].shift(1)
9
- return df[df['year'] == year]
10
 
11
  def format_number(num):
12
  return f"{num:,}"
13
 
14
- def make_donut(percentage, label, color):
15
- data = pd.DataFrame({
16
- 'category': [label, ''],
17
- 'value': [percentage, 100 - percentage]
18
- })
19
- chart = alt.Chart(data).mark_arc(innerRadius=50).encode(
20
- theta='value',
21
- color=alt.condition(
22
- alt.datum.category == label,
23
- alt.value(color),
24
- alt.value('lightgray')
25
- )
26
- ).properties(
27
- width=200,
28
- height=200
29
- )
30
- return chart
31
 
32
- # داده نمونه
33
- data = {
34
- 'states': ['State1', 'State2', 'State3', 'State4'],
35
- 'population': [100000, 200000, 150000, 250000],
36
- 'year': [2011, 2011, 2011, 2011]
37
- }
38
-
39
- df = pd.DataFrame(data)
40
-
41
- # ایجاد داشبورد
42
  st.title('داشبورد تجزیه و تحلیل مزارع نیشکر')
43
 
44
- selected_year = st.slider('سال را انتخاب کنید', min_value=1399, max_value=1401, value=1402, step=1)
45
-
46
- df_population_difference_sorted = calculate_population_difference(df, selected_year)
47
-
48
- col1, col2 = st.columns(2)
49
-
50
- with col1:
51
- st.markdown('#### Gains/Losses')
52
-
53
- if selected_year > 2010:
54
- first_state_name = df_population_difference_sorted.states.iloc[0]
55
- first_state_population = format_number(df_population_difference_sorted.population.iloc[0])
56
- first_state_delta = format_number(df_population_difference_sorted.population_difference.iloc[0])
57
- else:
58
- first_state_name = '-'
59
- first_state_population = '-'
60
- first_state_delta = ''
61
- st.metric(label=first_state_name, value=first_state_population, delta=first_state_delta)
62
-
63
- if selected_year > 2010:
64
- last_state_name = df_population_difference_sorted.states.iloc[-1]
65
- last_state_population = format_number(df_population_difference_sorted.population.iloc[-1])
66
- last_state_delta = format_number(df_population_difference_sorted.population_difference.iloc[-1])
67
- else:
68
- last_state_name = '-'
69
- last_state_population = '-'
70
- last_state_delta = ''
71
- st.metric(label=last_state_name, value=last_state_population, delta=last_state_delta)
72
 
73
- st.markdown('#### States Migration')
74
 
75
- if selected_year > 2010:
76
- # Filter states with population difference > 50000
77
- df_greater_50000 = df_population_difference_sorted[df_population_difference_sorted.population_difference > 50000]
78
- df_less_50000 = df_population_difference_sorted[df_population_difference_sorted.population_difference < -50000]
79
-
80
- # % of States with population difference > 50000
81
- states_migration_greater = round((len(df_greater_50000)/df_population_difference_sorted.states.nunique())*100)
82
- states_migration_less = round((len(df_less_50000)/df_population_difference_sorted.states.nunique())*100)
83
- donut_chart_greater = make_donut(states_migration_greater, 'Inbound Migration', 'green')
84
- donut_chart_less = make_donut(states_migration_less, 'Outbound Migration', 'red')
85
- else:
86
- states_migration_greater = 0
87
- states_migration_less = 0
88
- donut_chart_greater = make_donut(states_migration_greater, 'Inbound Migration', 'green')
89
- donut_chart_less = make_donut(states_migration_less, 'Outbound Migration', 'red')
90
 
91
- migrations_col = st.columns((0.2, 1, 0.2))
92
- with migrations_col[1]:
93
- st.write('Inbound')
94
- st.altair_chart(donut_chart_greater)
95
- st.write('Outbound')
96
- st.altair_chart(donut_chart_less)
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import plotly.express as px
4
 
5
+ # توابع کمکی
6
+ def calculate_growth(df, week):
7
+ df['growth'] = df['ارتفاع هفته جاری مزرعه'] - df['ارتفاع هفته گذشته مزرعه']
8
+ df = df[df['هفته'] == week]
9
+ return df
10
 
11
  def format_number(num):
12
  return f"{num:,}"
13
 
14
+ # بارگذاری داده‌ها
15
+ df = pd.read_csv('path_to_your_csv_file.csv')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # تنظیم داشبورد
 
 
 
 
 
 
 
 
 
18
  st.title('داشبورد تجزیه و تحلیل مزارع نیشکر')
19
 
20
+ selected_week = st.slider('هفته را انتخاب کنید', min_value=df['هفته'].min(), max_value=df['هفته'].max(), value=df['هفته'].min(), step=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ df_growth = calculate_growth(df, selected_week)
23
 
24
+ # ایجاد نمودارهای سه‌بعدی
25
+ fig1 = px.scatter_3d(df_growth, x='ارتفاع هفته جاری مزرعه', y='رشد هفته جاری', z='نیتروژن فعلی', color='واریته', title='ارتفاع و رشد در مقابل نیتروژن')
26
+ fig2 = px.scatter_3d(df_growth, x='ارتفاع هفته جاری مزرعه', y='رشد هفته جاری', z='رطوبت غلاف فعلی', color='واریته', title='ارتفاع و رشد در مقابل رطوبت')
27
+ fig3 = px.scatter_3d(df_growth, x='ارتفاع هفته جاری مزرعه', y='رشد هفته گذشته', z='نیتروژن قبلی', color='واریته', title='ارتفاع و رشد گذشته در مقابل نیتروژن')
28
+ fig4 = px.scatter_3d(df_growth, x='ارتفاع هفته گذشته مزرعه', y='رشد هفته جاری', z='رطوبت غلاف قبلی', color='واریته', title='ارتفاع گذشته و رشد در مقابل رطوبت')
29
+ fig5 = px.scatter_3d(df_growth, x='نیتروژن فعلی', y='رطوبت غلاف فعلی', z='ارتفاع هفته جاری مزرعه', color='واریته', title='نیتروژن و رطوبت در مقابل ارتفاع')
 
 
 
 
 
 
 
 
 
30
 
31
+ # نمایش نمودارها
32
+ st.plotly_chart(fig1)
33
+ st.plotly_chart(fig2)
34
+ st.plotly_chart(fig3)
35
+ st.plotly_chart(fig4)
36
+ st.plotly_chart(fig5)