Update app.py
Browse files
app.py
CHANGED
|
@@ -62,8 +62,7 @@ function sendLocationToStreamlit(lat, lon) {
|
|
| 62 |
const iframe = parent.document.querySelector('iframe[srcdoc*="streamlit"]');
|
| 63 |
const streamlitWindow = iframe ? iframe.contentWindow : null;
|
| 64 |
if (streamlitWindow) {
|
| 65 |
-
|
| 66 |
-
streamlitWindow.postMessage({type: "streamlit:setComponentValue", value: {current_lat: lat, current_lon: lon}}, "*");
|
| 67 |
}
|
| 68 |
}
|
| 69 |
|
|
@@ -77,7 +76,15 @@ function getLocation() {
|
|
| 77 |
const lat = position.coords.latitude.toFixed(6);
|
| 78 |
const lon = position.coords.longitude.toFixed(6);
|
| 79 |
statusDiv.innerHTML = "✅ Location found: Latitude = " + lat + ", Longitude = " + lon;
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
},
|
| 82 |
function(error) {
|
| 83 |
statusDiv.innerHTML = "❌ Location error: " + error.message;
|
|
@@ -88,23 +95,6 @@ function getLocation() {
|
|
| 88 |
statusDiv.innerHTML = "❌ Geolocation is not supported.";
|
| 89 |
}
|
| 90 |
}
|
| 91 |
-
|
| 92 |
-
// Add a callback function to handle the response
|
| 93 |
-
function handleResponse(event) {
|
| 94 |
-
if (event.data.type === "streamlit:setComponentValue") {
|
| 95 |
-
const { current_lat, current_lon } = event.data.value;
|
| 96 |
-
if (current_lat && current_lon) {
|
| 97 |
-
// Update the coordinates
|
| 98 |
-
st.session_state.current_lat = current_lat;
|
| 99 |
-
st.session_state.current_lon = current_lon;
|
| 100 |
-
st.session_state.aux_lat = current_lat;
|
| 101 |
-
st.session_state.aux_lon = current_lon;
|
| 102 |
-
}
|
| 103 |
-
}
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
// Add event listener for messages
|
| 107 |
-
window.addEventListener("message", handleResponse);
|
| 108 |
</script>
|
| 109 |
|
| 110 |
<div style="margin-top: 10px;">
|
|
@@ -120,6 +110,11 @@ window.addEventListener("message", handleResponse);
|
|
| 120 |
</div>
|
| 121 |
"""
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
#part 2
|
| 124 |
|
| 125 |
class LocationTools:
|
|
@@ -533,29 +528,17 @@ def create_navigation_map(places: List[Dict], user_location: Dict) -> str:
|
|
| 533 |
raise Exception(f"Error creating map: {str(e)}")
|
| 534 |
|
| 535 |
#part 4
|
|
|
|
|
|
|
| 536 |
def main():
|
| 537 |
st.title("Tourist Location Analyzer")
|
| 538 |
st.write("Discover interesting places around any location!")
|
| 539 |
|
| 540 |
# Session state defaults
|
| 541 |
if 'default_lat' not in st.session_state:
|
| 542 |
-
st.session_state.default_lat =
|
| 543 |
if 'default_lon' not in st.session_state:
|
| 544 |
-
st.session_state.default_lon =
|
| 545 |
-
if 'current_lat' not in st.session_state:
|
| 546 |
-
st.session_state.current_lat = "0.0"
|
| 547 |
-
if 'current_lon' not in st.session_state:
|
| 548 |
-
st.session_state.current_lon = "0.0"
|
| 549 |
-
if 'aux_lat' not in st.session_state:
|
| 550 |
-
st.session_state.aux_lat = "0.0"
|
| 551 |
-
if 'aux_lon' not in st.session_state:
|
| 552 |
-
st.session_state.aux_lon = "0.0"
|
| 553 |
-
if 'status_message' not in st.session_state:
|
| 554 |
-
st.session_state.status_message = ""
|
| 555 |
-
if 'extracted_lat' not in st.session_state:
|
| 556 |
-
st.session_state.extracted_lat = "0.0"
|
| 557 |
-
if 'extracted_lon' not in st.session_state:
|
| 558 |
-
st.session_state.extracted_lon = "0.0"
|
| 559 |
|
| 560 |
with st.sidebar:
|
| 561 |
st.header("Location Input")
|
|
@@ -565,53 +548,16 @@ def main():
|
|
| 565 |
st.markdown("### Get Your Current Location")
|
| 566 |
st.components.v1.html(GEOLOCATION_JS, height=200)
|
| 567 |
|
| 568 |
-
st.markdown("###
|
| 569 |
col1, col2 = st.columns(2)
|
| 570 |
with col1:
|
| 571 |
-
st.
|
| 572 |
with col2:
|
| 573 |
-
st.
|
| 574 |
-
|
| 575 |
-
# Add new textboxes for extracted values
|
| 576 |
-
st.markdown("### Extracted Coordinates")
|
| 577 |
-
col3, col4 = st.columns(2)
|
| 578 |
-
with col3:
|
| 579 |
-
st.text_input("Extracted Latitude", value=st.session_state.extracted_lat, key="extracted_lat")
|
| 580 |
-
with col4:
|
| 581 |
-
st.text_input("Extracted Longitude", value=st.session_state.extracted_lon, key="extracted_lon")
|
| 582 |
-
|
| 583 |
-
# Debug information
|
| 584 |
-
st.markdown("### Debug Information")
|
| 585 |
-
st.write(f"Current Coordinates: {st.session_state.current_lat}, {st.session_state.current_lon}")
|
| 586 |
-
st.write(f"Auxiliary Coordinates: {st.session_state.aux_lat}, {st.session_state.aux_lon}")
|
| 587 |
-
st.write(f"Status Message: {st.session_state.status_message}")
|
| 588 |
-
|
| 589 |
-
# Extract coordinates from status message
|
| 590 |
-
if "Location found:" in st.session_state.status_message:
|
| 591 |
-
try:
|
| 592 |
-
# Extract latitude and longitude using string manipulation
|
| 593 |
-
lat_str = st.session_state.status_message.split("Latitude = ")[1].split(",")[0]
|
| 594 |
-
lon_str = st.session_state.status_message.split("Longitude = ")[1]
|
| 595 |
-
|
| 596 |
-
# Print extracted values
|
| 597 |
-
st.write("Extracted Latitude:", lat_str)
|
| 598 |
-
st.write("Extracted Longitude:", lon_str)
|
| 599 |
-
|
| 600 |
-
# Convert to float and save
|
| 601 |
-
st.session_state.current_lat = lat_str
|
| 602 |
-
st.session_state.current_lon = lon_str
|
| 603 |
-
st.session_state.aux_lat = lat_str
|
| 604 |
-
st.session_state.aux_lon = lon_str
|
| 605 |
-
st.session_state.extracted_lat = lat_str
|
| 606 |
-
st.session_state.extracted_lon = lon_str
|
| 607 |
-
st.write("✅ Coordinates extracted and saved!")
|
| 608 |
-
except Exception as e:
|
| 609 |
-
st.error(f"Error extracting coordinates: {e}")
|
| 610 |
|
| 611 |
-
# Keep the button but we'll use it later
|
| 612 |
if st.button("Use These Coordinates"):
|
| 613 |
try:
|
| 614 |
-
location_tools = LocationTools(
|
| 615 |
current_location = location_tools.get_current_location()
|
| 616 |
places = location_tools.search_nearby_places(radius)
|
| 617 |
st.success(f"📍 Current Location: {current_location['address']}")
|
|
@@ -638,45 +584,5 @@ def main():
|
|
| 638 |
logger.error(f"Error processing location: {e}")
|
| 639 |
st.error("Error processing location. Please try again.")
|
| 640 |
|
| 641 |
-
# Add a new button to show the map with current coordinates
|
| 642 |
-
if st.button("Show Map with Current Coordinates"):
|
| 643 |
-
if st.session_state.current_lat != "0.0" and st.session_state.current_lon != "0.0":
|
| 644 |
-
try:
|
| 645 |
-
st.info("🔄 Processing your location, please wait...")
|
| 646 |
-
# Add a delay to ensure coordinates are properly set
|
| 647 |
-
time.sleep(5)
|
| 648 |
-
|
| 649 |
-
# Use the current coordinates
|
| 650 |
-
location_tools = LocationTools(float(st.session_state.current_lat), float(st.session_state.current_lon))
|
| 651 |
-
current_location = location_tools.get_current_location()
|
| 652 |
-
places = location_tools.search_nearby_places(radius)
|
| 653 |
-
st.success(f"📍 Current Location: {current_location['address']}")
|
| 654 |
-
|
| 655 |
-
# Create and display the map
|
| 656 |
-
map_html = create_navigation_map(places, current_location)
|
| 657 |
-
st.components.v1.html(map_html, height=600)
|
| 658 |
-
|
| 659 |
-
st.subheader("Top 5 Places by Category")
|
| 660 |
-
places_by_type = {}
|
| 661 |
-
for place in places:
|
| 662 |
-
category = place['type']
|
| 663 |
-
if category not in places_by_type:
|
| 664 |
-
places_by_type[category] = []
|
| 665 |
-
places_by_type[category].append(place)
|
| 666 |
-
|
| 667 |
-
for category, items in places_by_type.items():
|
| 668 |
-
with st.expander(f"{category} ({len(items)})"):
|
| 669 |
-
sorted_items = sorted(items, key=lambda x: x['importance_score'], reverse=True)[:5]
|
| 670 |
-
for i, place in enumerate(sorted_items, 1):
|
| 671 |
-
st.write(f"{i}. {place['name']}")
|
| 672 |
-
st.write(f" Score: {place['importance_score']:.1f}")
|
| 673 |
-
st.write(f" Distance: {place['distance']:.2f} km")
|
| 674 |
-
st.write("---")
|
| 675 |
-
except Exception as e:
|
| 676 |
-
logger.error(f"Error processing location: {e}")
|
| 677 |
-
st.error("Error processing location. Please try again.")
|
| 678 |
-
else:
|
| 679 |
-
st.warning("Please get your current location first!")
|
| 680 |
-
|
| 681 |
if __name__ == "__main__":
|
| 682 |
-
main()
|
|
|
|
| 62 |
const iframe = parent.document.querySelector('iframe[srcdoc*="streamlit"]');
|
| 63 |
const streamlitWindow = iframe ? iframe.contentWindow : null;
|
| 64 |
if (streamlitWindow) {
|
| 65 |
+
streamlitWindow.postMessage({type: "streamlit:setComponentValue", value: {lat: lat, lon: lon}}, "*");
|
|
|
|
| 66 |
}
|
| 67 |
}
|
| 68 |
|
|
|
|
| 76 |
const lat = position.coords.latitude.toFixed(6);
|
| 77 |
const lon = position.coords.longitude.toFixed(6);
|
| 78 |
statusDiv.innerHTML = "✅ Location found: Latitude = " + lat + ", Longitude = " + lon;
|
| 79 |
+
|
| 80 |
+
const inputs = parent.document.querySelectorAll('input[type=number]');
|
| 81 |
+
if (inputs.length >= 2) {
|
| 82 |
+
inputs[0].value = lat;
|
| 83 |
+
inputs[1].value = lon;
|
| 84 |
+
const event = new Event('input', { bubbles: true });
|
| 85 |
+
inputs[0].dispatchEvent(event);
|
| 86 |
+
inputs[1].dispatchEvent(event);
|
| 87 |
+
}
|
| 88 |
},
|
| 89 |
function(error) {
|
| 90 |
statusDiv.innerHTML = "❌ Location error: " + error.message;
|
|
|
|
| 95 |
statusDiv.innerHTML = "❌ Geolocation is not supported.";
|
| 96 |
}
|
| 97 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
</script>
|
| 99 |
|
| 100 |
<div style="margin-top: 10px;">
|
|
|
|
| 110 |
</div>
|
| 111 |
"""
|
| 112 |
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
#part 2
|
| 119 |
|
| 120 |
class LocationTools:
|
|
|
|
| 528 |
raise Exception(f"Error creating map: {str(e)}")
|
| 529 |
|
| 530 |
#part 4
|
| 531 |
+
from location_tools import LocationTools, create_navigation_map # make sure these are defined in your app
|
| 532 |
+
|
| 533 |
def main():
|
| 534 |
st.title("Tourist Location Analyzer")
|
| 535 |
st.write("Discover interesting places around any location!")
|
| 536 |
|
| 537 |
# Session state defaults
|
| 538 |
if 'default_lat' not in st.session_state:
|
| 539 |
+
st.session_state.default_lat = 48.8566
|
| 540 |
if 'default_lon' not in st.session_state:
|
| 541 |
+
st.session_state.default_lon = 2.3522
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
|
| 543 |
with st.sidebar:
|
| 544 |
st.header("Location Input")
|
|
|
|
| 548 |
st.markdown("### Get Your Current Location")
|
| 549 |
st.components.v1.html(GEOLOCATION_JS, height=200)
|
| 550 |
|
| 551 |
+
st.markdown("### Enter Coordinates")
|
| 552 |
col1, col2 = st.columns(2)
|
| 553 |
with col1:
|
| 554 |
+
lat = st.number_input("Latitude", -90.0, 90.0, value=st.session_state.default_lat, format="%.6f")
|
| 555 |
with col2:
|
| 556 |
+
lon = st.number_input("Longitude", -180.0, 180.0, value=st.session_state.default_lon, format="%.6f")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 557 |
|
|
|
|
| 558 |
if st.button("Use These Coordinates"):
|
| 559 |
try:
|
| 560 |
+
location_tools = LocationTools(lat, lon)
|
| 561 |
current_location = location_tools.get_current_location()
|
| 562 |
places = location_tools.search_nearby_places(radius)
|
| 563 |
st.success(f"📍 Current Location: {current_location['address']}")
|
|
|
|
| 584 |
logger.error(f"Error processing location: {e}")
|
| 585 |
st.error("Error processing location. Please try again.")
|
| 586 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
if __name__ == "__main__":
|
| 588 |
+
main()
|