File size: 2,076 Bytes
e49e46a
 
 
 
 
0b9c9a0
e49e46a
969a9f6
 
 
 
e49e46a
969a9f6
 
 
 
e49e46a
969a9f6
161f699
0b9c9a0
 
969a9f6
161f699
 
 
 
 
969a9f6
e49e46a
969a9f6
 
 
ebf4636
 
 
969a9f6
d868fa2
5740834
26d9eaf
969a9f6
bdf505f
 
 
 
969a9f6
5740834
26d9eaf
969a9f6
d868fa2
 
 
e49e46a
969a9f6
e49e46a
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
import gradio as gr
from gradio_folium import Folium
from folium import Map
import pandas as pd
import pathlib
import os # for file search of csv files

# Function to list all CSV files in the current directory
def list_csv_files():
    path = pathlib.Path(__file__).parent
    return [f for f in os.listdir(path) if f.endswith('.csv')]

# Function to load data from selected CSV and update the map
def update_map(csv_file):
    df = pd.read_csv(pathlib.Path(__file__).parent / csv_file)
    return df, Folium(value=Map(location=[df.iloc[0]['Latitude'], df.iloc[0]['Longitude']], zoom_start=10), height=400)

# Function to update location on map based on selected data row
def select2(data: gr.Dataframe):
    row = data.iloc[0, :]
    return Map(location=[row['Latitude'], row['Longitude']], zoom_start=10)

# select function with input dataframe and data where gradio Selects the Data then put data index 0 into df.iloc, then Map that location
def select(df, data: gr.SelectData):
    row = df.iloc[data.index[0], :]
    return Map(location=[row['Latitude'], row['Longitude']])
    
# Gradio Blocks
with gr.Blocks() as demo:
    gr.Markdown("# ๐Ÿ—บ๏ธ Explore AI Data Maps with Gradio and Folium\n"
                "Install this custom component with `pip install gradio_folium` - wheel files in this directory")

    # Map
    map_component = Folium(height=400)
    
    # Select box for CSV files
    csv_files = list_csv_files()
    csv_selector = gr.Dropdown(label="๐Ÿž๏ธ๐Ÿ™๏ธ Select CSV File ๐Ÿ—บ๏ธโœˆ๏ธ", choices=csv_files)

    # Dataframe and map components
    data = gr.Dataframe(
        row_count=(5,'static'), 
        interactive=False
    )
    # Button to reload data and map
    reload_button = gr.Button("๐Ÿ”„ Reload ๐ŸŒ๐Ÿ“๐Ÿ“‚", elem_id="reload_button")

    # Interaction logic
    csv_selector.change(update_map, inputs=csv_selector, outputs=[data, map_component])
    data.select(select, inputs=data, outputs=map_component)
    reload_button.click(update_map, inputs=csv_selector, outputs=[data, map_component])

# Launch the app
demo.launch()