awacke1 commited on
Commit
969a9f6
·
1 Parent(s): 7f81e33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
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
- # create a dataframe with pathlib's Path to a __file__ in parent called cities.csv with places and latitude/longitude
8
- df = pd.read_csv(pathlib.Path(__file__).parent / "cities.csv")
 
 
9
 
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
11
- def select(df, data: gr.SelectData):
12
- row = df.iloc[data.index[0], :]
13
- return Map(location=[row['Latitude'], row['Longitude']])
14
 
15
- # gradio blocks demo
 
 
 
 
 
16
  with gr.Blocks() as demo:
17
-
18
- gr.Markdown(("# 🗺️ Explore AI Data Maps with Gradio and Folium\n"
19
- "Install this custom component with `pip install gradio_folium` - wheel files in this directory"))
 
 
 
20
 
21
- # define map using Folium
22
- map = Folium(value=Map(location=[25.7617, -80.1918]), height=400)
 
23
 
24
- # Create a DataFrame viewer with dataframe input
25
- data = gr.DataFrame(value=df, height=200)
26
 
27
- # Run select with data and map
28
- data.select(select, data, map)
 
 
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()