AIDataMaps / app.py
awacke1's picture
Update app.py
265341a
raw
history blame
1.69 kB
import gradio as gr
from gradio_folium import Folium
from folium import Map
import pandas as pd
import pathlib
import os
# 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 select(row_data):
lat, lon = row_data['Latitude'], row_data['Longitude']
return Folium(value=Map(location=[lat, lon], zoom_start=10), height=400)
# 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.row, outputs=map_component) # Updated line
reload_button.click(update_map, inputs=csv_selector, outputs=[data, map_component])
# Launch the app
demo.launch()