File size: 28,982 Bytes
e433a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import streamlit as st
import pandas as pd
from pg_utils_fn import create_entity_name_list, fetch_block_mapping, fetch_gp_mapping
from pg_utils_fn import fetch_district_mapping
from pg_utils_fn import populate_entity_mapping
from pg_utils_fn import load_file
from utils import update_all_data
from pg_utils_fn import create_village_mapped_dataset, populate_village_mapping, process_file
from pg_utils_fn import create_sub_district_mapped_dataset, populate_sub_district_mapping
from pg_utils_fn import create_gp_mapped_dataset, populate_gp_mapping
from pg_utils_fn import create_block_mapped_dataset, populate_block_mapping
from pg_utils_fn import generate_download_link
from pg_utils_fn import update_variations
from pg_utils_fn import create_district_mapped_dataset
from pg_utils_fn import create_mapped_dataset, get_state_mappings
from databasefn import insert_record


# Set Streamlit configuration
st.set_option('deprecation.showfileUploaderEncoding', False)
def home_page():

    # Add an attractive header
    st.title('CodeYatra')
    #st.image('mandala.jpg')  # Replace 'your_logo.png' with the path to your logo image

    # Write a brief and engaging introduction
    st.write(
        "Welcome to the CodeYatra LGD Mapping App! 🌟 This app makes mapping a breeze, allowing "
        "you to explore states, districts, sub-districts, blocks, gram panchayats, and villages LGD Mapping with ease. "
        "Whether you're a researcher πŸ‘©β€πŸ”¬, a data analyst πŸ“Š, or simply curious about India's geography, this app is "
        "the perfect resource for you. Say goodbye to tedious manual searches and say hello to accurate and "
        "up-to-date mapping at your fingertips. Let's embark on an exciting journey of exploration and "
        "discovery with the CodeYatra LGD Code Mapping App! πŸ—ΊοΈπŸš€"
    )


    st.warning("⚠️ **Important Notice:** ")
    st.write(" To ensure smooth operation of the application, please update the column names as follows:")
    st.write("- State: Change the state column name to 'state_name'")
    st.write("- District: Change the district column name to 'district_name'")
    st.write("- Sub District: Change the sub district column name to 'sub_district_name'")
    st.write("- Block: Change the block column name to 'block_name'")
    st.write("- Gram Panchayat (GP): Change the gp column name to 'gp_name'")
    st.write("- Village: Change the village column name to 'village_name'")



    # Add visually appealing images or illustrations to showcase the app's features
    #st.image('your_app_features.png', use_column_width=True)  # Replace 'your_app_features.png' with the path to your app features image

    st.subheader('Start Mapping Now')
    st.write('Ready to explore and map LGD codes? Click the button below to get started.')
    

    if st.button('Start Mapping', key='sub_mapping_button'):
        #redirect_to_subset_dataset_page()
        redirect_to_state_mapping_page()


def redirect_to_update_dataset_page():
    # Modify the URL parameters to navigate to the state mapping page
    state_mapping_url = st.experimental_set_query_params(page='update')
    page_route()
    st.experimental_rerun()
def redirect_to_subset_dataset_page():
    # Modify the URL parameters to navigate to the state mapping page
    state_mapping_url = st.experimental_set_query_params(page='subset')
    page_route()
    st.experimental_rerun()
def redirect_to_state_mapping_page():
    # Modify the URL parameters to navigate to the state mapping page
    state_mapping_url = st.experimental_set_query_params(page='state')
    page_route()
    st.experimental_rerun()

def redirect_to_district_page():
    # Modify the URL parameters to navigate to the district page
    district_url = st.experimental_set_query_params(page='district')
    page_route()
    st.experimental_rerun()
def redirect_to_block_page():
    # Modify the URL parameters to navigate to the block page
    block_url = st.experimental_set_query_params(page='block')
    page_route()
    st.experimental_rerun()

def redirect_to_panchayat_page():
    # Modify the URL parameters to navigate to the block page
    panchayat_url = st.experimental_set_query_params(page='panchayat')
    page_route()
    st.experimental_rerun()

def redirect_to_village_page():
    # Modify the URL parameters to navigate to the block page
    panchayat_url = st.experimental_set_query_params(page='village')
    page_route()
    st.experimental_rerun()
def redirect_to_sub_district_page():
    # Modify the URL parameters to navigate to the district page
    subdistrict_url = st.experimental_set_query_params(page='subdistrict')
    page_route()
    st.experimental_rerun()


def page_route():
    query_params = st.experimental_get_query_params()
    if "page" in query_params and query_params["page"][0] == "state":
        state_mapping_page()
    elif "page" in query_params and query_params["page"][0] == "subset":
        subset_page()
    elif "page" in query_params and query_params["page"][0] == "update":
        update_data()
    elif "page" in query_params and query_params["page"][0] == "district":
        district_page()
    elif "page" in query_params and query_params["page"][0] == "block":
        block_page()
    elif "page" in query_params and query_params["page"][0] == "panchayat":
        gp_page()
    elif "page" in query_params and query_params["page"][0] == "subdistrict":
        sub_district_page()
    elif "page" in query_params and query_params["page"][0] == "village":
        village_page()
    elif "page" in query_params and query_params["page"][0] == "insertRecord":
        pass
        #insert_record()
    else:
        home_page()

# Main app logic
def main():
    st.set_page_config(
        page_title="CodeYatra",
        page_icon="🌟",
        layout="centered",
        initial_sidebar_state="expanded",
    )
    page_route()

def update_data():
     st.title("Update the Corpus of LGD codes")
     if st.button('Update Corpus', key='corpus_button'):   
        update_all_data()


#sub set
def subset_page():
    st.title("Select Specific Columns from CSV/Excel File")
    
    # File uploader
    file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
    
    if file is not None:
        # Process the file
        
        df = process_file(file)
        if df is not None:
            # Display the processed DataFrame
            st.write('Subsetted Columns')
            st.write(df.head())
            st.info("Important: To perform LGD mapping accurately, please download the subsetted file. It contains the selected columns required for mapping. Without the subsetted file, LGD mapping cannot be done effectively.")
            st.warning("⚠️ **Important Notice:** Please upload the subsetted file on the next page for LGD mapping. It contains the required columns for accurate mapping.")

            # Download button
            csv = df.to_csv(index=False)
            b64 = base64.b64encode(csv.encode()).decode()
            href = f'<a href="data:file/csv;base64,{b64}" download="processed_data.csv">Download CSV</a>'
            st.markdown(href, unsafe_allow_html=True)

        if st.button('Start Mapping', key='mapping_button'):
                    redirect_to_state_mapping_page()
# State Mapping Code
def state_mapping_page(dataset_selected=False):
    """
    Generates the state mapping page which allows users to upload a CSV or Excel file containing a 'state_name' column. 
    It then creates a mapped dataset which contains a state_code column based on the state_name column in the uploaded dataset. 
    It also allows users to update the state name variations and download the mapped dataset. 
    If the district_mapping_button is clicked, it redirects to the district mapping page. 
    
    Parameters:
    ----------
    dataset_selected: bool, optional
        Default value is False. If True, the 'dataset_file' is obtained from the session state. If False, it prompts the user to upload a dataset file.

    Returns:
    ----------
    None
    """
    
    st.title('State LGD Mapping')


    if not dataset_selected:
        dataset_file = st.file_uploader('Upload dataset', type=['csv', 'xlsx'])
        if dataset_file is None:
            st.warning("Please upload a dataset file.")
            return
    else:
        dataset_file = st.session_state['dataset_file']

    dataset = pd.read_csv(dataset_file) if dataset_file.name.endswith('.csv') else pd.read_excel(dataset_file)
    if 'state_name' not in dataset.columns:
        st.error("Error: The dataset does not contain the 'state_name' column.")
        return
    st.subheader("Before State LGD Mapping")
    st.write(dataset.head())
    with st.spinner("Processing..."):
        state_mapping = get_state_mappings()
        mapped_dataset = create_mapped_dataset(dataset, state_mapping)
        unmatched_names = mapped_dataset[mapped_dataset['state_code'] == -2]['state_name']

    if unmatched_names.empty:
        st.success('No Unmatched State Names')
        mapped_dataset.to_csv('data.csv', index=False)

        st.subheader("After State LGD Mapping")
        with st.spinner("Processing..."):
            st.write(mapped_dataset.head())
            generate_download_link(mapped_dataset)

        if st.button('Start District Mapping', key='district_mapping_button'):
            if 'district_name' not in mapped_dataset.columns:
                st.error("Error: The dataset does not contain the 'district_name' column.")
                return
            redirect_to_district_page()
    else:
        st.subheader('Unmatched State Names')
        st.write(unmatched_names.unique())
        note = "Please provide the state name variations separated by commas or a single state name."
        st.info(note)
        st.subheader('Update State Name Variations')
        state_mapping = get_state_mappings()
        mapped_dataset = create_mapped_dataset(dataset, state_mapping)
        unmatched_names = mapped_dataset[mapped_dataset['state_code'] == -2]['state_name']
        entity_table_name = "states"
        update_variations(unmatched_names.unique(), state_mapping, entity_table_name)
        unmatched_names = mapped_dataset[mapped_dataset['state_code'] == -2]['state_name']
        
        if unmatched_names.empty:
            st.write(mapped_dataset.head())
            mapped_dataset.to_csv('data.csv', index=False)
            generate_download_link(mapped_dataset)
            if st.button('Start District Mapping', key='district_mapping_button'):
                if 'district_name' not in mapped_dataset.columns:
                    st.error("Error: The dataset does not contain the 'district_name' column.")
                    return
                redirect_to_district_page()

def district_page():
    """
    This function is responsible for displaying the District LGD mapping page. It fetches the district mapping dataset,
    maps the dataset, and downloads the mapped dataset. If there are any unmatched district names, it prompts the user to
    provide the district name variations. Once the district name variations are provided, it updates the entityNameVariants
    column in the SQLite table and generates a new mapped dataset for download. If the 'Start Sub-District/Block Mapping'
    button is clicked, it does nothing. 
    """
    st.title('District LGD Mapping')
    st.subheader("Before District LGD Mapping")
    state_dataset = load_file()
    st.write(state_dataset.head())
    # Apply district mapping and create a new dataset
    data  = fetch_district_mapping()
    district_mapping = populate_entity_mapping(data,'district_name','state_code')
    mapped_dataset = create_district_mapped_dataset(state_dataset, district_mapping)
    # Check if there are any unmatched names
    unmatched_names = mapped_dataset[mapped_dataset['district_code'] == -2]['district_name']

    if unmatched_names.empty:
        # Display a message if there are no unmatched names
        st.success('No Unmatched district Names')
        mapped_dataset.to_csv("data.csv",index=False)
        # Create a CSV file in memory
        st.subheader("After District LGD Mapping")
        st.write(state_dataset.head())
        generate_download_link(mapped_dataset)
        condition = True 
        if 'sub_district_name' not in mapped_dataset.columns:
            condition = False

        if st.button('Start Block Mapping', key='block_mapping_button'):
            if 'block_name' not in mapped_dataset.columns:
                    st.error("Error: The dataset does not contain the 'block_name' column.")
                    return
            redirect_to_block_page()

        if condition:    
            if st.button('Start Sub-District Mapping', key='sub-district_mapping_button'):
                if 'sub_district_name' not in mapped_dataset.columns:
                        st.error("Error: The dataset does not contain the 'sub_district_name' column.")
                        return
                redirect_to_sub_district_page()
    else:
            # Display the dataset with unmatched names
            st.subheader('Unmatched District Names')
            st.write(f'Unmatched District Count: '+str(len(unmatched_names.unique())))
            st.write(unmatched_names.unique())
            # Display the note
            note = "Please provide the district name variations separated by commas or a single district name."
            st.info(note)
            # Accept comma-separated values or single value only
            st.subheader('Update District Name Variations')
            #district_mapping = populate_entity_mapping(data,'district_name','state_code')
            #mapped_dataset = create_district_mapped_dataset(state_dataset, district_mapping)
            # Check if there are any unmatched names
            #unmatched_names = mapped_dataset[mapped_dataset['district_code'] == -2]['district_name']
            district_names = [row[0] for row in data]
            entity_table_name = "district"
            update_variations(unmatched_names.unique(), district_names, entity_table_name)
            #unmatched_names = mapped_dataset[mapped_dataset['district_code'] == -2]['district_name']
            if unmatched_names.empty:
                st.success('District Name Variations Updated Successfully.')
                # Create a CSV file in memory
                mapped_dataset.to_csv("data.csv",index=False)
                st.subheader("After District LGD Mapping")
                st.write(mapped_dataset.head())
                generate_download_link(mapped_dataset)

                if 'block_name' in mapped_dataset.columns:
                    if st.button('Start Block Mapping', key='block_mapping_button'):
                        if 'block_name' not in mapped_dataset.columns:
                            st.error("Error: The dataset does not contain the 'block_name' column.")
                            return
                        redirect_to_block_page()

                if 'sub_district_name' in mapped_dataset.columns:
                    if st.button('Start Sub-District Mapping', key='sub-district_mapping_button'):
                        if 'sub_district_name' not in mapped_dataset.columns:
                            st.error("Error: The dataset does not contain the 'sub_district_name' column.")
                            return
                        redirect_to_sub_district_page()

# Block mapping
def block_page():
    """
    This function is responsible for displaying the block LGD mapping page. It fetches the block mapping dataset,
    maps the dataset, and downloads the mapped dataset. If there are any unmatched block names, it prompts the user to
    provide the block name variations. Once the block name variations are provided, it updates the entityNameVariants
    column in the SQLite table and generates a new mapped dataset for download. If the 'Start Sub-block/Block Mapping'
    button is clicked, it does nothing. 
    """
    st.title('Block LGD Mapping')
    st.subheader("Before Block LGD Mapping")
    block_dataset = pd.read_csv('data.csv')
    st.write(block_dataset.head())
    data = fetch_block_mapping()
    # Apply block mapping and create a new dataset
    block_mapping = populate_entity_mapping(data,'block_name','district_code')
    mapped_dataset = create_block_mapped_dataset(block_dataset, block_mapping)
    # Check if there are any unmatched names
    unmatched_names = mapped_dataset[mapped_dataset['block_code'] == -2]['block_name']

    if unmatched_names.empty:
        # Display a message if there are no unmatched names
        st.success('No Unmatched Block Names')

        # Create a CSV file in memory
        
        st.subheader("After Block LGD Mapping")
        mapped_dataset.to_csv("data.csv",index=False)
        st.write(mapped_dataset.head())
        generate_download_link(mapped_dataset)
        if st.button('Start Panchayat Mapping', key='Panchayat_mapping_button'):
            if 'gp_name' not in mapped_dataset.columns:
                        st.error("Error: The dataset does not contain the 'gp_name' column.")
                        return
            redirect_to_panchayat_page()

    else:
        # Display the dataset with unmatched names
        st.subheader('Unmatched block Names')
        st.write(f'Unmatched block Count: '+str(len(unmatched_names.unique())))
        st.write(unmatched_names.unique())

        # Display the note
        note = "Please provide the block name variations separated by commas or a single block name."
        st.warning(note)

        # Accept comma-separated values or single value only
        st.subheader('Update Block Name Variations')

        entity_table_name = "block"
        block_mapping = populate_entity_mapping(data,'block_name','district_code')
        mapped_dataset = create_block_mapped_dataset(block_dataset, block_mapping)
        block_names = [row[0] for row in data]
        unmatched_names = mapped_dataset[mapped_dataset['block_code'] == -2]['block_name']
        unmatched_names = unmatched_names.unique()
        update_variations(unmatched_names, block_names, entity_table_name)
        # Display a success message
        unmatched_names = mapped_dataset[mapped_dataset['block_code'] == -2]['block_name']
        if unmatched_names.empty:
            st.success('Block Name Variations Updated Successfully.')
            # Create a CSV file in memory
            st.subheader("After block LGD Mapping")
            mapped_dataset.to_csv("data.csv",index=False)
            st.write(mapped_dataset.head())
            generate_download_link(mapped_dataset)

            if st.button('Start Panchayat Mapping', key='Panchayat_mapping_button'):
                if 'gp_name' not in mapped_dataset.columns:
                            st.error("Error: The dataset does not contain the 'gp_name' column.")
                            return
                redirect_to_panchayat_page()

#GP Mapping 



def gp_page():
    """
    This function is responsible for displaying the gp LGD mapping page. It fetches the gp mapping dataset,
    maps the dataset, and downloads the mapped dataset. If there are any unmatched gp names, it prompts the user to
    provide the gp name variations. Once the gp name variations are provided, it updates the entityNameVariants
    column in the SQLite table and generates a new mapped dataset for download. If the 'Start Sub-gp/gp Mapping'
    button is clicked, it does nothing. 
    """
    st.title('GP LGD Mapping')
    st.subheader("Before GP LGD Mapping")
    gp_dataset = pd.read_csv('data.csv')
    data= fetch_gp_mapping()
    st.write(gp_dataset.head())
    unmatched_names = None
    # Apply gp mapping and create a new dataset
    
    #gp_mapping = populate_gp_mapping()
    gp_mapping = populate_entity_mapping(data,'gp_name','block_code')
    mapped_dataset = create_gp_mapped_dataset(gp_dataset, gp_mapping)

    # Check if there are any unmatched names
    unmatched_names = mapped_dataset[mapped_dataset['gp_code'] == -2]['gp_name']
    if unmatched_names.empty:
        # Display a message if there are no unmatched names
        st.success('No Unmatched GP Names')

        # Create a CSV file in memory
        csv_file = mapped_dataset.to_csv(index=False)
        st.subheader("After GP LGD Mapping")
        st.write(mapped_dataset.head())

        generate_download_link(mapped_dataset)

        if st.button('Start Village Mapping', key='village_mapping_button'):
            if 'village_name' not in mapped_dataset.columns:
                        st.error("Error: The dataset does not contain the 'village_name' column.")
                        return
            redirect_to_panchayat_page()

    else:
        # Display the dataset with unmatched names
        st.subheader('Unmatched GP Names')
        st.write(f'Unmatched GP Count: '+str(len(unmatched_names.unique())))
        st.write(unmatched_names.unique())
        
        # Display the note
        note = "Please provide the GP name variations separated by commas or a single GP name."
        st.info(note)

        # Accept comma-separated values or single value only
        st.subheader('Update GP Name Variations')
        gp_mapping = populate_gp_mapping()
        mapped_dataset = create_gp_mapped_dataset(gp_dataset, gp_mapping)
        entity_name = create_entity_name_list()
        # Check if there are any unmatched names
        unmatched_names = mapped_dataset[mapped_dataset['gp_code'] == -2]['gp_name']
        update_variations(unmatched_names.unique(), entity_name, "gp")
        unmatched_names = mapped_dataset[mapped_dataset['gp_code'] == -2]['gp_name']
        if unmatched_names.empty:
            st.success('GP Name Variations Updated Successfully.')
            # Create a CSV file in memory
            mapped_dataset.to_csv("data.csv",index=False)
            st.subheader("After GP LGD Mapping")
            st.write(mapped_dataset.head())
            # Generate download link for the CSV file
            generate_download_link(mapped_dataset)
            if st.button('Start Village Mapping', key='village_mapping_button'):
                if 'village_name' not in mapped_dataset.columns:
                            st.error("Error: The dataset does not contain the 'village_name' column.")
                            return
                redirect_to_panchayat_page()


#village page

def village_page():
    """
    This function is responsible for displaying the village LGD mapping page. It fetches the village mapping dataset,
    maps the dataset, and downloads the mapped dataset. If there are any unmatched village names, it prompts the user to
    provide the village name variations. Once the village name variations are provided, it updates the entityNameVariants
    column in the SQLite table and generates a new mapped dataset for download. If the 'Start Sub-village/village Mapping'
    button is clicked, it does nothing. 
    """
    st.title('village LGD Mapping')
    st.subheader("Before village LGD Mapping")
    village_dataset = pd.read_csv('data.csv')
    st.write(village_dataset.head())
    # Apply village mapping and create a new dataset
    village_mapping = populate_village_mapping()

    mapped_dataset = create_village_mapped_dataset(village_dataset, village_mapping)

    # Check if there are any unmatched names
    unmatched_names = mapped_dataset[mapped_dataset['village_code'] == -2]['village_name']

    if unmatched_names.empty:
        # Display a message if there are no unmatched names
        st.success('No Unmatched Village Names')
        # Create a CSV file in memory
        st.subheader("After village LGD Mapping")
        st.write(mapped_dataset.head())
        generate_download_link(mapped_dataset)

    else:
        # Display the dataset with unmatched names
        st.subheader('Unmatched village Names')
        st.write(f'Unmatched villages Count: '+str(len(unmatched_names.unique())))
        st.write(unmatched_names.unique())
        # Display the note
        note = "Please provide the village name variations separated by commas or a single village name."
        st.info(note)
        # Accept comma-separated values or single value only
        st.subheader('Update village Name Variations')
        village_mapping = populate_village_mapping()
        mapped_dataset = create_village_mapped_dataset(village_dataset, village_mapping)
        # Check if there are any unmatched names
        unmatched_names = mapped_dataset[mapped_dataset['village_code'] == -2]['panchayat_name']
        update_variations(unmatched_names.unique(), village_mapping, "villages")
        unmatched_names = mapped_dataset[mapped_dataset['village_code'] == -2]['panchayat_name']
        if unmatched_names.empty:
            st.success('village Name Variations Updated Successfully.')
            # Create a CSV file in memory
            mapped_dataset.to_csv("data.csv",index=False)
            st.subheader("After village LGD Mapping")
            st.write(mapped_dataset.head())
            # Generate download link for the CSV file
            generate_download_link(mapped_dataset)
            


#sub-district mapping
def sub_district_page():
    """
    This function is responsible for displaying the Sub-District LGD mapping page. It fetches the Sub-District mapping dataset,
    maps the dataset, and downloads the mapped dataset. If there are any unmatched Sub-District names, it prompts the user to
    provide the Sub-District name variations. Once the Sub-District name variations are provided, it updates the entityNameVariants
    column in the SQLite table and generates a new mapped dataset for download. If the 'Start Sub-Sub-District/Sub-District Mapping'
    button is clicked, it does nothing. 
    """
    st.title('Sub-District LGD Mapping')
    st.subheader("Before Sub-District LGD Mapping")
    sub_district_dataset = pd.read_csv('data.csv')
    st.write(sub_district_dataset.head())
    # Apply Sub-District mapping and create a new dataset
    sub_district_mapping = populate_sub_district_mapping()

    mapped_dataset = create_sub_district_mapped_dataset(sub_district_dataset, sub_district_mapping)

    # Check if there are any unmatched names
    unmatched_names = mapped_dataset[mapped_dataset['sub_district_code'] == -2]['sub_district_name']

    if unmatched_names.empty:
        # Display a message if there are no unmatched names
        st.success('No Unmatched Sub-District Names')
        mapped_dataset.to_csv("data.csv",index=False)
        # Create a CSV file in memory
        generate_download_link(mapped_dataset)
        if st.button('Start Panchayat Mapping', key='Panchayat_mapping_button'):
                if 'panchayat_name' not in mapped_dataset.columns:
                            st.error("Error: The dataset does not contain the 'panchayat_name' column.")
                            return
                redirect_to_panchayat_page()

    else:
        # Display the dataset with unmatched names
        st.subheader('Unmatched Sub-District Names')
        st.write(f'Unmatched Sub-District Count: '+str(len(unmatched_names.unique())))
        st.write(unmatched_names)

        # Display the note
        note = "Please provide the Sub-District name variations separated by commas or a single Sub-District name."
        st.info(note)

        # Accept comma-separated values or single value only
        st.subheader('Update Sub-District Name Variations')
        sub_district_mapping = populate_sub_district_mapping()
        mapped_dataset = create_sub_district_mapped_dataset(sub_district_dataset, sub_district_mapping)
        unmatched_names = mapped_dataset[mapped_dataset['sub_district_code'] == -2]['sub_district_name']
        update_variations(unmatched_names.unique(), sub_district_mapping, "sub_district")
        unmatched_names = mapped_dataset[mapped_dataset['sub_district_code'] == -2]['sub_district_name']
        if unmatched_names.empty:
            # Display a success message
            st.success('Sub-District Name Variations Updated Successfully.')
            # Create a CSV file in memory
            csv_file = mapped_dataset.to_csv(index=False)
            st.subheader("After Sub-District LGD Mapping")
            st.write(mapped_dataset.head())
            mapped_dataset.to_csv("data.csv",index=False)
            st.write(mapped_dataset.head())
            # Generate download link for the CSV file
            generate_download_link(mapped_dataset)


            if st.button('Start Panchayat Mapping', key='Panchayat_mapping_button'):
                if 'panchayat_name' not in mapped_dataset.columns:
                            st.error("Error: The dataset does not contain the 'panchayat_name' column.")
                            return
                redirect_to_panchayat_page()
    



if __name__ == "__main__":
    main()