Spaces:
Runtime error
Runtime error
Commit
·
b11d1b5
1
Parent(s):
a05c3f3
Add basic wiki summary retrieval of a fixed place with marker displaying the information
Browse files- frontend/src/components/Map.js +29 -3
- main.py +37 -0
frontend/src/components/Map.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
-
import React from 'react';
|
| 2 |
import { MapContainer, TileLayer,
|
| 3 |
useMapEvents,
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
} from 'react-leaflet';
|
| 7 |
import L from 'leaflet';
|
| 8 |
import 'leaflet/dist/leaflet.css';
|
|
@@ -25,7 +26,17 @@ const ClickHandler = ({ onMapClick }) => {
|
|
| 25 |
return null;
|
| 26 |
};
|
| 27 |
|
|
|
|
|
|
|
| 28 |
const Map = ( { onMapClick } ) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return (
|
| 30 |
<MapContainer
|
| 31 |
center={[0, 0]}
|
|
@@ -38,7 +49,22 @@ const Map = ( { onMapClick } ) => {
|
|
| 38 |
/>
|
| 39 |
|
| 40 |
<ClickHandler onMapClick={onMapClick}/>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
| 42 |
</MapContainer>
|
| 43 |
);
|
| 44 |
};
|
|
|
|
| 1 |
+
import React, { useState, useEffect, useCallback } from 'react';
|
| 2 |
import { MapContainer, TileLayer,
|
| 3 |
useMapEvents,
|
| 4 |
+
Marker,
|
| 5 |
+
Popup ,
|
| 6 |
+
// useMap
|
| 7 |
} from 'react-leaflet';
|
| 8 |
import L from 'leaflet';
|
| 9 |
import 'leaflet/dist/leaflet.css';
|
|
|
|
| 26 |
return null;
|
| 27 |
};
|
| 28 |
|
| 29 |
+
|
| 30 |
+
|
| 31 |
const Map = ( { onMapClick } ) => {
|
| 32 |
+
const [wikiContent, setWikiContent] = useState(null);
|
| 33 |
+
const fetchWiki = async (pageName) => {
|
| 34 |
+
const res = await fetch(`http://localhost:8004/wiki/${pageName}`);
|
| 35 |
+
const data = await res.json();
|
| 36 |
+
setWikiContent(data);
|
| 37 |
+
};
|
| 38 |
+
const markerPosition = [21.2514, 81.6296];
|
| 39 |
+
|
| 40 |
return (
|
| 41 |
<MapContainer
|
| 42 |
center={[0, 0]}
|
|
|
|
| 49 |
/>
|
| 50 |
|
| 51 |
<ClickHandler onMapClick={onMapClick}/>
|
| 52 |
+
<Marker position={markerPosition} eventHandlers={{
|
| 53 |
+
click: () => fetchWiki("Raipur"),
|
| 54 |
+
}}>
|
| 55 |
+
<Popup minWidth={250}>
|
| 56 |
+
{wikiContent ? (
|
| 57 |
+
<>
|
| 58 |
+
<strong>{wikiContent.title}</strong><br />
|
| 59 |
+
<p style={{ fontSize: '12px' }}>{wikiContent.content}</p>
|
| 60 |
+
</>
|
| 61 |
+
) : (
|
| 62 |
+
"Click marker to load Wikipedia content"
|
| 63 |
+
)}
|
| 64 |
+
</Popup>
|
| 65 |
+
</Marker>
|
| 66 |
|
| 67 |
+
{/* Example marker */}
|
| 68 |
</MapContainer>
|
| 69 |
);
|
| 70 |
};
|
main.py
CHANGED
|
@@ -1,7 +1,44 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
@app.get("/")
|
| 6 |
def health_check():
|
| 7 |
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
+
# from pydantic import BaseModel
|
| 5 |
+
import requests
|
| 6 |
+
from geopy.geocoders import Nominatim
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
loc = Nominatim(user_agent="GetLoc")
|
| 11 |
+
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"], # Replace with your frontend domain in prod
|
| 15 |
+
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"],
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
@app.get("/")
|
| 21 |
def health_check():
|
| 22 |
return {"status": "ok"}
|
| 23 |
+
|
| 24 |
+
@app.get("/wiki/{page_name}")
|
| 25 |
+
async def get_wiki_page(page_name: str):
|
| 26 |
+
response = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{page_name}", timeout=10)
|
| 27 |
+
|
| 28 |
+
if response.status_code != 200:
|
| 29 |
+
return JSONResponse(
|
| 30 |
+
content={"error": "Page not found"},
|
| 31 |
+
status_code=404
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
coords = loc.geocode(page_name)
|
| 35 |
+
|
| 36 |
+
return JSONResponse(
|
| 37 |
+
content={
|
| 38 |
+
"title": page_name,
|
| 39 |
+
"content": f"{response.json().get('extract', 'No content available')}",
|
| 40 |
+
"latitude": coords.latitude if coords else None,
|
| 41 |
+
"longitude": coords.longitude if coords else None
|
| 42 |
+
},
|
| 43 |
+
status_code=200
|
| 44 |
+
)
|