Key facts
- A virtual environment (virtualenv) is an isolated environment for your project
- A virtualenv prevents conflicts of modules.
- You can setup a virtual environment with these commands
- virtualenv -p python3 example
- cd example
- source bin/activate
- mkdir src
- cd src
- Install Flask in the virtualenv:
- # pip needs to be installed
- pip install flask
- Lecture NotesKey facts
- Setup a virtualenv
- Allow port 5000 in your firewall (if you have one running)
- Save the code below as hello.py
The code to create a hello world app:
- from flask import Flask
- app = Flask(__name__)
- @app.route('/')
- def index():
- return 'Python Flask'
- @app.route('/hello')
- def helloIndex():
- return 'Hello World from Python Flask!'
- app.run(host='0.0.0.0', port= 5000)
Start the server withpython hello.pyOpen your web browser athttp://0.0.0.0:5000/hello
Resources for this lecture- hello.py
Lecture Notes
Key facts- A route is a mapping of an URL, website address, to a Python function
- Add
@app.route('/hello')
before a function definition, where hello is your URL path - The python return string is shown in the web browser
- Lecture Notes
- Key facts
- A dynamic route is used to pass URL variables
- A route can accept a variable
- @app.route("/hello/<string:name>/")
- This can also be without specific data type
Lecture NotesKey facts- Templates are stored in the
/templates/
directory - A template is an HTML file (Flask uses the Jinja template engine)
- Templates can contain Python variables using this syntax
{{x}}
- Python functions can return templates
- # your function
- return render_template('example.html')
- Don't forget the line
- # top of python file
- import from flask import Flask, render_template, request
Resources for this lecture
- @app.route("/hello/<name>/")
- You can get more than one variable from the URL
- @app.route("/user/<name>/<age>")
The variables must also exist in the function definition,- @app.route("/user/<name>/<age>")
- def getUser(name, age):
Resources for this lecture
ecture Notes
Key facts
Static files (images, downloadables) are stored in the /static/ folder
Load a static file like this {{ url_for('static', filename = 'city.jpg') }}
Resources for this lecture
Instructions: Make a BMI Calculator
Create a Flask web app: a BMI calculator
This takes as input: the weight, the length
BMI = weight / (length*length)
Take it as input in a form, then when submit button is pressed show the result.
If you want you can style the app with css
We'll show the solution in the next lecture
Lecture Notes
Key facts
You can add forms simply by creating a route,
@app.route('/register/')
def register():
return render_template('register.html')
The template register.html then contains
<html><body>
<form action = "http://localhost:5000/result" method = "POST">
<p>Name <input type = "text" name = "Name" /></p>
<p>Job <input type = "text" name = "Job" /></p>
<p>Country <input type ="text" name = "Country" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body></html>
Then grab the form output
@app.route('/result',methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
result = request.form
return render_template("result.html",result = result)
If you want to show the variables in result.html
<html>
<body>
<table border = 1>
{% for key, value in result.items() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>Lecture Notes
Key facts
Cookies are files stored locally on the users computer
You can store information like country or shopping bag contents
To set a cookie
resp = make_response(render_template('result.html'))
resp.set_cookie('user', request.form['user'])
To get a cookie
name = request.cookies.get('user')
Overview
Q&A
Notes
Announcements
</body>
</html>
Lecture Notes
Key facts
A session is active until it expires or is deleted
To set a session variable
# sets variable user
session['user'] = request.form['user']
To get a session variable
# gets variable user, stores it as variable name
name = session['user']
To delete a session variable
# deletes variable user
session.pop('user', None)
Resources for this lecture
sessionExample.zip
Key facts
A session is active until it expires or is deleted
To set a session variable
# sets variable user
session['user'] = request.form['user']
To get a session variable
# gets variable user, stores it as variable name
name = session['user']
To delete a session variable
# deletes variable user
session.pop('user', None)
Resources for this lecture
sessionExample.zip
No comments:
Post a Comment