How to navigate through FastAPI routes by clicking on HTML button in Jinja2 Templates?

Backend Your /tasks endpoint (i.e., find_all_tasks function) should return a new Jinja2 TemplateResponse with the list of tasks. For instance: @app.get(‘/tasks’) def find_all_tasks(request: Request): tasks = [‘task 1’, ‘task 2’, ‘task 3’] return templates.TemplateResponse(“tasks.html”, {“request”: request, ‘tasks’: tasks}) Frontend 1. Click on the URL to get to the tasks page In index.html, you can use … Read more

How to generate an html directory list using Python

You could separate the directory tree generation and its rendering as html. To generate the tree you could use a simple recursive function: def make_tree(path): tree = dict(name=os.path.basename(path), children=[]) try: lst = os.listdir(path) except OSError: pass #ignore errors else: for name in lst: fn = os.path.join(path, name) if os.path.isdir(fn): tree[‘children’].append(make_tree(fn)) else: tree[‘children’].append(dict(name=name)) return tree To … Read more

How to use jinja2 as a templating engine in Django 1.8

Frist you have to install jinja2: $ pip install Jinja2 Then modify your TEMPLATES list in the settings.py to contain the jinja2 BACKEND : TEMPLATES = [ { ‘BACKEND’: ‘django.template.backends.jinja2.Jinja2’, ‘DIRS’: [os.path.join(BASE_DIR, ‘templates/jinja2’)], ‘APP_DIRS’: True, ‘OPTIONS’: {‘environment’: ‘myproject.jinja2.Environment’,}, }, { ‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’, ‘DIRS’: [], ‘APP_DIRS’: True, ‘OPTIONS’: { ‘context_processors’: [ ‘django.template.context_processors.debug’, ‘django.template.context_processors.request’, ‘django.contrib.auth.context_processors.auth’, ‘django.contrib.messages.context_processors.messages’, ], … Read more

Serve static files from a CDN rather than Flask in production

There’s no need to change how you link to static files, you can still use url_for(‘static’, filename=”myfile.txt”). Replace the default static view with one that redirects to the CDN if it is configured. from urllib.parse import urljoin # or for python 2: from urlparse import urljoin from flask import redirect @app.endpoint(‘static’) def static(filename): static_url = … Read more