City_Guide / app.py
ilsa15's picture
Update app.py
01f37dd verified
import streamlit as st
import requests
import pandas as pd
# -----------------------
# Overpass API function
# -----------------------
def fetch_restaurants(city_name):
query = f"""
[out:json][timeout:25];
area["name"="{city_name}"]->.searchArea;
(
node["amenity"="restaurant"](area.searchArea);
way["amenity"="restaurant"](area.searchArea);
relation["amenity"="restaurant"](area.searchArea);
);
out center tags;
"""
url = "https://overpass-api.de/api/interpreter"
response = requests.post(url, data={"data": query})
if response.status_code != 200:
return None
data = response.json()
restaurants = []
for element in data["elements"]:
tags = element.get("tags", {})
name = tags.get("name", "Unnamed")
cuisine = tags.get("cuisine", "Not listed")
phone = tags.get("phone", "N/A")
street = tags.get("addr:street", "")
housenumber = tags.get("addr:housenumber", "")
city = tags.get("addr:city", city_name)
address = f"{housenumber} {street}, {city}".strip()
restaurants.append({
"Name": name,
"Cuisine": cuisine,
"Contact": phone,
"Address": address
})
return pd.DataFrame(restaurants)
# -----------------------
# Streamlit UI
# -----------------------
st.set_page_config(page_title="Live City Restaurant Guide", layout="wide")
st.title("🍽️ Live City Restaurant Guide")
st.subheader("Enter a city to find real-time restaurant data (OpenStreetMap API)")
city = st.text_input("Enter city name (e.g., Lahore, Karachi, Paris, New York):").strip().title()
if city:
with st.spinner("πŸ” Fetching data... please wait..."):
df = fetch_restaurants(city)
if df is None or df.empty:
st.warning("❌ No restaurant data found or API limit reached. Try another city.")
else:
st.success(f"βœ… Found {len(df)} restaurants in {city}")
for idx, row in df.iterrows():
with st.expander(f"{row['Name']} 🍴"):
st.markdown(f"**πŸ“ Address:** {row['Address']}")
st.markdown(f"**🍱 Cuisine:** {row['Cuisine']}")
st.markdown(f"**πŸ“ž Contact:** {row['Contact']}")
else:
st.info("πŸ” Please enter a city name to search.")