could you create an app that shows the best restaurants in an X mile radius
Drop files here
or click to upload
import streamlit as st
import folium
from streamlit_folium import folium_static
import requests
from geopy.geocoders import Nominatim
import json
st.title("Restaurant Finder")
# User inputs
location = st.text_input("Enter your location (address, city, or place):")
radius = st.slider("Select radius (miles)", 1, 20, 5)
# Convert miles to meters for the API
radius_meters = radius * 1609.34
if location:
try:
# Geocode the location
geolocator = Nominatim(user_agent="restaurant_finder")
location_data = geolocator.geocode(location)
if location_data:
# Create a map centered on the location
m = folium.Map(location=[location_data.latitude, location_data.longitude], zoom_start=13)
# Add a marker for the user's location
folium.Marker(
[location_data.latitude, location_data.longitude],
popup="Your Location",
icon=folium.Icon(color="red", icon="info-sign"),
).add_to(m)
# Use the Places API to find restaurants
# NOTE: Using corsproxy.io because we're in a WASM environment
url = f"https://corsproxy.io/?https://maps.googleapis.com/maps/api/place/nearbysearch/json"
params = {
"location": f"{location_data.latitude},{location_data.longitude}",
"radius": radius_meters,
"type": "restaurant",
"key": "YOUR_GOOGLE_PLACES_API_KEY" # You would need to replace this with a real API key
}
st.info("Note: This is a demo app. In a real application, you would need to use a valid Google Places API key to get actual restaurant data. Currently showing sample data.")
# Simulate some sample restaurants
sample_restaurants = [
{
"name": "Italian Delight",
"rating": 4.5,
"location": [location_data.latitude + 0.01, location_data.longitude + 0.01],
"address": "123 Main St"
},
{
"name": "Sushi Express",
"rating": 4.3,
"location": [location_data.latitude - 0.01, location_data.longitude + 0.01],
"address": "456 Oak Ave"
},
{
"name": "Burger Paradise",
"rating": 4.7,
"location": [location_data.latitude + 0.01, location_data.longitude - 0.01],
"address": "789 Pine Rd"
}
]
# Add restaurant markers to the map
for restaurant in sample_restaurants:
folium.Marker(
restaurant["location"],
popup=f"{restaurant['name']}\nRating: {restaurant['rating']}\n{restaurant['address']}",
icon=folium.Icon(color="green", icon="cutlery", prefix='fa'),
).add_to(m)
# Draw the radius circle
folium.Circle(
[location_data.latitude, location_data.longitude],
radius=radius_meters,
color="blue",
fill=True,
opacity=0.2
).add_to(m)
# Display the map
folium_static(m)
# Display restaurant list
st.subheader("Nearby Restaurants")
for restaurant in sample_restaurants:
with st.container():
st.markdown(f"""
**{restaurant['name']}**
Rating: ⭐ {restaurant['rating']}
Address: {restaurant['address']}
---
""")
else:
st.error("Location not found. Please try a different location.")
except Exception as e:
st.error(f"An error occurred: {str(e)}")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?