-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_web_hello_world_template.txt
48 lines (34 loc) · 1.45 KB
/
07_web_hello_world_template.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Code explanation
================
- There are two more things Flask can do for you: serve static files and render templates
- Everything you put to "static" will be available at http://127.0.0.1:5000/static/.
Check out http://127.0.0.1:5000/static/background.jpg
- HTML-files, put to templates can be used as so-called "jinja-templates".
- Jinja templates look like regular HTML files, but inclusions like {{ var }}
or {% for i in something %}...{% endfor %} interpreted on the server
side on the fly
- There is a bolierplate code in splash.html and styles.css to show
"inspiring splash thing"
- flask.render_template('splash.html', text=text) does the trick
Code run
========
Try these URLs:
- http://127.0.0.1:5000
- http://127.0.0.1:5000/user/Roman
- http://127.0.0.1:5000/user/%3Ch1%3ERoman
You can see how render_template works if you run a special
"flask-activated" shell with:
$ FLASK_APP=07_web_hello_world_template.py flask shell
>>> import flask
>>> flask.render_template('splash.html', text='Hello world')
Documentation
===================
- Jinja documentation: http://jinja.pocoo.org/docs/
Practical task
==============
Modify the Hello World example to show some random quotes of wisdom instead.
- http://127.0.0.1:5000 has to show one randomly chosen quote from quotes.txt
or quotes.csv
- http://127.0.0.1:5000/<quote_id> has to show the quote with the given id
from quotes.csv. If nothing is found, it has to show the text
"nothing is found"