Flask

Key facts
  • A virtual environment (virtualenv) is an isolated environment for your project
  • virtualenv prevents conflicts of modules.   
  • You can setup a virtual environment with these commands
  • Install Flask in the virtualenv:
  • Lecture Notes
    Key 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 with
    python hello.py
    Open your web browser at
    http://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 
    • This can also be without specific data type

    Lecture Notes
    Key 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
    • Don't forget the line



    Resources for this lecture





    • You can get more than one variable from the URL
    The variables must also exist in the function definition,


    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








No comments:

Post a Comment

Office hours tomorrow(Tuesday) 5:00pm-6:00pm, 4/26/2021, 5:13 PM, English, 4/26/2021, 5:13 PM

Your assigned language is: English Classroom blog: googleclouduconn.blogspot.com 4/26/2021, 5:13 PM Office hours tomorrow(Tuesday) 5...