• No results found

CIS 192: Lecture 10 Web Development with Flask

N/A
N/A
Protected

Academic year: 2021

Share "CIS 192: Lecture 10 Web Development with Flask"

Copied!
22
0
0

Loading.... (view fulltext now)

Full text

(1)

CIS 192: Lecture 10

Web Development with Flask

Lili Dworkin University of Pennsylvania

(2)

Web Frameworks

I We’ve been talking about making HTTP requests

I What about serving them?

I Flask is a “microframework” – small and simple, and you can see how and why everything is happening

I Django is the “big guy on the block” – more fully featured, but also more black magic / mysterious

(3)

Hello World

from flask import Flask app = Flask(__name__) @app.route('/')

def hello_world():

return "Hello World!" if __name__ == '__main__':

app.run()

prompt$ python flask.py

(4)

Hello World

app = Flask(__name__)

I When we create an instance of the Flask class, the first argument is the name of the application’s module or package

I When using a single module, use __name__because this will work regardless of whether __name__equals ’__main__’or the actual import name

(5)

Hello World

@app.route('/') def hello_world():

return "Hello World!"

I The app.route('/')decorator tells Flask to call the hello_world()function when the relative url ’/’ is accessed

I The hello_world()function returns the web page (in this case, a simple string) to be displayed

(6)

Hello World

app.run()

I Theapp.run() function runs the application on a local server

I This will only be visible on your own computer! We will talk about deployment later

(7)

Debugging

I When testing, use app.run(debug=True)

I Now the server will reload itself on code changes

I Additionally, you will see error messages in the browser

(8)

More Routing

@app.route('/bad') def bad(): return 'hi' + 4 @app.route('/bye') def bye_world():

(9)

Variable Rules

I To add variable parts to a url, use <variable_name>

I The variables are passed as arguments to the function @app.route('/user/<username>')

def greet_user(username):

(10)

Variable Rules

Multiple urls can route to the same function: @app.route('/name/<first>')

@app.route('/name/<first>/<last>') def greet_name(first, last=None):

name = first + ' ' + last if last else first return "Hello %s!" % name

(11)

Templating

What about some real HTML?

Flask uses a templating system called Jinja. <!doctype html>

<title>Hello from Flask</title> {% if name %}

<h1>Hello {{ name }}!</h1> {% else %}

<h1>Hello World!</h1> {% endif %}

(12)

Templating

from flask import render_template @app.route('/template/')

@app.route('/template/<name>') def template(name=None):

(13)

GET Requests

I Recall: a url can be accessed with parameters, i.e. /hello?key=value

I Retrieve these parameters from the request.args dictionary from flask import request

@app.route('/args') def args():

html = ''

for key, value in request.args.items(): html += '%s=%s' % (key, value) html += '<br/>'

(14)

GET Requests

Even better, using templates: <!doctype html>

<title>Displaying Params</title> <ul>

{% for key, value in params.items() %} <li>{{ key }}={{ value }}</li>

{% endfor %} </ul>

@app.route('/template_args') def template_args():

return render_template('params.html', params= request.args)

(15)

POST Requests

I We can also make POST requests to a url

I Add keyword argument methods=['POST', 'GET']to the app.route()decorator

I Check if a request was a POST by looking at request.method

I The data from a POST request can be retrieved from the request.form dictionary

(16)

POST Requests

@app.route('/post', methods=['POST', 'GET']) def post():

if request.method == 'POST':

return request.form.get('data', 'default') else:

return 'That was a GET request.'

>>> req = requests.post('http://127.0.0.1:5000/post', data={'data':'test data'})

>>> req.text u'test data'

>>> req = requests.post('http://127.0.0.1:5000/post') >>> req.text

(17)

Returning JSON

Instead of returning HTML source, what if we want to return JSON?

from flask import jsonify @app.route('/json')

def return_json():

return jsonify({'some': 'data'})

>>> req = requests.get('http://127.0.0.1:5000/json') >>> req.json()

(18)

Sessions

Sometimes you need to store information between requests. For this we use the “session” object, which is essentially a cookie. from Flask import session

app.secret_key = os.urandom(24) @app.route('/step1')

def step1():

session['key'] = '12345' return 'Saved key.' @app.route('/step2') def step2():

key = session['key']

(19)

url for

What if we need to know the link for one of our functions? from Flask import url_for

@app.route('/url') def url():

html = 'relative url: %s <br/>' % (url_for('bye_world'))

html += 'absolute url: %s' %

(url_for('bye_world', _external=True)) return html

(20)

Back to Twitter

Let’s recall the 3-legged OAuth process:

1. Get a request token

2. Send user to an authorization url

3. Redirect user back to application with their pin/verifier

4. Get an access token

(21)

Back to Twitter

Getting request/access tokens is a little tricky – we need to:

1. Put together the consumer/key secret (use oauth.Consumer)

2. For the access token only: Put together the request token key/secret (use oauth.Token)

3. Put our params in a dictionary

I For the request token, this is{oauth_callback: ...} I For the access token, this is{oauth_verifier: ...}

4. Make a signed request (use oauth.Request)

5. POST to the signed url

6. Parse the body of the response for the key and secret I The response body will be of the form

(22)

Back to Twitter

Posting the status update is pretty similar:

1. Put together the consumer/key secret (use oauth.Consumer)

2. Put together the access token key/secret (use oauth.Token)

3. Put our params in a dictionary: {status: ...}

4. Make a signed request (use oauth.Request)

References

Related documents

In this study, it is aimed to develop the Science Education Peer Comparison Scale (SEPCS) in order to measure the comparison of Science Education students'

By first analysing the image data in terms of the local image structures, such as lines or edges, and then controlling the filtering based on local information from the analysis

Many nonprofit organizations have elected to reorganize themselves into systems of affiliated nonprofit organizations. A typical simple struc- ture includes a parent organization

Rotating inside knob/ lever simultaneously retracts latchbolt and unlocks outside knob/lever.. Auxiliary latch deadlocks latchbolt when door

• Follow up with your employer each reporting period to ensure your hours are reported on a regular basis?. • Discuss your progress with

The main optimization of antichain-based algorithms [1] for checking language inclusion of automata over finite alphabets is that product states that are subsets of already

The issue of an interoperable North-South railway corridor linking the Baltic Countries (Lithuania, Latvia and Estonia) with Poland and the rest of the EU rail network can be seen

CDBG will use the parallel TSEP ranking criterion general scoring definitions to rank the project concept and technical design for the proposed CDBG project.. CDBG will use