Spaces:
Runtime error
Runtime error
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") | |
# 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() | |
map_component = Folium(height=400) | |
# 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() | |