jinja2
---
{{ variable | filter }}
{{ variable | filter | filter }}
Jinja2_filters.py
# incremental code - jinja2 filters # SECTION 3: LECTURE 15 from flask import Flask, render_template, request app = Flask(__name__) @app.route('/index') @app.route('/') def hello_flask(): return 'Hello Flask' @app.route('/new/') def query_string(greeting='hello'): query_val = request.args.get('greeting', greeting) return '<h1> the greeting is: {0} </h1>'.format(query_val) @app.route('/user') @app.route('/user/<name>') def no_query_strings(name='mina'): return '<h1> hello there ! {} </>'.format(name) # strings @app.route('/text/<string:name>') def working_with_strings(name): return '<h1> here is a string: ' + name + '</h1>' # numbers @app.route('/numbers/<int:num>') def working_with_numbers(num): return '<h1> the number you picked is: ' + str(num) + '</h1>' # add numbers @app.route('/add/<int:num1>/<int:num2>') def adding_integers(num1, num2): return '<h1> the sum is : {}'.format(num1 + num2) + '</h1>' # floats @app.route('/product/<float:num1>/<float:num2>') def product_two_numbers(num1, num2): return '<h1> the product is : {}'.format(num1 * num2) + '</h1>' # rendering templates @app.route('/temp') def using_templates(): return render_template('hello.html') # JINJA TEMPLATES 1 @app.route('/watch') def top_movies(): movie_list = ['autopsy of jane doe', 'neon demon', 'ghost in a shell', 'kong: skull island', 'john wick 2', 'spiderman - homecoming'] return render_template('movies.html', movies=movie_list, name='Harry') # JINJA TEMPLATES 2 @app.route('/tables') def movies_plus(): movies_dict = {'autopsy of jane doe': 02.14, 'neon demon': 3.20, 'ghost in a shell': 1.50, 'kong: skull island': 3.50, 'john wick 2': 02.52, 'spiderman - homecoming': 1.48} return render_template('table_data.html', movies=movies_dict, name='Sally') # JINJA2 - FILTERS @app.route('/filters') def filter_data(): movies_dict = {'autopsy of jane doe': 02.14, 'neon demon': 3.20, 'ghost in a shell': 1.50, 'kong: skull island': 3.50, 'john wick 2': 02.52, 'spiderman - homecoming': 1.48} return render_template('filter_data.html', movies=movies_dict, name=None, film='a christmas carol') if __name__ == '__main__': app.run(debug=True)
filter_data.html
<!-- HTML FOR - JINJA2 FILTERS --> <!-- SECTION 3: LECTURE 15 --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Data Filters</title> </head> <body> <h2>working with jinja2 filters</h2> <ul> {% for movie, duration in movies|dictsort (false, 'value') %} <li> {{ movie|title }} : {{ duration|round(1) }} </li> {% endfor %} </ul> <hr> <ol type="1"> <li> default value : {{ name|default('value not defined', true) }}</li> <br> <li> capital case : {{ film|capitalize }}</li> <br> <li> length : {{ movies|length }} items in the dictionary </li> <br> <li> first item : {{ movies|first }}</li> <br> <li> make a list : {{ film|list }}</li> <br> <li> replace data : {{ film|replace('carol', 'cookie')|upper }}</li> </ol> <hr> {{ data }} <h2>more filters</h2> <p> filters are like little utilities that help us modify the variables or their state. for a complete list of built-in jinja2 filters, check-out the following link: </p> <a href="http://jinja.pocoo.org/docs/2.9/templates/#builtin-filters" target="_blank"> http://jinja.pocoo.org/docs/2.9/templates/#builtin-filters </a> </body> </html>
No comments:
Post a Comment