More Related Content Similar to Web develop in flask (20) Web develop in flask5. Given a URL
http://www.bing.com/search?q=math
Protocol IP / Hostname Path Query String
6. HTTP Protocol
A communication protocol
A well-known client is web browser
Not merely for browser
9. GET
Browser a website by url
Request resources (contents)
Example: http://www.bing.com/search?q=math
12. IP / Hostname
Address of a server
You can register a Hostname (domain name)
Hostname will be resolve to a IP address by DNS
Where a request will be delivered to
13. Some Special IPs
127.0.0.1 : loopback address
0.0.0.0 : Network traffic from any valid IP address
192.168.x.x : Preserved private IP address
17. Inspect Web Server
Web Server
(apache2, nginx)
Python
Web
Application
Request
Request
Object
Response
Response
Object
18. Inspect Web Server
Web Server
(apache2, nginx)
Python
Web
Application
Request
Request
Object
Response
Response
Object
What we focus on
26. First App
Create an app
Assign path
server.py
Enable Debug Mode
View function
27. First App
Create an app
Assign path
View function
Start the server
server.py
Enable Debug Mode
36. Exercise I
Given a dictionary:
{“apple”: “a fruit”, “calculus”: “a subject”, … and more}
Query it by GET method
Display “Not found” if the key is unavailable
41. Is there a way to move
contents out?
A: Use Template (jinja2)
42. Create a templates
<= Very important
Create a “templates” folder
Create a txt file (hello.txt) in templates folder
Write contents in this file
43. Use template in view function
from flask import render_template
render_template(“hello.txt”)
47. A HTML Element
<tag_name attr1=“value” attr2=“value”> content
</tag_name>
<p> This is a book </p>
<h1> This a title </h1>
48. Some Elements
<p> … </p>
<h1> … <h1> to </h5> … </h5>
<div> … </div>
<ul>
<li> item </li>
<li> item </li>
</ul>
<ol>
<li> item </li>
<li> item </li>
</ol>
58. Transfer variables to template
Use keyword arguments in render_template
render_template(“hi.html”, name=name)
59. For, If Statement
{% if name == “Jim” %} …… {% endif %}
{% for item in things %} {% endfor %}
60. Case Study: Food List
Display a list of your favorite food if it is public
Otherwise, display a message
64. HTML Form
Form elements
form - a block of a form
input - input fields
label - label for an input
65. Case: Who are you?
Ask for a name
Once a name is submitted, Display the welcome message.
67. view function for whoareyou
Allow method for POST
Handle POST
request
Handle GET
request
72. Save to file
Use pickle module (pickle.load and pickle.dump)
pickle.load(open(“todo_list.data”, “r”))
pickle.dump(dictionary, open(“todo_list.data”, “r”))