from cgitb import text import os import gradio as gr import pandas as pd import plotly.graph_objects as go MAPBOX_TOKEN = os.environ.get('MAPBOX_TOKEN') df = pd.read_csv("AB_NYC_2019.csv") def update_map(min_price, max_price, boroughs): new_df = df[(df['neighbourhood_group'].isin(boroughs)) & (df['price'] > min_price) & (df['price'] < max_price)] names = df["name"].tolist() prices = df["price"].tolist() text_list = [(names[i], prices[i]) for i in range(0, len(names))] fig = go.Figure(go.Scattermapbox( customdata=text_list, lat=new_df['latitude'].tolist(), lon=new_df['longitude'].tolist(), mode='markers', marker=go.scattermapbox.Marker( size=6 ), hoverinfo="text", hovertemplate = '
Name: %{customdata[0]}'+ '
Price: $%{customdata[1]}
' )) fig.update_layout( autosize=True, hovermode='closest', mapbox=dict( accesstoken=MAPBOX_TOKEN, bearing=0, center=go.layout.mapbox.Center( lat=40.67, lon=-73.90 ), pitch=0, zoom=9 ), ) return fig with gr.Blocks() as demo: with gr.Row(): with gr.Column(): min_price = gr.Number(value=0, label="Minimum Price") max_price = gr.Number(value=1000, label="Maximum Price") boroughs = gr.CheckboxGroup(["Queens", "Brooklyn", "Manhattan", "Bronx", "Staten Island"], label="Select Boroughs:") with gr.Column(): with gr.Box(): map = gr.Plot() min_price.submit(update_map, [min_price, max_price, boroughs], map) max_price.submit(update_map, [min_price, max_price, boroughs], map) max_price.change(update_map, [min_price, max_price, boroughs], map) boroughs.change(update_map, [min_price, max_price, boroughs], map) demo.load(update_map, [min_price, max_price, boroughs], map) demo.launch()