AIDataMaps / app.py
awacke1's picture
Update app.py
61c828c
raw
history blame
2.17 kB
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 select_experimental(data: gr.Dataframe):
row = data.iloc[0, :]
return Map(location=[row['Latitude'], row['Longitude']], zoom_start=10)
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 = Folium(value=Map(location=[25.7617, -80.1918]), height=400)
data = gr.DataFrame(value=df, height=200)
data.select(select, data, map)
demo.launch()
# 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)
#map = Folium(value=Map(location=[25.7617, -80.1918]), height=400)
#data = gr.DataFrame(value=df, height=200)
#data.select(select, data, map)
# 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()