cs1520 recitation flask 2 templating
play

CS1520 Recitation: Flask 2: Templating Jeongmin Lee Plan for Today - PowerPoint PPT Presentation

CS1520 Recitation: Flask 2: Templating Jeongmin Lee Plan for Today Templating in Flask Jinja Tags Control Flow Static Files Templating In previous example Everything to show from flask import Flask app = Flask(__name__)


  1. CS1520 Recitation: Flask 2: Templating Jeongmin Lee

  2. Plan for Today ● Templating in Flask ● Jinja Tags ● Control Flow ● Static Files

  3. Templating

  4. In previous example ● Everything to show from flask import Flask app = Flask(__name__) would be written in a python code @app.route('/') def hello_world(): return 'Hello World!' ● That is, what is seen is if __name__ == '__main__': coupled to what is app.run() processed

  5. Templating ● Let’s decouple this mechanism: ○ Place what is seen into a template html file ○ Place what is processed into a python file render_template (“_template.html”) links two ● components # file structure: ./hello.py ./templates/_template.html

  6. Templating # _template.html # hello.py <!doctype html> from flask import Flask, render_template <html> app = Flask(__name__) <body> @app.route('/hello/<user>') <h1>Hello {{ name }}!</h1> def hello_name(user): return render_template(_template.html', name = user) </body> </html> if __name__ == '__main__': app.run(debug = True) Run hello.py and visit http://127.0.0.1/hello/yourname Example code from tutorialspoint.com

  7. Control Statement

  8. (Recap) Jinja Tags ● {{ ... }} ○ Expression tag, contents are evaluated and place in the text ● {% ... %} ○ Statement tag, used to define Jinja constructs and issue flow control statements ● {# ... #} ○ Comment

  9. Templating with if/else from flask import Flask, render_template <!doctype html> app = Flask(__name__) <html> <body> @app.route('/hello/<int:score>') def hello_name(score): {% if marks>50 %} return render_template('hello.html', <h1> Your result is pass!</h1> marks = score) {% else %} <h1>Your result is fail</h1> if __name__ == '__main__': {% endif %} app.run(debug = True) </body> </html>

  10. Loop in Template ● Python loop constructors can be used inside a template ● {% for x in a_list %} {{ x }} {% endfor %} ● Notice that inside loop, {{ x }} is used instead of {% x %}

  11. Loop in Template with Dictionary from flask import Flask, render_template <!doctype html> app = Flask(__name__) <html> <body> @app.route('/result') def result(): <table border = 1> dict = {'phy':50,'che':60,'maths':70} {% for key, value in result.iteritems() %} return render_template('result.html', result = dict) <tr> <th> {{ key }} </th> if __name__ == '__main__': <td> {{ value }} </td> app.run(debug = True) </tr> {% endfor %} </table> </body> </html>

  12. Static Files

  13. Static files ● There are some files that are not dynamically changed its contents while being used. ● Example: CSS and Javascript files

  14. Static files ● There are some files that are not dynamically changed its contents while being used. ● Example: CSS and Javascript files that helps display ● While templating, you would be confused about routings (locating location of a file: relative path? ) ● In Jinja, there is a solution.

  15. Static files ● Place all of your static files (.css, .js) in ./static folder. ● In template html file, source files with {{ url_for(‘static’, filename = ‘hello.js’ ) }} <html> <head> <script type = "text/javascript" src = "{{ url_for('static', filename = 'hello.js') }}" ></script> </head> <body> <input type = "button" onclick = "sayHello()" value = "Say Hello" /> </body> </html>

  16. Questions?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend