Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,29 +3,43 @@ from gradio_folium import Folium
|
|
3 |
from folium import Map
|
4 |
import pandas as pd
|
5 |
import pathlib
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
def
|
12 |
-
|
13 |
-
return Map(location=[
|
14 |
|
15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
16 |
with gr.Blocks() as demo:
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
|
27 |
-
#
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
# Launch
|
31 |
demo.launch()
|
|
|
3 |
from folium import Map
|
4 |
import pandas as pd
|
5 |
import pathlib
|
6 |
+
import os # for file search of csv files
|
7 |
|
8 |
+
# Function to list all CSV files in the current directory
|
9 |
+
def list_csv_files():
|
10 |
+
path = pathlib.Path(__file__).parent
|
11 |
+
return [f for f in os.listdir(path) if f.endswith('.csv')]
|
12 |
|
13 |
+
# Function to load data from selected CSV and update the map
|
14 |
+
def update_map(csv_file):
|
15 |
+
df = pd.read_csv(pathlib.Path(__file__).parent / csv_file)
|
16 |
+
return df, Folium(value=Map(location=[df.iloc[0]['Latitude'], df.iloc[0]['Longitude']], zoom_start=10), height=400)
|
17 |
|
18 |
+
# Function to update location on map based on selected data row
|
19 |
+
def select(data: gr.Dataframe):
|
20 |
+
row = data.iloc[0, :]
|
21 |
+
return Map(location=[row['Latitude'], row['Longitude']], zoom_start=10)
|
22 |
+
|
23 |
+
# Gradio Blocks
|
24 |
with gr.Blocks() as demo:
|
25 |
+
gr.Markdown("# 🗺️ Explore AI Data Maps with Gradio and Folium\n"
|
26 |
+
"Install this custom component with `pip install gradio_folium` - wheel files in this directory")
|
27 |
+
|
28 |
+
# Select box for CSV files
|
29 |
+
csv_files = list_csv_files()
|
30 |
+
csv_selector = gr.Dropdown(label="Select CSV File", choices=csv_files)
|
31 |
|
32 |
+
# Dataframe and map components
|
33 |
+
data = gr.Dataframe()
|
34 |
+
map_component = Folium(height=400)
|
35 |
|
36 |
+
# Button to reload data and map
|
37 |
+
reload_button = gr.Button("🔄 Reload", elem_id="reload_button")
|
38 |
|
39 |
+
# Interaction logic
|
40 |
+
csv_selector.change(update_map, inputs=csv_selector, outputs=[data, map_component])
|
41 |
+
data.select(select, inputs=data, outputs=map_component)
|
42 |
+
reload_button.click(update_map, inputs=csv_selector, outputs=[data, map_component])
|
43 |
|
44 |
+
# Launch the app
|
45 |
demo.launch()
|