Flask Dynamic data update without reload page

Working example: app.py from flask import Flask, render_template, request import requests from bs4 import BeautifulSoup app = Flask(__name__) @app.route(“https://stackoverflow.com/”) def index(): return render_template(‘index.html’) @app.route(‘/suggestions’) def suggestions(): text = request.args.get(‘jsdata’) suggestions_list = [] if text: r = requests.get(‘http://suggestqueries.google.com/complete/search?output=toolbar&hl=ru&q={}&gl=in’.format(text)) soup = BeautifulSoup(r.content, ‘lxml’) suggestions = soup.find_all(‘suggestion’) for suggestion in suggestions: suggestions_list.append(suggestion.attrs[‘data’]) #print(suggestions_list) return render_template(‘suggestions.html’, suggestions=suggestions_list) if __name__ … Read more

How to use url_for() to pass path and query data to a route using FastAPI and Jinja2?

This is not FastAPI’s issue, but rather Starlette’s issue (i.e., request.url_for() receives path parameters, not query parameters). So, nspired by #560 and #1385, I have created the following working example for calling FastAPI routes from within Jinja2 templates, and passing query params (alone or along with path params as well). Please note that this is … Read more

Link to a specific location in a Flask template

Thanks to dirn’s comments I managed to get this working with the code below. Pass the _anchor keyword to url_for to append an anchor to the generated URL. nav menu: <a href=”https://stackoverflow.com/questions/35843675/{{ url_for(“.stuff’, _anchor=”exactlocation”) }}”>Go to specific id on suff page</a> Flask route: @main.route(“https://stackoverflow.com/”) def stuff(): return render_template(‘stuff.html’) stuff.html: … <section id=”exactlocation”> …

Chrome blocks FastAPI file download using FileResponse due to using HTTP instead of HTTPS

Option 1 You could use HTTPSRedirectMiddleware to enforce all incoming requests to http being redirected to the secure scheme instead. from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware app = FastAPI() app.add_middleware(HTTPSRedirectMiddleware) Option 2 In addition to the above, you could use relative URLs instead of using url_for() in your Jinja2 template; for instance: <a href=”/upload_schedule”>Link text</a> In this … Read more