1 of 23

Python Web Applications

By Zuhayer Quazi & Mihika Bairathi

2 of 23

Why?

  • Python scripts are cool, but no one has any use for your .py file
  • Leaving ‘desktop’ based applications
  • Use python code to deliver dynamic content
  • All you need is an internet connection!

3 of 23

Flask

4 of 23

Flask

  • Python web-framework
  • Written after Django
  • Great for beginners
    • Also used in real, corporate life!
  • Extensive documentation
  • Built in development server/debugger

pip install flask

5 of 23

Hello World

from flask import Flask

app = Flask(__name__)

@app.route("/")

def hello():

return "Hello World!"

if __name__ == "__main__":

app.run()

6 of 23

URL Routes

7 of 23

URL Routes

  • Make remembering URLs easier
  • google.com
    • /flights
    • /news
    • /gmail

8 of 23

Routes

from flask import Flask

app = Flask(__name__)

@app.route("/")

def index():

return "Index!"

@app.route("/hello")

def hello():

return "Hello World!"

@app.route("/members")

def members():

return "Members"

@app.route("/members/<string:name>/")

def getMember(name):

return name

if __name__ == "__main__":

app.run()

9 of 23

Structure

  • Project Name
    • templates
      • Templates for dynamically generated pages (generally .html pages)
    • static
      • Files that are not dynamically generated (css, images, etc)
    • app.py
      • Our application file!

10 of 23

Templates!

  • Hello World goes in .html file!
    • Don’t put HTML in your app.py → no point, bad practice!
  • from flask import render_template
    • Render template will reach into the templates folder and find the HTML file you are referencing.

11 of 23

Send Information

12 of 23

Sending Information

  • Pass in parameters through render_template()

Add the following:

rest_count = 50

return render_template('index.html', count=rest_count)

13 of 23

Hello, Z

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")

def index():

return "Flask App!"

@app.route("/hello/<string:name>/")

def hello(name):

return render_template('hello.html',name=name)

if __name__ == "__main__":

app.run()

14 of 23

Make it look ‘prettier’

15 of 23

More Variables

return render_template(___.html, **locals())

16 of 23

Random Quote?

  • edit the html file to include {{quote}}
  • edit app.py to choose random quote!
  • From random import randint

17 of 23

Next Steps

  • Adding more layers
    • Connecting templates together
  • Adding CSS
  • Making it DYNAMIC
    • Databases
    • MongoDB
    • SQL

18 of 23

Requests

19 of 23

HTTP - hypertext transfer protocol

  • Set of protocols that allows communication between client and servers
  • Web browser client, application/website is ‘server’
  • POST request
    • Send information to the server (when you login or register)
  • GET request
    • Request data from server (after you login!)
  • Use the `requests` library in Python

20 of 23

Example

21 of 23

Term Projects

22 of 23

Personal Projects > Term Projects

  • We don’t recommend using web-apps for term projects.
  • Need approval from the professor(s) AND Arman.
    • They’ll be pretty strict
  • Make your own website
  • Hackathons
  • Get a job?!?!

23 of 23

Questions?