ImportError: cannot import name ‘Markup’ from ‘jinja2’

As the import error comes from flask File “/usr/local/lib/python3.7/site-packages/flask/__init__.py I tried to create a new Flask application using Flask==1.0.2 and found that the error comes from this version of Flask when it used with Jinja2>=2.10.1. But when you remove Flask==1.0.2 and install Flask==2.0.3, everything works fine. pip uninstall Flask Jinja2 pip install Flask Jinja2 Dependencies … Read more

jinja2 load template from string: TypeError: no loader for this environment specified

You can provide loader in Environment from that list from jinja2 import Environment, BaseLoader rtemplate = Environment(loader=BaseLoader).from_string(myString) data = rtemplate.render(**data) Edit: The problem was with myString, it has {% include ‘test.html’ %} and Jinja2 has no idea where to get template from. UPDATE As @iver56 kindly noted, it’s better to: rtemplate = Environment(loader=BaseLoader()).from_string(myString)

Changing the active class of a link with the twitter bootstrap css in python/flask

Have you looked at this ? https://jinja.palletsprojects.com/en/3.0.x/tricks/#highlighting-active-menu-items Highlighting Active Menu Items Often you want to have a navigation bar with an active navigation item. This is really simple to achieve. Because assignments outside of blocks in child templates are global and executed before the layout template is evaluated it’s possible to define the active menu … Read more

use a css stylesheet on a jinja2 template

All public files (the ones that are not processed, like templates or python files) should be placed into dedicated static folders. By default, Jinja2 has one static folder called static. This should fix your problem: Move /templates/styles.css to /static/styles.css Update your code with following code, that will be translated into correct file location: <link rel=”stylesheet” … Read more

How to get list of all variables in jinja 2 templates

Since no one has answered the question and I found the answer from jinja2 import Environment, PackageLoader, meta env = Environment(loader=PackageLoader(‘gummi’, ‘templates’)) template_source = env.loader.get_source(env, ‘page_content.html’) parsed_content = env.parse(template_source) meta.find_undeclared_variables(parsed_content) This will yield list of undeclared variables since this is not executed at run time, it will yield list of all variables. Note: This will … Read more

Mako or Jinja2? [closed]

I personally prefer Jinja2’s syntax over Mako’s. Take this example from the Mako website <%inherit file=”base.html”/> <% rows = [[v for v in range(0,10)] for row in range(0,10)] %> <table> % for row in rows: ${makerow(row)} % endfor </table> <%def name=”makerow(row)”> <tr> % for name in row: <td>${name}</td>\ % endfor </tr> </%def> There are so … Read more

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