-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
926 lines (749 loc) · 43.7 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
import json
import psycopg2
import streamlit as st
import pandas as pd
import plotly.express as px
from dotenv import load_dotenv
import os
import plotly.graph_objects as go
load_dotenv()
import matplotlib.pyplot as plt
db_name = os.getenv("DB_NAME")
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")
db_host = os.getenv("DB_HOST")
db_port = os.getenv("DB_PORT")
st.set_page_config(
layout="wide",
initial_sidebar_state="auto",
page_title="Telegram BOT Dashboard",
page_icon=None,
)
def connect_to_database():
"""Connect to the PostgreSQL database."""
conn = psycopg2.connect(
dbname=db_name,
user=db_user,
password=db_password,
host=db_host,
port=db_port
)
return conn
def fetch_data(query):
"""Fetch data from the database and return as a DataFrame."""
conn = connect_to_database()
df = pd.read_sql(query, conn)
conn.close()
return df
st.header("Telegram BOT Dashboard")
query = """
SELECT da.id as da_id, da.name as da_name,da.gender as da_gender, k.id as kebele_id, k.name as kebele_name,
w.name as woreda_name, z.name as zone_name, rg.name as region_name, w.id as woreda_id,
z.id as zone_id, rg.id as region_id, ofp.gender as gender,
p.id as practice_id, a.id as advisory_id, r.id as reach_id,
ofp.id as farmer_id,a.label as advisory_label,p.name as practice_name,ad.id as adoption_id,
r.created_at as created_at
FROM
integration_developmentagent da
INNER JOIN
integration_kebele k ON k.id = da.kebele_id
INNER JOIN
integration_woreda w ON w.id=k.woreda_id
INNER JOIN
integration_zone z ON z.id=w.zone_id
INNER JOIN
integration_region rg ON rg.id=z.region_id
INNER JOIN
core_outboundfarmerprofile ofp ON ofp.kebele_id = k.id
INNER JOIN
core_reach r ON r.farmer_id = ofp.id
INNER JOIN
core_advisory a ON r.advisory_id = a.id
INNER JOIN
core_subpractice s ON a.sub_practice_id=s.id
INNER JOIN
core_practice p ON s.practice_id=p.id
LEFT JOIN
core_adoption ad ON ad.farmer_id = ofp.id AND ad.advisory_id = r.advisory_id
"""
@st.cache_data
def cached_data(query):
return fetch_data(query)
df = cached_data(query)
df['created_at'] = pd.to_datetime(df['created_at'])
min_created_at = df['created_at'].min()
max_created_at = df['created_at'].max()
col1, col2 = st.columns(2)
with col1:
start_date = st.date_input("Start Date", min_value=min_created_at, max_value=max_created_at, value=min_created_at)
with col2:
end_date = st.date_input("End Date", min_value=min_created_at, max_value=max_created_at, value=max_created_at)
if end_date < start_date:
st.error("End Date must be greater than Start Date.")
#Region
unique_regions = df.groupby(['region_id', 'region_name']).size().reset_index(name='count')
unique_regions = unique_regions.drop(columns=['count'])
region_list = unique_regions.to_dict(orient='records')
# print("Unique Regions:", region_list)
#Zone
unique_zones=df.groupby(['region_id','zone_id', 'zone_name']).size().reset_index(name='count')
unique_zones = unique_zones.drop(columns=['count'])
zone_list = unique_zones.to_dict(orient='records')
# print("Unique Zones:", zone_list)
#Woreda
unique_woreda=df.groupby(['zone_id','woreda_id', 'woreda_name']).size().reset_index(name='count')
unique_woreda = unique_woreda.drop(columns=['count'])
woreda_list = unique_woreda.to_dict(orient='records')
# print("Unique Woreda:", len(woreda_list))
#Kebele
unique_kebele=df.groupby(['woreda_id','kebele_id', 'kebele_name']).size().reset_index(name='count')
unique_kebele = unique_kebele.drop(columns=['count'])
kebele_list = unique_kebele.to_dict(orient='records')
# print("Unique Kebele:", len(kebele_list))
#DA
unique_da=df.groupby(['da_id', 'da_name']).size().reset_index(name='count')
unique_da = unique_da.drop(columns=['count'])
da_list = unique_da.to_dict(orient='records')
# print("Unique DA:", len(da_list))
#Advisory
unique_advisory=df.groupby(['advisory_id', 'advisory_label']).size().reset_index(name='count')
unique_advisory = unique_advisory.drop(columns=['count'])
advisory_list = unique_advisory.to_dict(orient='records')
# print("Unique advisory:", len(advisory_list))
#Practice
unique_practice=df.groupby(['practice_id', 'practice_name']).size().reset_index(name='count')
unique_practice = unique_practice.drop(columns=['count'])
practice_list = unique_practice.to_dict(orient='records')
# print("Unique practice:", len(practice_list))
placeholders = {
"Region": "Select Region",
"Zone": "Select Zone",
"Woreda": "Select Woreda",
"Kebele": "Select Kebele",
"DA": "Select DA",
"Advisory": "Select Advisory",
"Practice": "Select Practice"
}
filters = {
"Location": region_list,
"Zone": zone_list,
"Woreda": woreda_list,
"Kebele": kebele_list,
"DA": da_list,
"Advisory": advisory_list,
"Practice": practice_list
}
col1, col2, col3, col4 = st.columns(4)
with col1:
selected_region = st.selectbox("Select Region", options=[placeholders["Region"]] + [region['region_name'] for region in filters['Location']])
with col2:
selected_zone = st.selectbox("Select Zone", options=[placeholders["Zone"]] + [zone['zone_name'] for zone in filters['Zone']])
with col3:
selected_woreda = st.selectbox("Select Woreda", options=[placeholders["Woreda"]] + [woreda['woreda_name'] for woreda in filters['Woreda']])
with col4:
selected_kebele = st.selectbox("Select Kebele", options=[placeholders["Kebele"]] + [kebele['kebele_name'] for kebele in filters['Kebele']])
col5, col6, col7 = st.columns(3)
with col5:
selected_da = st.selectbox("Select DA", options=[placeholders["DA"]] + [da['da_name'] for da in filters['DA']])
with col6:
selected_advisory = st.selectbox("Select Advisory", options=[placeholders["Advisory"]] + [advisory['advisory_label'] for advisory in filters['Advisory']])
with col7:
selected_practice = st.selectbox("Select Practice", options=[placeholders["Practice"]] + [practice['practice_name'] for practice in filters['Practice']])
# selected_segregation = st.selectbox("Select Segregation Type", ("Region", "Zone", "Woreda", "Kebele"))
# Function to calculate reach & adoption counts for male and female genders
def calculate_reach_counts(data):
############# Reach ##############
total_reach_count = data['reach_id'].nunique()
male_reach_count = data[data['gender'] == 'male']['reach_id'].nunique()
female_reach_count= data[data['gender'] == 'female']['reach_id'].nunique()
############ Adoption ############
total_adoption_count = data['adoption_id'].nunique()
male_adoption_count = data[data['gender'] == 'male']['adoption_id'].nunique()
female_adoption_count= data[data['gender'] == 'female']['adoption_id'].nunique()
############### DA ##############
male_das = data[data['da_gender'] == 'Male']['da_id'].nunique()
female_das = data[data['da_gender'] == 'Female']['da_id'].nunique()
total_das = data['da_id'].nunique()
practice_counts = {}
unique_practices = data['practice_name'].unique()
for practice in unique_practices:
practice_df = data[data['practice_name'] == practice]
male_reach_count_practice = practice_df[(practice_df['gender'] == 'male') & (practice_df['reach_id'])]['reach_id'].nunique()
female_reach_count_practice = practice_df[(practice_df['gender'] == 'female') & (practice_df['reach_id'])]['reach_id'].nunique()
male_adoption_count_practice = practice_df[(practice_df['gender'] == 'male') & (practice_df['adoption_id'])]['adoption_id'].nunique()
female_adoption_count_practice = practice_df[(practice_df['gender'] == 'female') & (practice_df['adoption_id'])]['adoption_id'].nunique()
practice_counts[practice] = {
'male_reach_count_by_practice': male_reach_count_practice,
'female_reach_count_by_practice': female_reach_count_practice,
'male_adoption_count_by_practice': male_adoption_count_practice,
'female_adoption_count_by_practice': female_adoption_count_practice
}
advisory_counts = {}
unique_advisories = data['advisory_label'].unique()
for advisory in unique_advisories:
advisory_df = data[data['advisory_label'] == advisory]
male_reach_count_advisory = len(advisory_df[(advisory_df['gender'] == 'male') & (~advisory_df['reach_id'].isnull())]['reach_id'].unique())
female_reach_count_advisory = len(advisory_df[(advisory_df['gender'] == 'female') & (~advisory_df['reach_id'].isnull())]['reach_id'].unique())
male_adoption_count_advisory = len(advisory_df[(advisory_df['gender'] == 'male') & (~advisory_df['adoption_id'].isnull())]['adoption_id'].unique())
female_adoption_count_advisory = len(advisory_df[(advisory_df['gender'] == 'female') & (~advisory_df['adoption_id'].isnull())]['adoption_id'].unique())
advisory_counts[advisory] = {
'male_reach_count': male_reach_count_advisory,
'female_reach_count': female_reach_count_advisory,
'male_adoption_count': male_adoption_count_advisory,
'female_adoption_count': female_adoption_count_advisory
}
total_male_farmers = len(data[data['gender'] == 'male'])
total_female_farmers = len(data[data['gender'] == 'female'])
data['created_date'] = data['created_at'].dt.date
male_reach_counts_created_at = data[data['gender'] == 'male'].groupby('created_date')['reach_id'].nunique()
female_reach_counts_created_at = data[data['gender'] == 'female'].groupby('created_date')['reach_id'].nunique()
male_adoption_counts_created_at = data[data['gender'] == 'male'].groupby('created_date')['adoption_id'].nunique()
female_adoption_counts_created_at = data[data['gender'] == 'female'].groupby('created_date')['adoption_id'].nunique()
time_based_counts={
"male_reach_counts_created_at":male_reach_counts_created_at,
"female_reach_counts_created_at":female_reach_counts_created_at,
"male_adoption_counts_created_at":male_adoption_counts_created_at,
"female_adoption_counts_created_at":female_adoption_counts_created_at
}
unique_regions = data.groupby(['region_id', 'region_name']).size().reset_index(name='count')
region_counts = {}
for _, region_row in unique_regions.iterrows():
region_id = region_row['region_id']
region_name = region_row['region_name']
region_data = data[data['region_id'] == region_id]
male_reach_count_by_region = region_data[region_data['gender'] == 'male']['reach_id'].nunique()
female_reach_count_by_region = region_data[region_data['gender'] == 'female']['reach_id'].nunique()
male_adoption_count_by_region = region_data[region_data['gender'] == 'male']['adoption_id'].nunique()
female_adoption_count_by_region = region_data[region_data['gender'] == 'female']['adoption_id'].nunique()
region_counts[region_name] = {
"male_reach_count_by_region": male_reach_count_by_region,
"female_reach_count_by_region": female_reach_count_by_region,
"male_adoption_count_by_region": male_adoption_count_by_region,
"female_adoption_count_by_region": female_adoption_count_by_region
}
unique_zones = data.groupby(['zone_id', 'zone_name']).size().reset_index(name='count')
zone_counts = {}
for _, zone_row in unique_zones.iterrows():
zone_id = zone_row['zone_id']
zone_name = zone_row['zone_name']
zone_data = data[data['zone_id'] == zone_id]
male_reach_count_by_zone = zone_data[zone_data['gender'] == 'male']['reach_id'].nunique()
female_reach_count_by_zone = zone_data[zone_data['gender'] == 'female']['reach_id'].nunique()
male_adoption_count_by_zone = zone_data[zone_data['gender'] == 'male']['adoption_id'].nunique()
female_adoption_count_by_zone = zone_data[zone_data['gender'] == 'female']['adoption_id'].nunique()
zone_counts[zone_name] = {
"male_reach_count_by_zone": male_reach_count_by_zone,
"female_reach_count_by_zone": female_reach_count_by_zone,
"male_adoption_count_by_zone": male_adoption_count_by_zone,
"female_adoption_count_by_zone": female_adoption_count_by_zone
}
unique_woredas = data.groupby(['woreda_id', 'woreda_name']).size().reset_index(name='count')
woreda_counts = {}
for _, woreda_row in unique_woredas.iterrows():
woreda_id = woreda_row['woreda_id']
woreda_name = woreda_row['woreda_name']
woreda_data = data[data['woreda_id'] == woreda_id]
male_reach_count_by_woreda = woreda_data[woreda_data['gender'] == 'male']['reach_id'].nunique()
female_reach_count_by_woreda = woreda_data[woreda_data['gender'] == 'female']['reach_id'].nunique()
male_adoption_count_by_woreda = woreda_data[woreda_data['gender'] == 'male']['adoption_id'].nunique()
female_adoption_count_by_woreda = woreda_data[woreda_data['gender'] == 'female']['adoption_id'].nunique()
woreda_counts[woreda_name] = {
"male_reach_count_by_woreda": male_reach_count_by_woreda,
"female_reach_count_by_woreda": female_reach_count_by_woreda,
"male_adoption_count_by_woreda": male_adoption_count_by_woreda,
"female_adoption_count_by_woreda": female_adoption_count_by_woreda
}
# Get unique kebeles
unique_kebeles = data.groupby(['kebele_id', 'kebele_name']).size().reset_index(name='count')
kebele_counts = {}
for _, kebele_row in unique_kebeles.iterrows():
kebele_id = kebele_row['kebele_id']
kebele_name = kebele_row['kebele_name']
kebele_data = data[data['kebele_id'] == kebele_id]
male_reach_count_by_kebele = kebele_data[kebele_data['gender'] == 'male']['reach_id'].nunique()
female_reach_count_by_kebele = kebele_data[kebele_data['gender'] == 'female']['reach_id'].nunique()
male_adoption_count_by_kebele = kebele_data[kebele_data['gender'] == 'male']['adoption_id'].nunique()
female_adoption_count_by_kebele = kebele_data[kebele_data['gender'] == 'female']['adoption_id'].nunique()
kebele_counts[kebele_name] = {
"male_reach_count_by_kebele": male_reach_count_by_kebele,
"female_reach_count_by_kebele": female_reach_count_by_kebele,
"male_adoption_count_by_kebele": male_adoption_count_by_kebele,
"female_adoption_count_by_kebele": female_adoption_count_by_kebele
}
return {
"total_reach_count": total_reach_count,
"male_reach_count": male_reach_count,
"female_reach_count": female_reach_count,
"male_adoption_count": male_adoption_count,
"female_adoption_count": female_adoption_count,
"total_adoption_count": total_adoption_count,
"male_das":male_das,
"female_das":female_das,
"total_das":total_das,
"practice_counts": practice_counts,
"advisory_counts": advisory_counts,
"total_male_farmers": total_male_farmers,
"total_female_farmers": total_female_farmers,
"time_based_counts":time_based_counts,
"zone_counts":zone_counts,
"woreda_counts":woreda_counts,
"kebele_counts":kebele_counts,
"region_counts":region_counts
}
reach_and_adoption_counts= calculate_reach_counts(df)
# Function to get the region ID based on the region name
def get_region_id(region_name):
for region in region_list:
if region['region_name'] == region_name:
return region['region_id']
return None
def get_zone_id(zone_name):
for zone in zone_list:
if zone['zone_name'] == zone_name:
return zone['zone_id']
return None
def get_woreda_id(woreda_name):
for woreda in woreda_list:
if woreda['woreda_name'] == woreda_name:
return woreda['woreda_id']
return None
def get_zones_for_region(selected_region_id):
zones_for_selected_region = [zone['zone_name']for zone in zone_list if zone['region_id'] == selected_region_id]
return zones_for_selected_region
def get_woreda_for_zone(selected_zone_id):
woredas_for_selected_zone = [woreda['woreda_name']for woreda in woreda_list if woreda['zone_id'] == selected_zone_id]
return woredas_for_selected_zone
def get_kebele_for_woreda(selected_woreda_id):
kebele_for_selected_woreda = [kebele['kebele_name']for kebele in kebele_list if kebele['woreda_id'] == selected_woreda_id]
return kebele_for_selected_woreda
# Apply filters
if selected_region or selected_zone or selected_woreda or selected_kebele or selected_da or selected_advisory or selected_practice or (start_date and end_date):
filtered_df = df.copy()
if selected_region and selected_region != placeholders["Region"]:
filtered_df = filtered_df[filtered_df['region_name'] == selected_region]
selected_region_id = get_region_id(selected_region)
zones_under_selected_region=get_zones_for_region(selected_region_id)
# print("zones_under_selected_region---->",zones_under_selected_region)
if selected_zone and selected_zone != placeholders["Zone"]:
filtered_df = filtered_df[filtered_df['zone_name'] == selected_zone]
selected_zone_id = get_zone_id(selected_zone)
woreda_under_selected_zone=get_woreda_for_zone(selected_zone_id)
# print("woreda_under_selected_zone---->",woreda_under_selected_zone)
if selected_woreda and selected_woreda != placeholders["Woreda"]:
filtered_df = filtered_df[filtered_df['woreda_name'] == selected_woreda]
selected_woreda_id = get_woreda_id(selected_woreda)
kebele_under_selected_woreda=get_kebele_for_woreda(selected_woreda_id)
# print("kebele_under_selected_woreda---->",kebele_under_selected_woreda)
if selected_kebele and selected_kebele != placeholders["Kebele"]:
filtered_df = filtered_df[filtered_df['kebele_name'] == selected_kebele]
if selected_da and selected_da != placeholders["DA"]:
filtered_df = filtered_df[filtered_df['da_name'] == selected_da]
if selected_advisory and selected_advisory != placeholders["Advisory"]:
filtered_df = filtered_df[filtered_df['advisory_label'] == selected_advisory]
if selected_practice and selected_practice != placeholders["Practice"]:
filtered_df = filtered_df[filtered_df['practice_name'] == selected_practice]
if start_date and end_date :
if start_date <= end_date:
filtered_df = filtered_df[(filtered_df['created_at'].dt.date >= start_date) & (filtered_df['created_at'].dt.date <= end_date)]
reach_and_adoption_counts= calculate_reach_counts(filtered_df)
else:
reach_and_adoption_counts=reach_and_adoption_counts
data = reach_and_adoption_counts["time_based_counts"]
df_male = pd.DataFrame(data.get("male_reach_counts_created_at", {}).items(), columns=["Date", "Male Count"])
df_female = pd.DataFrame(data.get("female_reach_counts_created_at", {}).items(), columns=["Date", "Female Count"])
st.subheader("Male vs Female Reach Counts Over Time")
if df_male.empty:
df_male = pd.DataFrame(columns=["Date", "Male Count"])
if df_female.empty:
df_female = pd.DataFrame(columns=["Date", "Female Count"])
if df_male.empty and df_female.empty:
st.warning("No data available for adoption counts over time.")
else:
df_data_reach = pd.merge(df_male, df_female, on="Date", how="outer")
df_data_reach.fillna(0, inplace=True)
with st.container(border=True):
fig = px.line(df_data_reach, x="Date", y=["Male Count", "Female Count"], title="Male vs Female Reach Counts Over Time")
fig.update_xaxes(title_text="Date")
fig.update_yaxes(title_text="Reach Count")
fig.update_layout(xaxis=dict(range=[min_created_at, max_created_at]))
st.plotly_chart(fig, use_container_width=True)
data = reach_and_adoption_counts["time_based_counts"]
df_male = pd.DataFrame(data.get("male_adoption_counts_created_at", {}).items(), columns=["Date", "Male Count"])
df_female = pd.DataFrame(data.get("female_adoption_counts_created_at", {}).items(), columns=["Date", "Female Count"])
st.subheader("Male vs Female Adoption Counts Over Time")
if df_male.empty:
df_male = pd.DataFrame(columns=["Date", "Male Count"])
if df_female.empty:
df_female = pd.DataFrame(columns=["Date", "Female Count"])
if df_male.empty and df_female.empty:
st.warning("No data available for adoption counts over time.")
else:
df_data = pd.merge(df_male, df_female, on="Date", how="outer")
df_data.fillna(0, inplace=True)
with st.container(border=True):
fig = px.line(df_data, x="Date", y=["Male Count", "Female Count"], title="Male vs Female Adoption Counts Over Time")
fig.update_xaxes(title_text="Date")
fig.update_yaxes(title_text="Adoption Count")
fig.update_layout(xaxis=dict(range=[min_created_at, max_created_at]))
st.plotly_chart(fig, use_container_width=True)
col1, col2 = st.columns(2)
########################### Reach Counts ##########################################
with col1:
st.subheader("Reach Counts")
with st.container(border=True):
reach_data = {
'Gender': [],
'Reach Count': []
}
male_reach_count = reach_and_adoption_counts.get('male_reach_count', 0)
female_reach_count = reach_and_adoption_counts.get('female_reach_count', 0)
if male_reach_count > 0:
reach_data['Gender'].append(f"Male - {male_reach_count}")
reach_data['Reach Count'].append(male_reach_count)
if female_reach_count > 0:
reach_data['Gender'].append(f"Female - {female_reach_count}")
reach_data['Reach Count'].append(female_reach_count)
reach_df = pd.DataFrame(reach_data)
if not reach_df.empty:
reach_fig = px.pie(reach_df, values='Reach Count', names='Gender')
# reach_fig.update_traces(textposition='inside', textinfo='percent')
st.plotly_chart(reach_fig,use_container_width=True)
else:
st.warning("No reach data available.")
with col2:
st.subheader("Adoption Counts")
with st.container(border=True):
adoption_data = {
'Gender': [],
'Adoption Count': []
}
male_adoption_count = reach_and_adoption_counts.get('male_adoption_count', 0)
female_adoption_count = reach_and_adoption_counts.get('female_adoption_count', 0)
if male_adoption_count > 0:
adoption_data['Gender'].append(f"Male - {male_adoption_count}")
adoption_data['Adoption Count'].append(male_adoption_count)
if female_adoption_count > 0:
adoption_data['Gender'].append(f"Female - {female_adoption_count}")
adoption_data['Adoption Count'].append(female_adoption_count)
adoption_df = pd.DataFrame(adoption_data)
if not adoption_df.empty:
adoption_fig = px.pie(adoption_df, values='Adoption Count', names='Gender')
# adoption_fig.update_traces(textposition='inside', textinfo='percent')
st.plotly_chart(adoption_fig,use_container_width=True)
else:
st.warning("No adoption data available.")
############################## Development Agent distribution ############################################
das_df = pd.DataFrame({
'Gender': ['Male', 'Female'],
'Count': [reach_and_adoption_counts["male_das"], reach_and_adoption_counts["female_das"]]
})
das_df['Gender'] = das_df.apply(lambda x: f"{x['Gender']} - {x['Count']}", axis=1)
with col1:
st.subheader("Distribution of Development Agents by Gender")
with st.container(border=True):
if das_df['Count'].sum() > 0:
fig_das = px.pie(das_df, values='Count', names='Gender')
st.plotly_chart(fig_das, use_container_width=True)
else:
st.warning("No data available for development agent distribution.")
#################################### Farmers distribution #######################################
farmers_df = pd.DataFrame({
'Gender': ['Male', 'Female'],
'Count': [reach_and_adoption_counts["total_male_farmers"], reach_and_adoption_counts["total_female_farmers"]]
})
farmers_df['Gender'] = farmers_df.apply(lambda x: f"{x['Gender']} - {x['Count']}", axis=1)
with col2:
st.subheader("Distribution of Farmers by Gender")
with st.container(border=True):
if farmers_df['Count'].sum() > 0:
fig_farmers = px.pie(farmers_df, values='Count', names='Gender')
st.plotly_chart(fig_farmers, use_container_width=True)
else:
st.warning("No data available for farmer distribution.")
############################### practice ###########################################
practice_df = pd.DataFrame(reach_and_adoption_counts.get("practice_counts", {})).transpose()
if len(practice_df):
practice_df = practice_df.reset_index().rename(columns={'index': 'Practice'})
else:
practice_df = None
st.subheader("Practice Reach and Adoption Counts")
with st.container(border=True):
if practice_df is not None:
reach_fig = px.bar(practice_df, x='Practice', y=['male_reach_count_by_practice', 'female_reach_count_by_practice'], barmode='stack', title='Reach Counts by Practice', labels={'value': 'Count', 'variable': 'Gender'})
st.plotly_chart(reach_fig, use_container_width=True)
else:
st.warning("No data available for the selected filter.")
with st.container(border=True):
if practice_df is not None:
adoption_fig = px.bar(practice_df, x='Practice', y=['male_adoption_count_by_practice', 'female_adoption_count_by_practice'], barmode='stack', title='Adoption Counts by Practice', labels={'value': 'Count', 'variable': 'Gender'})
st.plotly_chart(adoption_fig, use_container_width=True)
else:
st.warning("No data available for the selected filter.")
############################## Advisory ###################################################
advisory_df = pd.DataFrame(reach_and_adoption_counts.get("advisory_counts", {})).transpose()
if len(advisory_df):
advisory_df = advisory_df.reset_index().rename(columns={'index': 'Advisory'})
else:
advisory_df = None
st.subheader("Reach Counts by Advisory")
with st.container(border=True):
if advisory_df is not None:
reach_fig = px.bar(advisory_df, x='Advisory', y=['male_reach_count', 'female_reach_count'], barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
st.plotly_chart(reach_fig, use_container_width=True)
else:
st.warning("No data available for selected filter.")
st.subheader("Adoption Counts by Advisory")
with st.container(border=True):
if advisory_df is not None:
adoption_fig = px.bar(advisory_df, x='Advisory', y=['male_adoption_count', 'female_adoption_count'], barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
st.plotly_chart(adoption_fig, use_container_width=True)
else:
st.warning("No data available for selected filter.")
######################################## Region ######################################
# if (selected_region == placeholders["Region"] and
# selected_zone == placeholders["Zone"] and
# selected_woreda == placeholders["Woreda"] and
# selected_kebele == placeholders["Kebele"]):
# region_df = pd.DataFrame(reach_and_adoption_counts.get("region_counts", {})).transpose()
# if not region_df.empty:
# if len(region_df):
# region_df = region_df.reset_index().rename(columns={'index': 'Region'})
# col1, col2 = st.columns(2)
# with col1:
# st.subheader("Reach Counts by Region")
# with st.container(border=True):
# reach_fig = px.bar(region_df, x="Region", y=['male_reach_count_by_region', 'female_reach_count_by_region'],
# barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
# reach_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
# xaxis_rangeslider_visible=True)
# st.plotly_chart(reach_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
# with col2:
# st.subheader("Adoption Counts by Region")
# with st.container(border=True):
# adoption_fig = px.bar(region_df, x="Region", y=['male_adoption_count_by_region', 'female_adoption_count_by_region'],
# barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
# adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
# xaxis_rangeslider_visible=True)
# st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
# else:
# st.warning("No data available for regions.")
# if selected_region != placeholders["Region"] and selected_region :
# zone_df = pd.DataFrame(reach_and_adoption_counts.get("zone_counts", {})).transpose()
# if not zone_df.empty:
# if len(zone_df):
# zone_df = zone_df.reset_index().rename(columns={'index': 'Zone'})
# col1, col2 = st.columns(2)
# with col1:
# st.subheader("Reach Counts by Zone")
# with st.container(border=True):
# reach_fig = px.bar(zone_df, x="Zone", y=['male_reach_count_by_zone', 'female_reach_count_by_zone'],
# barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
# reach_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
# xaxis_rangeslider_visible=True)
# st.plotly_chart(reach_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
# with col2:
# st.subheader("Adoption Counts by Zone")
# with st.container(border=True):
# adoption_fig = px.bar(zone_df, x="Zone", y=['male_adoption_count_by_zone', 'female_adoption_count_by_zone'],
# barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
# adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
# xaxis_rangeslider_visible=True)
# st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
# else:
# st.warning("No data available for zones.")
# if selected_zone != placeholders["Zone"] and selected_zone :
# woreda_df = pd.DataFrame(reach_and_adoption_counts.get("woreda_counts", {})).transpose()
# if not woreda_df.empty:
# if len(woreda_df):
# woreda_df = woreda_df.reset_index().rename(columns={'index': 'Woreda'})
# col1, col2 = st.columns(2)
# with col1:
# st.subheader("Reach Counts by Woreda")
# with st.container(border=True):
# reach_fig = px.bar(woreda_df, x="Woreda", y=['male_reach_count_by_woreda', 'female_reach_count_by_woreda'],
# barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
# reach_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
# xaxis_rangeslider_visible=True)
# st.plotly_chart(reach_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
# with col2:
# st.subheader("Adoption Counts by Woreda")
# with st.container(border=True):
# adoption_fig = px.bar(woreda_df, x="Woreda", y=['male_adoption_count_by_woreda', 'female_adoption_count_by_woreda'],
# barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
# adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
# xaxis_rangeslider_visible=True)
# st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
# else:
# st.warning("No data available for woredas.")
# if selected_woreda != placeholders["Woreda"] and selected_woreda :
# kebele_df = pd.DataFrame(reach_and_adoption_counts.get("kebele_counts", {})).transpose()
# if not kebele_df.empty:
# col1, col2 = st.columns(2)
# if len(kebele_df):
# kebele_df = kebele_df.reset_index().rename(columns={'index': 'Kebele'})
# with col1:
# st.subheader("Reach Counts by Kebele")
# with st.container(border=True):
# reach_fig = px.bar(kebele_df, x="Kebele", y=['male_reach_count_by_kebele', 'female_reach_count_by_kebele'],
# barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
# reach_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
# xaxis_rangeslider_visible=True)
# st.plotly_chart(reach_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
# with col2:
# st.subheader("Adoption Counts by Kebele")
# with st.container(border=True):
# adoption_fig = px.bar(kebele_df, x="Kebele", y=['male_adoption_count_by_kebele', 'female_adoption_count_by_kebele'],
# barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
# adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
# xaxis_rangeslider_visible=True)
# st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
# else:
# st.warning("No data available for kebeles.")
# if selected_kebele != placeholders["Kebele"] and selected_kebele :
# kebele_df = pd.DataFrame(reach_and_adoption_counts.get("kebele_counts", {})).transpose()
# if not kebele_df.empty:
# col1, col2 = st.columns(2)
# if len(kebele_df):
# kebele_df = kebele_df.reset_index().rename(columns={'index': 'Kebele'})
# with col1:
# st.subheader("Reach Counts by Kebele")
# with st.container(border=True):
# reach_fig = px.bar(kebele_df, x="Kebele", y=['male_reach_count_by_kebele', 'female_reach_count_by_kebele'],
# barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
# adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
# xaxis_rangeslider_visible=True)
# st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
# with col2:
# st.subheader("Adoption Counts by Kebele")
# with st.container(border=True):
# adoption_fig = px.bar(kebele_df, x="Kebele", y=['male_adoption_count_by_kebele', 'female_adoption_count_by_kebele'],
# barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
# adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
# xaxis_rangeslider_visible=True)
# st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
# else:
# st.warning("No data available for kebeles.")
#############
if (selected_region == placeholders["Region"] and
selected_zone == placeholders["Zone"] and
selected_woreda == placeholders["Woreda"] and
selected_kebele == placeholders["Kebele"]):
region_df = pd.DataFrame(reach_and_adoption_counts.get("region_counts", {})).transpose()
if not region_df.empty:
if len(region_df):
region_df = region_df.reset_index().rename(columns={'index': 'Region'})
col1, col2 = st.columns(2)
with col1:
st.subheader("Reach Counts by Region")
with st.container(border=True):
reach_fig = px.bar(region_df, x="Region", y=['male_reach_count_by_region', 'female_reach_count_by_region'],
barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
reach_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
xaxis_rangeslider_visible=True)
st.plotly_chart(reach_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
with col2:
st.subheader("Adoption Counts by Region")
with st.container(border=True):
adoption_fig = px.bar(region_df, x="Region", y=['male_adoption_count_by_region', 'female_adoption_count_by_region'],
barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
xaxis_rangeslider_visible=True)
st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
else:
st.warning("No data available for regions.")
elif selected_kebele != placeholders["Kebele"] and selected_kebele :
kebele_df = pd.DataFrame(reach_and_adoption_counts.get("kebele_counts", {})).transpose()
if not kebele_df.empty:
col1, col2 = st.columns(2)
if len(kebele_df):
kebele_df = kebele_df.reset_index().rename(columns={'index': 'Kebele'})
with col1:
st.subheader("Reach Counts by Kebele")
with st.container(border=True):
reach_fig = px.bar(kebele_df, x="Kebele", y=['male_reach_count_by_kebele', 'female_reach_count_by_kebele'],
barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
xaxis_rangeslider_visible=True)
st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
with col2:
st.subheader("Adoption Counts by Kebele")
with st.container(border=True):
adoption_fig = px.bar(kebele_df, x="Kebele", y=['male_adoption_count_by_kebele', 'female_adoption_count_by_kebele'],
barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
xaxis_rangeslider_visible=True)
st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
else:
st.warning("No data available for kebeles.")
elif selected_woreda != placeholders["Woreda"] and selected_woreda :
kebele_df = pd.DataFrame(reach_and_adoption_counts.get("kebele_counts", {})).transpose()
if not kebele_df.empty:
col1, col2 = st.columns(2)
if len(kebele_df):
kebele_df = kebele_df.reset_index().rename(columns={'index': 'Kebele'})
with col1:
st.subheader("Reach Counts by Kebele")
with st.container(border=True):
reach_fig = px.bar(kebele_df, x="Kebele", y=['male_reach_count_by_kebele', 'female_reach_count_by_kebele'],
barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
reach_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
xaxis_rangeslider_visible=True)
st.plotly_chart(reach_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
with col2:
st.subheader("Adoption Counts by Kebele")
with st.container(border=True):
adoption_fig = px.bar(kebele_df, x="Kebele", y=['male_adoption_count_by_kebele', 'female_adoption_count_by_kebele'],
barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
xaxis_rangeslider_visible=True)
st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
else:
st.warning("No data available for kebeles.")
elif selected_zone != placeholders["Zone"] and selected_zone :
woreda_df = pd.DataFrame(reach_and_adoption_counts.get("woreda_counts", {})).transpose()
if not woreda_df.empty:
if len(woreda_df):
woreda_df = woreda_df.reset_index().rename(columns={'index': 'Woreda'})
col1, col2 = st.columns(2)
with col1:
st.subheader("Reach Counts by Woreda")
with st.container(border=True):
reach_fig = px.bar(woreda_df, x="Woreda", y=['male_reach_count_by_woreda', 'female_reach_count_by_woreda'],
barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
reach_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
xaxis_rangeslider_visible=True)
st.plotly_chart(reach_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
with col2:
st.subheader("Adoption Counts by Woreda")
with st.container(border=True):
adoption_fig = px.bar(woreda_df, x="Woreda", y=['male_adoption_count_by_woreda', 'female_adoption_count_by_woreda'],
barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
xaxis_rangeslider_visible=True)
st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
else:
st.warning("No data available for woredas.")
elif selected_region != placeholders["Region"] and selected_region :
zone_df = pd.DataFrame(reach_and_adoption_counts.get("zone_counts", {})).transpose()
if not zone_df.empty:
if len(zone_df):
zone_df = zone_df.reset_index().rename(columns={'index': 'Zone'})
col1, col2 = st.columns(2)
with col1:
st.subheader("Reach Counts by Zone")
with st.container(border=True):
reach_fig = px.bar(zone_df, x="Zone", y=['male_reach_count_by_zone', 'female_reach_count_by_zone'],
barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
reach_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
xaxis_rangeslider_visible=True)
st.plotly_chart(reach_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
with col2:
st.subheader("Adoption Counts by Zone")
with st.container(border=True):
adoption_fig = px.bar(zone_df, x="Zone", y=['male_adoption_count_by_zone', 'female_adoption_count_by_zone'],
barmode='stack', labels={'value': 'Count', 'variable': 'Gender'})
adoption_fig.update_layout(xaxis=dict(type='category', categoryorder='category ascending'),
xaxis_rangeslider_visible=True)
st.plotly_chart(adoption_fig, use_container_width=True, style={'width': '100%', 'overflow-x': 'auto'})
else:
st.warning("No data available for zones.")