thisAKcode / fastapi_streetart_map

0 stars 0 forks source link

attempt to pass json to js in map2.html #15

Closed thisAKcode closed 2 years ago

thisAKcode commented 2 years ago

Hey, I was not able to send data within content while using TemplateResponse to the js script in map.html templates.TemplateResponse("map.html", context)

I tried to add CORSmiddleware thing to grab data from api directly and then send int ot map2.html. maybe I keep this and remove TemplateResponse solution? Meanwhile i'll keep both in order to learn how templateresponse works.

bbelderbos commented 2 years ago

The FastAPI side is correct and locs has data in map.html and it renders in the browser. But I don't think you can pass data from map.html into loci.js, so what about moving the function calls into map.html so you can actually embed variables, e.g.:

image
thisAKcode commented 2 years ago

Thanks for review. I'll try to work it out.

thisAKcode commented 2 years ago

The FastAPI side is correct and locs has data in map.html and it renders in the browser. But I don't think you can pass data from map.html into loci.js, so what about moving the function calls into map.html so you can actually embed variables, e.g.:

image

make sense at the same time I'll have quite big js code file and it'll be hard to re use in another app. So I try to grab data from api and pass it to js, which almost works now:

@app.get("/map2/", response_class=HTMLResponse)
async def _map(
    request:Request,
    db:Session = Depends(get_db)
    ):
    locations1 = db.query(model.ArtItem).all() # []
    print(type(locations1[0]))
    #locs = json.dumps({"message": "fine"}, indent = 4)
    json_object = json.dumps({"message": "fine"}, indent = 4)
    return json_object